Find and replace blank in the last field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace blank in the last field
# 8  
Old 11-19-2010
@dragon

sed '/^$/d' infile will delete empty lines, but not whitespace-only lines..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 11-22-2010
Data It's not working for my new file!

Hi all,

I am trying to insert missing value (99999) in the blanks which are always in the last field.
Could someone please kindly tell me why this code
Code:
awk -F\,  '{if($17==""){$17=99999};print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17}' input file

which is working flawlessly with my previous file (sample in the first page) is not working for this new file?

The original file with blanks:
Code:
110050 99999 99999 1986 02 26 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999
110050 99999 99999 1986 02 27 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999
110050 99999 99999 1986 02 28 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999
110050 99999 99999 1986 03 1 99999 99999 99999 99999 99999 99999 0 99999 99999 99999
110050 99999 99999 1986 03 2 99999 99999 99999 99999 99999 99999 0 99999 99999 99999
110050 99999 99999 1986 03 3 99999 99999 99999 99999 99999 99999 0 99999 99999 99999
110050 99999 99999 1986 03 4 99999 99999 99999 99999 99999 99999 0 99999 99999 99999
110050 99999 99999 1986 03 5 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999
110050 99999 99999 1986 03 6 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999
110050 99999 99999 1986 03 7 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999

After using the code, 99999 is inserting at the end of all lines Smilie
Code:
110050 99999 99999 1986 02 26 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999                99999
110050 99999 99999 1986 02 27 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999                99999
110050 99999 99999 1986 02 28 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999                99999
110050 99999 99999 1986 03 1 99999 99999 99999 99999 99999 99999 0 99999 99999 99999                  99999
110050 99999 99999 1986 03 2 99999 99999 99999 99999 99999 99999 0 99999 99999 99999                  99999
110050 99999 99999 1986 03 3 99999 99999 99999 99999 99999 99999 0 99999 99999 99999                  99999
110050 99999 99999 1986 03 4 99999 99999 99999 99999 99999 99999 0 99999 99999 99999                  99999
110050 99999 99999 1986 03 5 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999                99999
110050 99999 99999 1986 03 6 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999                99999
110050 99999 99999 1986 03 7 99999 99999 99999 99999 99999 99999 0 99999 9999.9 99999 99999                99999

Thanks very much for your time and help in advance.
# 10  
Old 11-22-2010
Verify your input file format. First post fields are separated by ","

so you can use
Code:
 awk  '{if($17==""){$17=99999};print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 11  
Old 11-22-2010
Code:
awk 'NF<17{$0=$0 FS 99999}1' infile

This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 11-22-2010
Thank you very much pravin27 and Scrutinizer. I learned from you Smilie

I like this one
Code:
awk 'NF<17{$0=$0 FS 99999}1' infile

# 13  
Old 11-22-2010
Code:
awk 'NF<17{$(NF+1)=99999}1' infile

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find max length of the field and then replace zero

hai guys, pick the 1st field and calculate max length. if suppose max length is 2, then compare the all records if <2 then add zero's prefix of the record. for ex: s.no,sname 1,djud 37,jtuhe in this max length of the 1st field is 2 right the output wil be s.no,sname 01,djud... (6 Replies)
Discussion started by: Suneelbabu.etl
6 Replies

2. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

3. Shell Programming and Scripting

To find char field and replace null

hi, i having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field for eg: file1.txt xx|yy|xx|12 output file: xx|yy||12 (5 Replies)
Discussion started by: rohit_shinez
5 Replies

4. Shell Programming and Scripting

Find a blank field

Find a blank field Hi I have set of fields that have some blank values, how to find that and get its line noumbers in output file. Ex: Col1 col2 col3 11 ss 103 12 104 13 105 14 se 106 (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

5. Shell Programming and Scripting

replace blank field in file 2 with content of file 1

Something like vlookup in excel, column 2 in file 2 is blank and should be replaced by column 2 in file 1 based on comparing column 1 in both files. file1 Code: 1234~abc~b~c~d~e~f~g~h~09/10/09 5678~def~b~c~d~e~f~g~h~12/06/10 8910~hij~b~c~d~e~f~g~h~03/28/13... (1 Reply)
Discussion started by: sigh2010
1 Replies

6. Shell Programming and Scripting

Find field count and replace

Hello All, I have a file with contents like apple|ball|charlie|David| England|France|Germany| Ireland|Japan|King|London| Man|Nancy|Orange| here the column delimiter is | so if any of the lines/rows in the file has 3 only records (last field is empty), i want to place a | at the end of... (4 Replies)
Discussion started by: vinredmac
4 Replies

7. UNIX for Dummies Questions & Answers

Find and replace a field in the last line

I have a file 'test.out' with contents: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| 4|1|10|10|I|hgfj| 34|0|10|10|I|sdg| I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

8. Shell Programming and Scripting

awk: find and replace in certain field only, help needed

I got a sample file like this. $ cat test 12|13|100|s 12|13|100|s 100|13|100|s 12|13|100|s I want to replace all 100 by 2000 only in 3rd field using "awk" This is replacing all 100's :-( $ awk -F "|" '{gsub( /100/,"2000");print}' test 12|13|2000|s 12|13|2000|s 2000|13|2000|s... (5 Replies)
Discussion started by: jkl_jkl
5 Replies

9. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies
Login or Register to Ask a Question