sed conditional string replace for each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed conditional string replace for each line
# 1  
Old 04-14-2008
Question sed conditional string replace for each line

Hi all,
I appreciate the enormous amount of knowledge that flows in this forum.
I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have find_corp_pdm_part. I know s/search/replace/n replaces the nth occurance but I would be happy to know if I can check the condition of "find_corp_pdm_part" on the replacing line. Thanks all guys, you are all doing wonderful job.

2.<A HREF="/cgi-bin/perseus/find_pdm_part.cgi/5187-1238/54">5187-1238</A> CARD-PEN-INFO 1.0000 54 <
A HREF="/cgi-bin/perseus/bom/find_parents_in_html/5187-1238/54">Parents</A>

2.<A HREF="/cgi-bin/perseus/find_corp_pdm_part/7121-7243/00">7121-7243</A> LABEL-BLANK 3-IN-WD 3-IN-LG PP 1.0000 00
<A HREF="/cgi-bin/perseus/bom/find_parents_in_html/7121-7243/00">Parents</A>

2.<A HREF="/cgi-bin/perseus/find_corp_pdm_part/8120-6260/00">8120-6260</A> Power cord (N.A. LAR AP) 1.0000 00
# 2  
Old 04-14-2008
sed conditional string replace for each line

BTW, I have tried shell script ans loops to read line by line but that hurts by destroying the existing format (tabs replaced with one white space), please help

for FILE in $FILES
do
{
while read LINE
do
echo $LINE | grep "find_corp_pdm_part"
if [ $? -eq 0 ]
then
echo $LINE | sed 's#/00">#">#' >> temp
else
# [ "$LINE" = "*" ] && { echo "*" >> $FILE.new$$ ; continue ; }
echo $LINE >> temp
fi
# set -- $LINE
done
} < $FILE
mv temp $FILE
done
# 3  
Old 04-14-2008
Try this sed command :
Code:
sed '/find_corp_pdm_part/s_/00__' inputfile

Modify your while loop (this will preserve spaces and tabs) :
Code:
while IFS= read LINE

Jean-Pierre.
# 4  
Old 04-14-2008
Perl is a good (direct) option:
Code:
perl -pi -e "s/\/00//&&/find_corp_pdm_part/" file/s

# 5  
Old 04-14-2008
The sed and the perl solution won't work if you have double zero's after the 4th slash.

Try this:

Code:
awk 'BEGIN{OFS=FS="/"}$4=="find_corp_pdm_part"{sub("^00","",$6)}1' file

Regards
# 6  
Old 04-14-2008
Quote:
Originally Posted by Franklin52
The sed and the perl solution won't work if you have double zero's after the 4th slash.

Try this:

Code:
awk 'BEGIN{OFS=FS="/"}$4=="find_corp_pdm_part"{sub("^00","",$6)}1' file

Regards
Just add the quote in the pattern:
Code:
perl -pi -e "s/\/00\"//&&/find_corp_pdm_part/" file/s

Regds
# 7  
Old 04-14-2008
MySQL sed conditional string replace for each line

Like I said before, you people are great.

I got away with my problem by using the very first solution by Aigles. I believe all the other suggestions work too. I never posted queries in forum but the very first one was overwhelming. I promise to use it judiciously and also try to help others to the best of my knowledge. This thread is closed unless there is really anything fascinating.

Thanks guys, keep up the good work!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed conditional \n replace for each line

How could be removed \n only if appearing at position 80 in the line? (4 Replies)
Discussion started by: RomanF
4 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

4. Shell Programming and Scripting

sed or awk to replace a value in a certain line containing a string

hi experts , I have an input like following. R sfst 1000.0000 $ new time step for mass scaled calculation R dt2ms -4.000E-7 $ friction value for blank R mue ... (10 Replies)
Discussion started by: hamnsan
10 Replies

5. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

6. Shell Programming and Scripting

sed: how to replace string by another line

hi experts, i would like to do some html-file replacements and encounter the following problem: after cleaning up the file a bit i'd like to replace a regex with a reference and the content of another line. example: /<title>\(*\) - Wikipedia<\/title>/ should be replaced by \1 (the title... (2 Replies)
Discussion started by: chrisnie
2 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

Sed conditional replace

Given this row: |lastname1|middlename1|firstname1|lastname2|middlename2|firstname2 produce this result: |lastname|middlename|firstname where the resultant names are based on the presence of the #2 names above. I.e., if a #2 name is passed (usually will be null,) use that - otherwise... (8 Replies)
Discussion started by: tiggyboo
8 Replies

9. Shell Programming and Scripting

HELP Need in SED/PERL conditional line replacement

Hi , I need some help on perl/sed conditional replacement The situation is like below . I have a file contents like below . AAA|BBB|CCC|DDD AAA|BCF|CCC|HHH AAA|BVF|JJJ|KKK Here in the above file . I know my second column value (taking "|" as my delimited ) Basically I have to... (3 Replies)
Discussion started by: robin.r888
3 Replies

10. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies
Login or Register to Ask a Question