3

Example csv:

AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

Now i wan't a copy column 2 (BBB) and add it in front of column 3 so the file looks like:

AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH

2 Answers 2

7
$ cat test.txt
AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

$ awk -F, '{$2=$2","$2}1' OFS=, test.txt
AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
2
  • 1
    much cleaner than my answer :) Commented Aug 31, 2018 at 9:15
  • In this case I think this is enough: awk '{$2=$2" "$2}1' test.txt Commented Sep 1, 2018 at 22:47
4

awk '{print $1,$2,$2,$3,$4,$5,$6,$7,$8}' file.csv

Example:

 ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk '{print $1,$2,$2,$3,$4,$5,$6,$7,$8}'
 AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
2
  • Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
    – T-One
    Commented Aug 31, 2018 at 9:15
  • 1
    check out @Kamaraj's answer, should do the trick for you Commented Aug 31, 2018 at 9:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .