0

I have a text file called: example.txt which contains these lines:

email1:location1
email2:location2
email3:location3

I want to change it up to look like this:

location1:email1 
location2:email2 
location3:email3 

What would be the easiest way to do this? I tried several awk commands, but couldnt get them to work. From what I read so far, its not possible with cut.
I am using Ubuntu or GNUwin32 on Windows.

Jotne
  • 40,548
  • 12
  • 51
  • 55
Simon
  • 19
  • 4

2 Answers2

3

Simply

awk -F : 'BEGIN { OFS = FS } { print $2, $1 }' filename

The only trick here is that the output field separatorOFS needs to be set to the field separator FS (that -F : sets to :) so the fields are separated the same way in the output as in the input. $2 refers to the second field in the line, $1 to the first.

Also possible:

awk -F : '{ print $2 ":" $1 }' filename

...at the cost of having to change the call in two places to use it with a different separator.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
1

Here is one awk

awk -F: '$0=$2":"$1' file
location1:email1
location2:email2
location3:email3

-F: sets field separator to :
$0=$2":"$1 reconstruct line by using field 2 before field 1
Since this always will be true, it also will do the default action, print the line.

Jotne
  • 40,548
  • 12
  • 51
  • 55