search a replace each line- help needed ASAP


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users search a replace each line- help needed ASAP
# 1  
Old 05-22-2008
search a replace each line- help needed ASAP

can someone help me with the find and replace command.

I have a input file which is in the below format:

0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000008EGYPT 000000000000199800000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000006EGYPT 000000000000199800000
0011200ALN00000000007EGYPT 000000000000199800000

I need to replace anything that has 9” in column 21 and ‘1999' in column 42to be changed to have ‘1900'. It should keep the rest of the file that does not match this criteria intact.

output of this change needs to be like this:

0011200ALN00000000009EGYPT 000000000000190000000
0011200ALN00000000009EGYPT 000000000000190000000
0011200ALN00000000008EGYPT 000000000000199800000
0011200ALN00000000009EGYPT 000000000000190000000
0011200ALN00000000006EGYPT 000000000000199800000
0011200ALN00000000007EGYPT 000000000000199800000

Can anyone help me with this?

i will be very helpful.

Thanks,
sandeep
# 2  
Old 05-22-2008
Try...
Code:
awk '{print substr($0,1,41) (substr($0,42,4)=="1999"?"1900":substr($0,42,4)) substr($0,46)}' file1 > file2

# 3  
Old 05-22-2008
Hi,


0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000008EGYPT 000000000000199800000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000006EGYPT 000000000000199800000
0011200ALN00000000007EGYPT 000000000000199800000


I got the output same as the input with no chnages.

Thanks,
Sandeep
# 4  
Old 05-22-2008
Hello Sandeep,
I would suggest that you replace the complete string "000000000000199900000", if it is not a problem.

The command to use would be within vi and something like this
%s/old_word/new_word /g [this is within "vi". Command used to replace text (occurring any no. of times in the line) in the whole file (ignore /g if only first occurance in the line is to be replaced]

But be sure to make a copy of the original file before trying out this command.

Let me know how it goes.

Regards
Bimal
# 5  
Old 05-22-2008
Hi Bimal,

Thanks for looking into this.

I need to schedule this job and hence doing it from Vi editor will be manual. Hence i will not be using that, and the records that i have pasted here are just some sample records, so replacing the complete string "000000000000199900000" will be a problem.

Thanks again

Sandeep
# 6  
Old 05-22-2008
You gave a wrong position for the year so the solution of Ygor doesn't work. Try this one:

Code:
awk 'substr($0,21,1)==9{print $1,substr($2,1,12)"1900"substr($2,17);next}1' file

Regards
# 7  
Old 05-22-2008
thanks so much

can you please explain this for me.. i am new to awk,

Thanks,
Sandeep
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Search for a pattern and replace. Help needed

I have three variables $a, $b and $c $a = file_abc_123.txt $b = 123 $c = 100 I want to search if $b is present in $a. If it is present, then i want to replace that portion by $c. Here $b = 123 is present in "file_abc_123.txt", so i need the output as "file_abc_100.txt' How can this be... (3 Replies)
Discussion started by: irudayaraj
3 Replies

3. 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

4. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

5. UNIX for Advanced & Expert Users

Search Parameter in first line and replace next line content

Hi, I need help. I have XML file as below &lt;a n=&quot;infoLevel&quot;&gt; &lt;v s=&quot;true&quot;/&gt; &lt;/a&gt; &lt;a n=&quot;localAddr&quot;&gt; &lt;v s=&quot;server.host.com&quot;/&gt; &lt;/a&gt; &lt;a n=&quot;ListenPort&quot;&gt; &lt;v s=&quot;21111&quot;/&gt; &lt;/a&gt; I need to find variable "ListenPort" in line and then replace... (4 Replies)
Discussion started by: rdtrivedi
4 Replies

6. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

7. Shell Programming and Scripting

KERBEROS_V4 -help needed ASAP

#!/bin/sh HOST='ftp.bend.com' USER='temp1' PASSWD='temp2' FTPPATH='SY1:' ifile='concat.txt' #FTP concatenated file ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD binary cd $FTPPATH put $ifile get $ifile retrieval.$$ quit END_SCRIPT if then echo "FTP of... (2 Replies)
Discussion started by: Sgiri1
2 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question