1

I'm currently learning CentOS and need some assistance if possible. I have a file UserNameList.lst, which is used to generate user accounts. The content of the file is below

Josh, Adams, Joshadams@gmail.com
Henry, Ford, HFord@gmail.com

I need to output a txt file which looks like this. (basically combining column 2 and 1 to make a single column)

Adams Josh
Ford Henry

I tried using the command

cut -d "," -f 1 >> Last.txt
cut -d "," -f 2 >> First.txt
paste First.txt Last.txt >> full

which outputs

Adams    Josh
Ford     Henry

Is there a simpler way to do this?

Satō Katsura
  • 13,368
  • 2
  • 31
  • 50
Leon
  • 15

1 Answers1

1

This is best suited for awk

$ cat ip.txt 
Josh, Adams, Joshadams@gmail.com
Henry, Ford, HFord@gmail.com

$ awk -F"[ ,]+" '{print $2, $1}' ip.txt 
Adams Josh
Ford Henry
  • -F"[ ,]+" field separator is one or more of space and , characters
  • Then simply print second and first columns
Sundeep
  • 12,008
  • thanks for the prompt reply! Would you happen to know if I would be able to assign each column to a variable to use as a variable for loop? – Leon Oct 23 '16 at 04:00
  • @Leon, see https://unix.stackexchange.com/questions/103932/parsing-a-delimited-text-file-in-bash-as-command-arguments – Sundeep Oct 23 '16 at 04:02