Reputation:
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.
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
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
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
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
Reputation: 274532
A short awk
command to move the last column to the third:
awk '{$3=$NF OFS $3;$NF=""}1' file
Upvotes: 4
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
Reputation: 249093
awk '{s = ""; for (i = 3; i < NF; i++) s = s $i FS; print $1, $2, $NF, s}'
Upvotes: 1