Tuesday, 2 April 2013

How to print current line and next line at once, using "awk" ?

A bit of what I call "awk magic" :-)

I have a simple text file, like this:


$cat my_file.txt

1
2
3
4
5
6

 My desire output is like this:

1 2
3 4
5 6

Solution:


awk '{print $0 p; p=" "$0}' my_file.txt|awk -F" " '{print $2"  "$1}'
  1
1  2
2  3
3  4
4  5
5  6



We need to get rid of the 1st line ; the 2nd awk was needed to reverse the order of the 2 elements returned by awk utility.

This may be helpful sometimes, when you have a list of numbers and need to prepare like a script, which will take as parameters ranges.




No comments:

Post a Comment