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, [email protected]
Henry, Ford, [email protected]

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?

1 Answer 1

1

This is best suited for awk

$ cat ip.txt 
Josh, Adams, [email protected]
Henry, Ford, [email protected]

$ 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
2
  • 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? Commented Oct 23, 2016 at 4:00
  • @Leon, see unix.stackexchange.com/questions/103932/… Commented Oct 23, 2016 at 4:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.