The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 4 06-18-2009 03:36 PM
search for a string ,replace the whole line with new line kkraja Shell Programming and Scripting 4 08-07-2008 03:26 AM
How to replace a line ukatru Shell Programming and Scripting 2 08-04-2008 03:51 PM
How to replace one line with three in sed ch0sen Shell Programming and Scripting 6 03-05-2008 12:08 PM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 07:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-01-2008
unknown123 unknown123 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Red face Replace line with Nothing

Hi,

i want to replace some lines in a file "Source.txt" by taking search pattern input from a different file named "abc".

Search pattern input file have following lines:

vi abc
2637918
2707239
2709124
2709148
2709159
2709177
...........

i am running following code:

for i in `cat abc` ;do perl -pi.bk4 -e 's/^.*$i.*//g' Source.txt;done

but it's not working.......
pls help.
  #2 (permalink)  
Old 09-01-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,407
while read line
do
sed -i "s/^.*$line.*//g" source.txt //or// sed -i 's/^.*${line}.*//g'
done < abc

or try

Last edited by vidyadhar85; 09-01-2008 at 05:15 AM..
  #3 (permalink)  
Old 09-01-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You want to replace them with empty lines, or delete them? Delete is easy:


Code:
fgrep -vf abc Source.txt

For replacing with nothing, perhaps the following:


Code:
sed -e 's%.*%s/.*&.*//%' abc | sed -f - Source.txt

Maybe your sed can't handle standard input for the -f option; if so, report back here and we'll think about workarounds.

Repeated looping over all the input patterns seems awfully inelegant and inefficient.
  #4 (permalink)  
Old 09-01-2008
unknown123 unknown123 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Hi Vidya,
ur sed commnd is replacing the input seach pattern line with nothing but.... size of output file is getting 3 times more than the orignal file.... means each time after replacing the matched line with nothing the command is adding the entire file into the output file again....

pl help anybody.......hw to overcome this problem...
  #5 (permalink)  
Old 09-01-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 610

Code:
while read line
do
perl -i -ne 's/'$line'//g; print; ' filename
done < abc

  #6 (permalink)  
Old 09-01-2008
unknown123 unknown123 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Dennis...it's also not working.......pls specify clearly.

suppose my output file is OUTFILE .....

pls tell me the exact code..
  #7 (permalink)  
Old 09-01-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 610
Quote:
Originally Posted by unknown123 View Post
Dennis...it's also not working.......pls specify clearly.

suppose my output file is OUTFILE .....

pls tell me the exact code..
Its working well!!!!
Input:

Quote:
["/tmp" ]>cat sample
1233
message
["/tmp" ]>cat OUTFILE
test message 1233 abcf fsd ds f
test message abccd masage message
Code :

Quote:
["/tmp" ]>while read line
> do
> perl -i -ne 's/'$line'//g; print; ' OUTFILE
> done<sample
Output:

Quote:
["/tmp" ]>cat OUTFILE
test abcf fsd ds f
test abccd masage
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0