How to retain blank spaces in AWK?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retain blank spaces in AWK?
# 1  
Old 02-05-2011
How to retain blank spaces in AWK?

Hi all,

I have space delimated file which look like this
Code:
 
1    2    3    4 
5    6    7    8
9       1 0   11

I am using simple awk command to read the second column
Code:
 
awk '{print $2}' input_file

but i got the output like this which also read 10 from the third column

Code:
 
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
# 2  
Old 02-05-2011
We need some more info,
could you post the output of the following
command:

Code:
head -3 infile|od -cb

# 3  
Old 02-05-2011
Quote:
Originally Posted by bsn2011
Hi all,

I have space delimated file which look like this
Code:
 
1    2    3    4 
5    6    7    8
9       1 0   11

I am using simple awk command to read the second column
Code:
 
awk '{print $2}' input_file

but i got the output like this which also read 10 from the third column

Code:
 
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:
Code:
awk '{print substr($0,4,3)}'

# 4  
Old 02-05-2011
if you know how many spaces in between these columns, you can set -F or FS to that number of spaces.
# 5  
Old 02-06-2011
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
Code:
 
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.

Code:
 
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
# 6  
Old 02-06-2011
As already stated and given your input file, you could get the data for Feb with the following command:

Code:
awk -F'    ' '{print $5}' infile.txt

There are four white space characters between -F' and '.
# 7  
Old 02-06-2011
Thanks alot. my problem is solved using 4 spaces with -F option.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

2. UNIX for Dummies Questions & Answers

Remove blank spaces

Dear Masters, I want to remove all lines with blank spaces input file: a|abc|0|1 a|abc|2|3 b||3|5 c|def||7 d|def|0|1 Expected: a|abc|0|1 a|abc|2|3 d|def|0|1 I did this awk -F'|' '!/^$/' input (4 Replies)
Discussion started by: radius
4 Replies

3. Shell Programming and Scripting

Remove blank spaces

Gents, Please can you help me.. to remove blank spaces :) Input ABSOLUTE , ,FALSE ,1035 ,28 ,669 ,1817.0 ,CORREL BEFORE ,1 ABSOLUTE , ,FALSE ,1035 ,28 ,686 ,1817.0 ,CORREL BEFORE ,1 ABSOLUTE , ,FALSE ,1035 ,28 ,670 ,1819.0 ,CORREL BEFORE ,1 ABSOLUTE , ,FALSE ... (4 Replies)
Discussion started by: jiam912
4 Replies

4. Shell Programming and Scripting

Problems with Blank Spaces

Hi to all. How can I pass to the stat command a file path with blank spaces? And another question, if I use stat command like this: stat / -name "*.sh" -user $user_name -exec stat -c %n%x {} \; How can I get the result with a ":" into the name of the file and the time of the last... (4 Replies)
Discussion started by: daniel.gbaena
4 Replies

5. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. Shell Programming and Scripting

Handling blank spaces

Hi, I am trying to replace a specific column values in a csv file with double quotes when I am find embedded spaces with in the fields. Example: SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS 1,Robert,74538,12,34, M,Robert Street, NY 2,Sam,07564,13,M,12 Main Ave, CA 3,Kim, Ed,12345,14,M,123D ,... (1 Reply)
Discussion started by: techmoris
1 Replies

7. Shell Programming and Scripting

how to remove blank spaces of a file with awk??

hello how to remove blank spaces of a file with awk?? i´m trying awk '{gsub(" ","",$0); print $0;}' filename.txt but it answers syntax error near line first of all i did this for download from netbackup database jobs privilege bpdbjobs -report -M sv88 -gdm -header |cut -c-1024... (4 Replies)
Discussion started by: pabloli150
4 Replies

8. UNIX for Dummies Questions & Answers

string with blank spaces

I have a file that has dates like this: date FINAL_RESULT; 7 date FINAL_RESULT; 2 date FINAL_RESULT; 5 With this command: seira=`cut -f2 -d\; tes.txt` i take the date FINAL RESULTs and i store them on variable seira.then seira look like this: 6 3 8 I want to read seira and make a sum of all... (4 Replies)
Discussion started by: psalas
4 Replies

9. Shell Programming and Scripting

blank spaces getting deleted

I have to filter out data from a file based on the value of first three characters of each record I have used the following logic FIN=$LOC/TEST2.TXT FEEDFILE=$LOC/TEST1.TXT while read FDROW do FEEDROW=$FDROW; DTYPE=`echo $FEEDROW |cut -c 1-3` if ; then echo $FEEDROW >> $FIN... (5 Replies)
Discussion started by: gander_ss
5 Replies

10. Shell Programming and Scripting

howtodelete blank spaces

sry im new to this...another qns.. if i have a line in the normal shell the file is email.scp. it contains a txt file eg hello.txt that contain this infomation. k@hotmail.com j@jotm.com how do i delete away the blank spaces after the k@hotmail.com..i dun wan any blank spaces after the... (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question