Replace empty string on particular column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace empty string on particular column
# 1  
Old 08-03-2011
Replace empty string on particular column

Hi
I would like to replace empty string with a particluar value, any suggessions with awk ?

my input file is not delimited with any delimiters

input
Code:
 
52001073M8000000004567777
5200107     000000004567778
5200107     000000004567779
52001073M8000000004567789

Expected output
output
52001073M8000000004567777
52001073M8000000004567778
52001073M8000000004567779
52001073M8000000004567789

any tips suggessions pls (i wanted with awk since my input file size will be very big runs around 10 MB)
# 2  
Old 08-03-2011
Can it be Perl? It can handle 10MB files just as easy as AWK:
Code:
perl -pe 's/\s+//' infile > outfile

# 3  
Old 08-03-2011
Hi,

Try next command:
Code:
$ awk '{ if ( NF == 2 ) { $0 = $1 "3M8" $2; }; print }' infile
52001073M8000000004567777
52001073M8000000004567778
52001073M8000000004567779
52001073M8000000004567789

Regards,
Birei
# 4  
Old 08-03-2011
replace

awk '{ gsub( / /,"3M8");print }' yourfilename

Last edited by a20786; 08-03-2011 at 01:59 PM..
# 5  
Old 08-03-2011
Code:
awk '{print (NF>1?$1"3M8"$2:$0)}' file

This User Gave Thanks to shamrock For This Post:
# 6  
Old 08-04-2011
Shamrock

Code:
awk '{print (NF>1?$1"3M8"$2:$0)}' file

can you explain me the above piece of code ..

Actually i am encountering few other hiccups when i try to update the blank records

input file

Code:
5201007   93M999999 999999 
52010 7781     878788887878
520100 7M2    999993336630

Basically there will be spaces any were in the input file, but i will have to check(for empty string -" ") and replace the only from a particular character to a particular...

TO put it more clearly
how can i use awk if input fields are not delimited at all and replace empty string( with value)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

2. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

3. Shell Programming and Scripting

Replace pipe delimited column string to null

Hi All, I have a large dat file where each lines are pipe delimited values. I need to parse the file depending on the request. For example: sometimes I have told to remove all the values in the 7th column (this case remove values '3333' only from the first line and '3543' from the second line)... (4 Replies)
Discussion started by: express14
4 Replies

4. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

5. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

6. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

7. UNIX for Dummies Questions & Answers

Search and replace string only in a particular column in a delimited file

I have file with multiple columns. Column values for a record may be same. Now i have to replace a column value(this can be same for the other columns) with new value. File.txt A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D... (1 Reply)
Discussion started by: ksailesh
1 Replies

8. UNIX and Linux Applications

Replace string in unix from 10th column onwards

Hi All, I need to replace the last 19 bytes of the following string My_Org_Testing_20090102_231124.txt (Text_Date_Time.txt). I would like to derive the current time using "date +%Y%m%d_%H%M%S.txt" and replace the last 19 bytes of the above string I would appreciate if someone could... (3 Replies)
Discussion started by: rpk2008
3 Replies

9. UNIX for Advanced & Expert Users

Replace string in column

Hi, I want to replace string in column,Example i have file caleed a1.txt ,want to replace string "A12" with "A23" only in column2 ,not from file itself.Using sed command replace string in file itself. Thanks, Mohan (3 Replies)
Discussion started by: mohan705
3 Replies
Login or Register to Ask a Question