blackroad
blackroad

Reputation: 39

Swap in shell from file

Through cut -d":" -f1,3 I made a new file which looks like this:

username1:number1
username2:number2
username3:number3

But my point is, I want to my file to looks like this:

number1:username1
number2:username2
number3:username3

I tried that cut -d":" -f3,1 but it still gets me username1:number1 even when I want to be that 3rd column be the 1st and the 1st column to print it like a last one ... Any help with that ?

Upvotes: 0

Views: 89

Answers (2)

ghoti
ghoti

Reputation: 46856

I like awk for this sort of thing. You've already got an awk answer though.

To do this in pure bash, you'd do the following:

while IFS=: read -r one two; do printf '%s:%s\n' "$two" "$one"; done < input.txt

The IFS variable is the field separator used to slice up the input into separate variables for read, and I'm using printf to gives us predictably formatted output.

Upvotes: 1

thanasisp
thanasisp

Reputation: 5975

cut -f3,1 will print the same as cut -f1,3. Use awk:

awk -F: '{print $3 FS $1}' file

Upvotes: 4

Related Questions