4rlekin
4rlekin

Reputation: 758

Either grep or AWK are ereasing the content of processed stream

Lets say i have a file like this:

ASI BEK BKG COE
0.00112000003 0.00003000000 -0.00001000000 0.00000000000
0.00170999998 -0.00009000000 -0.00008000000 0.00052000000
0.00089000002 -0.00003000000 -0.00028000001 0.00068000000
0.00031000000 0.00003000000 -0.00026000000 0.00057999999
0.00239000004 -0.00003000000 0.00004000000 0.00076999998
0.00000000000 0.00002000000 -0.00039000000 0.00050000002
0.00401999988 -0.00014000000 -0.00029000000 0.00046000001
0.00179999997 -0.00011000000 -0.00025000001 0.00044000000
0.00025000001 -0.00008000000 0.00004000000 0.00063000002

(Obviously it is larger, with longer records - but this sample is quite enough to understand the structure)

I want to simply use only digit-starting records (omit the title). So i obviously do it by grep ^[0-9]. BUT ! The output is perfectly nothing. Because I need to use the file in general by columns I also use AWK. And here is next odd thing. When I tried it like cat file | grep ^[0-9] | awk '{ print }' it gaves me nothing. But when I set explicit column number in AWK (like awk '{ print $1,$2...<and_so_on>}' it works. I'd like to avoid using explicit column numbers since I don't understand what is wrong with this grep and also it is not the most beautiful solution.

Thanks in advance for any help. I hope it is simply such silly mistake made by me.

Upvotes: 0

Views: 90

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85775

It can be dangerous not to use quoting with the shell. Some shells such as csh won't do what you think unless you quote the pattern ^[0-9]. You should use single quotes here to ensure nothing is interpreted:

$ cat file | grep '^[0-9]' | awk '{print $0}'
0.00112000003 0.00003000000 -0.00001000000 0.00000000000
0.00170999998 -0.00009000000 -0.00008000000 0.00052000000
0.00089000002 -0.00003000000 -0.00028000001 0.00068000000
0.00031000000 0.00003000000 -0.00026000000 0.00057999999
0.00239000004 -0.00003000000 0.00004000000 0.00076999998
0.00000000000 0.00002000000 -0.00039000000 0.00050000002
0.00401999988 -0.00014000000 -0.00029000000 0.00046000001
0.00179999997 -0.00011000000 -0.00025000001 0.00044000000
0.00025000001 -0.00008000000 0.00004000000 0.00063000002

The cat and the grep are redundant here if you are going to use awk. awk can do patterning matching and read files all by itself:

$ awk '/^[0-9]/{print $0}' file

The default block in awk is {print $0} so we can also drop that:

$ awk '/^[0-9]/' file

As you only want to skip over the first line in the file a better solution would be:

$ awk 'NR>1' file

Failing that it will most likely be a hidden character issues such a line endings. Try dos2unix file and see if that does the trick or inspect the file with a hex editor.

Upvotes: 3

Related Questions