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
# 8  
Old 05-22-2008
Sure,

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

substr($0,21,1)==9 -> if postition 21 of the line = 9 then do everything between the braces

print $1
-> print first field (before the space)

substr($2,1,12)
-> print position 1 to 12 of second field (after space)

"1900"substr($2,17);next}
-> print "1900" and position 17 of second field untill the end of the string and read the next line

1
-> print other lines

Go here for some tutorials:

https://www.unix.com/answers-frequent...tutorials.html


Regards
# 9  
Old 05-22-2008
thanks so muchSmilie
# 10  
Old 05-26-2008
I had one last question on the above requirement. I tried a few changes in the awk code but did not work.


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

this will replace anything that has "9” in column 21 with ‘1900' in column 42.

what if i need to replace only records that has "9” in column 21 AND '1999' in column 42 ( matches both this criteria ) with ‘1900' in column 42.

Please advise

Thanks,
Sandeep
# 11  
Old 05-26-2008
Code:
awk 'substr($0,21,1)==9 && substr($0,42,4)==1999{print $1,substr($2,1,12)"1900"substr($2,17);next}1' file

Regards
# 12  
Old 05-26-2008
Thanks so much
# 13  
Old 05-28-2008
Hi,

In my prevoius example file there was only one small space

example:

0011200ALN00000000009EGYPT 000000000000199900000

what If my file has spaces like the below file:

0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000009EGYPT 000000000000000000000
146920000100000000009DE SOTO 000000000001999000000

Can't there be a generic script written for this

Please advise?

Thanks,
Sandeep

Last edited by bsandeep_80; 05-28-2008 at 11:49 AM..
# 14  
Old 05-29-2008
Just operate on $0 all the time. $0 is the entire input line in awk, $1 $2 $3 etc is split into individual fields (on whitespace by default).

Code:
awk 'substr($0,21,1)==9 && substr($0,42,4)==1999{
  print substr($0,0,41) "1900" substr($0, 46); next }
1'

The offsets in your latest example don't match what was posted before; do you have multiple adjacent spaces there? Please use code tags when posting material where spaces are significant.

The vi tip also works; you can use sed instead of vi, although the regular expressions would be getting rather clumsy.
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