gawk script to search and replace text in a flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gawk script to search and replace text in a flat file
# 1  
Old 04-01-2012
gawk script to search and replace text in a flat file

Hi

I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file.

Input file text :
Code:
ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 
ZIDE_RECORD000 1000000000038699209000004000000026307257711 6307257711SAP20120321164053676 
ZIDE_STGPREMISE000 1000000000038699209000005000004036307257711 DATABUILDING00110 02311 XXXXXX DATABUILDING00110 00110 A 
ZIDE_STGSDP000 1000000000038699209000006000004036307257711-1 6307257711-1 A O Active 6307257711 


What I want to achieve is to replace the text that is in BOLD with a different text and a new file to be created

Thanks in advance

Last edited by Franklin52; 04-02-2012 at 03:44 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 04-01-2012
What do you want to replace that text with...
# 3  
Old 04-01-2012
oops sorry missed it
want to replace the text with a different values say 666666666
once I replace the text, I want to save the file with a new name

Thanks
# 4  
Old 04-01-2012
Code:
gawk '{gsub(/6307257711/,666666666);print}' infile > outfile

# 5  
Old 04-01-2012
Code:
sed 's/6307257711/666666666/g' infile > outfile

# 6  
Old 04-02-2012
So you do not want replacements in the first two columns, is that correct?
Code:
awk '{for(i=3;i<=NF;i++)gsub(from,to,$i)}1' from=6307257711 to=666666666 infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search field in text file and replace value

Hi there, First of all this is my first post here. Thank you in advance for your help. What I am trying to do is the following. I have a text file where each field of each row is separated by a tabulator. Looks like this: ATOM 1 N HSE A 26 3.033 -10.429 -2.262 1.00 17.07 ... (8 Replies)
Discussion started by: doom4
8 Replies

2. Shell Programming and Scripting

Gawk - Text replace - need help

Hello, i am a working student and have to build a patch setup. I have to replace 2 lines in a .txt file. Can someone help me please. Now: wrapper.java.mainclass=org.tanukisoftware.wrapper. WrapperSimpleApp wrapper.java.classpath.2=../../jdk/lib/tools.jar replace with:... (12 Replies)
Discussion started by: Baefisch
12 Replies

3. Emergency UNIX and Linux Support

Search and replace in text file

Hi, I have gigabytes of text files that I need to search for "&" and replace with "&amp". Is there a way to do this efficiently (like sed command)? Hope you could help. Thanks. (17 Replies)
Discussion started by: daytripper1021
17 Replies

4. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

5. Shell Programming and Scripting

text file search and replace with awk

hello all greeting for the day i have a text file as the following text.xml abcd<FIELD>123.456</FIELD>efgh i need to replace the value between <FIELD> and </FIELD> by using awk command. please throw some light on this. thank you very very much Erik (5 Replies)
Discussion started by: erikshek
5 Replies

6. Shell Programming and Scripting

search and replace a text in a file

Hi all, I have a requirement where i have to search data between strings 'SELECT' and ';' and replace this text as "SELECT.....;" so that i can export this extracted string into a excel cell. Please suggest on this. (5 Replies)
Discussion started by: goutam_igate
5 Replies

7. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

8. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

9. Shell Programming and Scripting

How to search and replace text in same file

script is as below v_process_run=5 typeset -i p_cnt=0 pdata=/home/proc_data.log while do # execute script in background dummy_test.sh "a1" "a2" & p_cnt=$p_cnt+1 echo "data : $p_cnt : Y" >> $pdata done file created with following data in... (1 Reply)
Discussion started by: Vrgurav
1 Replies

10. Shell Programming and Scripting

automating file search and replace text

Hi, I am trying something like this: Let's say I have a file called File1 with contents: x=-0.3 y=2.1 z=9.0 I have another file, File2, with contents: xx= yy= zz= (nothing after "="). What I want to do is get the value of x in File1 and set it to xx in File2, i.e., xx=-0.3. And the... (3 Replies)
Discussion started by: ommatidia
3 Replies
Login or Register to Ask a Question