sorting - change column position in a text linux file -


i'm looking solution change position of column in linux. in particular case 1st position last position.

qty | sender | recipient | email subject  sender | recipient | email subject | qty 

if have file following content:

4 | 1 | 2 | first subject 5 | 1 | 4 | other interesting subject 

i have following output:

one | 2 | first subject | 4 1 | 4 | other interesting subject | 5 

delimiter "|". not important if have "|" @ beginning or @ end of each row.

thank you!

this kind of thing classic work sed:

sed 's/\(^[^|]*\)|\(.*$\)/\2 | \1/' yourfile.txt >newfile.txt 

to save changes directly in same file:

sed -i 's/\(^[^|]*\)|\(.*$\)/\2 | \1/' yourfile.txt 
  • ^[^|]* --> beginning of line until first vertical bar.
  • .*$ --> remaining characters until end of line.
  • \( \) --> save these parts , recover later \{number}
  • \2 | \1 --> recover saved part new order.

to know more sed here.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -