Alex Rivera | Logout

Why is awk not printing out newlines?

Asked 2012-02-28T22:44:52.733
9

I have a file which looks like this:

1 
2
AA 
4
5
AA BB
7
8
AA BB CC
10
11
AA BB CC DD

I am using awk to extract only every nth line where n=3.

>>awk 'NR%3==0' /input/file_foo >> output/file_foobar

The output is appearing in a single line as:

AA AA BB AA BB CC AA BB CC DD

.....and so on

I want it to appear as:

AA 
AA BB 
AA BB CC 
AA BB CC DD 

I tried using \n, printf with \n, and so on but it doesn't work as I expect. Please advise.

awk
Edit
Report

1 Answer

4

I think the problem is in the way you're showing the data, not in the processing.

$ cat x
1 
2
AA 
4
5
AA BB
7
8
AA BB CC
10
11
AA BB CC DD
$ awk 'NR%3==0' x
AA 
AA BB
AA BB CC
AA BB CC DD
$

I suspect that what you're doing is similar to:

$ awk 'NR%3==0' x > y
$ x=$(<y)
$ echo $x
AA AA BB AA BB CC AA BB CC DD
$ echo "$x"
AA 
AA BB
AA BB CC
AA BB CC DD
$

This would confuse you. See also: Capturing multi-line output to a bash variable.

answered 2012-02-28T23:08:08.963

Your Answer