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
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
  • 1
    much cleaner than my answer :) – RobotJohnny Aug 31 '18 at 9:15
  • In this case I think this is enough: awk '{$2=$2" "$2}1' test.txt – Claes Wikner Sep 1 '18 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
  • 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 Aug 31 '18 at 9:15
  • 1
    check out @Kamaraj's answer, should do the trick for you – RobotJohnny Aug 31 '18 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.