user2809888
user2809888

Reputation:

How to move last column to nth column?

I need to move last column of my text to the nth place ,I tried many variants of awk but not able to maintain the alingment of the file.

Below is the replica of the file I am working on:

2014-05-27T02:26:46-05:00 nil-severity uuuu vvvvv xxxx yyyy zzzz  
2014-05-27T02:26:22-05:00 nil-severity vvvvv xxxx yyyy zzzz    
2014-05-27T02:25:58-05:00 nil-severity xxxx yyyy zzzz  
2014-05-27T02:25:58-05:00 nil-severity yyyy zzzz  
2014-05-27T02:25:58-05:00 nil-severity vvvvv xxxx yyyy zzzz  

Desired Output:

2014-05-27T02:26:46-05:00 nil-severity zzzz uuuu vvvvv xxxx yyyy   
2014-05-27T02:26:22-05:00 nil-severity zzzz vvvvv xxxx yyyy     
2014-05-27T02:25:58-05:00 nil-severity zzzz xxxx yyyy  
2014-05-27T02:25:58-05:00 nil-severity zzzz yyyy  
2014-05-27T02:25:58-05:00 nil-severity zzzz vvvvv xxxx yyyy  

Upvotes: 1

Views: 779

Answers (5)

Etan Reisner
Etan Reisner

Reputation: 80921

Avoid messing with FS manually, just let awk do the work for you,

awk '{for (i = NF; i >= 3; i--) {$(i+1) = $i}; $3=$NF; NF--}1'

Upvotes: 2

Avinash Raj
Avinash Raj

Reputation: 174696

Try this sed command,

sed -r 's/^(.*severity) (.*) (.*)$/\1 \3 \2/g' file

Example:

$ cat aa

2014-05-27T02:26:46-05:00 nil-severity uuuu vvvvv xxxx yyyy zzzz
2014-05-27T02:26:22-05:00 nil-severity vvvvv xxxx yyyy zzzz
2014-05-27T02:25:58-05:00 nil-severity xxxx yyyy zzzz
2014-05-27T02:25:58-05:00 nil-severity yyyy zzzz
2014-05-27T02:25:58-05:00 nil-severity vvvvv xxxx yyyy zzzz

$ sed -r 's/^(.*severity) (.*) (.*)$/\1 \3 \2/g' aa

2014-05-27T02:26:46-05:00 nil-severity zzzz uuuu vvvvv xxxx yyyy
2014-05-27T02:26:22-05:00 nil-severity zzzz vvvvv xxxx yyyy
2014-05-27T02:25:58-05:00 nil-severity zzzz xxxx yyyy
2014-05-27T02:25:58-05:00 nil-severity zzzz yyyy
2014-05-27T02:25:58-05:00 nil-severity zzzz vvvvv xxxx yyyy

Explanation:

sed -r 's/^(.*severity) (.*) (.*)$/\1 \3 \2/g' file

^(.*severity) - Catches all the characters upto the word severity and stored it into a group(1).

- Matches the space that was just after to the word severity.

(.*) (.*)$ - Fetches all the characters next to the space that was after severity upto the last space and stores it to group(2). Atlast the third group contains all the characters that are just after to the last space until the end.

In the replacement part, we just rearranges the captured group according to the desired output.

Upvotes: 0

dogbane
dogbane

Reputation: 274532

A short awk command to move the last column to the third:

awk '{$3=$NF OFS $3;$NF=""}1' file

Upvotes: 4

jrjc
jrjc

Reputation: 21873

With awk

awk '{printf $1" "$2" "$NF" "; for (i=3;i<NF;i++) printf $i" "; printf "\n"}' file

2014-05-27T02:26:46-05:00 nil-severity zzzz uuuu vvvvv xxxx yyyy 
2014-05-27T02:26:22-05:00 nil-severity zzzz vvvvv xxxx yyyy 
2014-05-27T02:25:58-05:00 nil-severity zzzz xxxx yyyy 
2014-05-27T02:25:58-05:00 nil-severity zzzz yyyy 
2014-05-27T02:25:58-05:00 nil-severity zzzz vvvvv xxxx yyyy 

Hope this helps

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249093

awk '{s = ""; for (i = 3; i < NF; i++) s = s $i FS; print $1, $2, $NF, s}'

Upvotes: 1

Related Questions