Hi all,
I have space delimated file which look like this
1 2 3 4
5 6 7 8
9 1 0 11
I am using simple awk command to read the second column
awk '{print $2}' input_file
but i got the output like this which also read 10 from the third column
2
6
10
i want to retain the blank space in the second column, but i dont know how to do this in awk
please help me
Thanks in advance
We need some more info,
could you post the output of the following
command:
head -3 infile|od -cb
bsn2011:
Hi all,
I have space delimated file which look like this
1 2 3 4
5 6 7 8
9 1 0 11
I am using simple awk command to read the second column
awk '{print $2}' input_file
but i got the output like this which also read 10 from the third column
2
6
10
i want to retain the blank space in the second column, but i dont know how to do this in awk
10 is in the second column. Any number of consecutive spaces form a single delimiter. You want to extract by character position:
awk '{print substr($0,4,3)}'
if you know how many spaces in between these columns, you can set -F or FS to that number of spaces.
Hi all,
thanks for your replies. In my previous message i actually used some data to explain my problem but i did not actually posted the original data that i am having problem with it. I have attached the origional file to this message. in this file i have monthly temperature data with 13 columns where ist colums is daya and other 12 cols are months. the problem i am having is that this file is not comma delimited file and for the months with 28 or 30 days, i have blank spaces at the end. if I use this command to read the file
awk -F" " '{$1=$1;print}' ASTMAX91.TXT
then i got the following output where the position of the data is disturbed after 28 days due to these spaces between columns.
JAN FEB MAR APR MAY JUN JULY AUG SEP OCT NOV DEC
DATE -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- ------------
1 3.4 5.1 5.6 14.1 21.2 20.9 26.7 29.2 26.7 19.2 13.7 8.4
2 1.2 2.8 3.8 13.9 11.4 22.9 25.6 27.8 25.9 18.9 13.9 6.7
3 -2.2 3.4 1.2 14.1 12.0 24.5 28.4 25.6 28.0 21.2 14.5 7.3
4 -3.3 3.9 7.6 15.4 15.3 26.2 25.1 28.4 28.8 22.0 14.6 4.7
5 -1.1 0.3 2.4 15.8 12.9 26.2 23.9 20.8 29.6 22.3 15.6 6.7
6 0.1 0.6 3.1 16.9 13.5 27.0 25.8 28.4 29.0 19.5 15.1 10.1
7 0.3 0.8 2.5 16.8 18.2 28.4 18.4 29.2 30.2 21.2 15.1 7.6
8 -1.9 2.3 6.9 12.9 17.3 26.9 17.8 31.2 26.2 17.8 15.6 4.5
9 4.4 4.4 5.2 8.4 15.2 29.0 26.2 27.8 26.9 14.5 13.4 4.8
10 0.1 4.5 6.9 10.1 13.9 28.2 25.9 21.2 28.0 19.5 13.4 7.8
11 -0.5 3.3 5.6 12.6 17.8 26.2 28.4 15.9 28.5 19.5 12.3 7.8
12 -3.8 3.4 3.7 13.4 18.6 21.3 29.3 23.9 29.1 16.4 12.8 7.8
13 1.3 4.2 3.9 13.7 20.3 27.6 25.1 26.6 29.0 15.6 11.2 3.8
14 -3.3 1.1 7.4 12.6 20.3 27.3 18.9 28.7 30.3 15.1 12.8 6.3
15 -3.3 1.8 5.3 10.6 20.4 29.0 18.9 30.6 28.9 17.3 10.1 8.1
16 0.1 2.1 6.3 5.2 20.6 29.2 24.5 30.3 27.3 13.7 10.3 6.9
17 -2.7 -0.4 10.3 8.7 18.1 28.3 27.3 28.9 27.3 12.3 10.1 9.6
18 2.3 1.4 7.9 10.4 22.0 24.7 30.1 29.1 17.8 15.2 8.9 12.3
19 0.3 3.1 2.0 11.7 22.3 22.8 28.2 29.5 18.9 16.8 10.6 10.2
20 3.4 3.4 8.1 14.2 18.5 15.1 28.6 31.3 17.8 17.0 9.2 8.7
21 0.1 5.6 9.5 16.2 14.5 18.9 30.1 28.4 19.6 16.7 11.2 5.3
22 2.8 6.7 11.0 15.6 12.3 22.2 25.3 23.7 21.7 15.1 13.4 2.6
23 0.6 8.1 6.7 10.6 17.0 21.7 19.5 20.8 21.7 18.1 12.3 5.6
24 0.1 8.4 10.6 11.7 15.2 24.1 24.5 23.4 22.6 18.1 10.1 2.9
25 1.7 6.7 8.9 17.4 16.9 24.5 26.7 26.2 23.6 15.6 10.9 1.7
26 1.7 4.5 9.9 17.3 7.7 23.7 19.5 27.0 22.8 15.6 8.9 1.2
27 2.8 6.8 13.4 17.9 14.1 25.1 25.1 28.1 18.9 18.7 7.3 5.1
28 6.3 6.4 12.3 17.4 19.1 25.7 28.1 29.0 15.4 15.6 4.7 1.2
29 5.6 14.3 19.6 19.2 27.3 28.4 29.0 17.8 13.9 7.8 2.8
30 7.6 15.2 20.3 19.9 27.0 29.5 25.1 18.9 13.9 8.4 2.6
31 3.9 16.4 19.1 30.1 24.5 14.7 2.3
i really appreciate your help. i have 100 of file to process.
Thanks
As already stated and given your input file, you could get the data for Feb with the following command:
awk -F' ' '{print $5}' infile.txt
There are four white space characters between -F' and '.
bsn2011
February 6, 2011, 12:36pm
7
Thanks alot. my problem is solved using 4 spaces with -F option.
not really, if the number is bigger, such as -45630.4, you will lost it.
So cfajohnson's solution will be more suitable for you.
awk '{print substr($0,20,10)}' infile
or by cut
cut -c 20-30 < infile
Ygor
February 6, 2011, 9:01pm
9
If using gawk, you could use FIELDWIDTHS instead of FS, e.g...
gawk '$1=$1' FIELDWIDTHS='12 9 9 9 9 9 9 9 9 9 9 9 9' OFS=',' infile.txt