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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove the specific lines from file using perl dipakg Shell Programming and Scripting 4 06-10-2008 11:45 PM
Remove duplicates from File from specific location gopikgunda Shell Programming and Scripting 1 04-08-2008 11:16 PM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 06:50 PM
remove specific lines from a file hcclnoodles Shell Programming and Scripting 14 09-07-2006 09:31 AM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-22-2005 09:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-27-2007
Registered User
 

Join Date: Sep 2007
Posts: 53
how to remove specific lines from a file

When restoring a file in my uninstall program I need to remove the lines I added to a file during the install. In between the file can be modified by the users.

Assume file1 is as follow:

xxx str2 xxxx
.....
...The Following lines containing str* have to be removed...
xxx str1 xxxx
xxx str2 xxxxx
xxxx aaa, xxx
xxxxx str4 xxx


Result:
xxx str2 xxxx
.....
xxxx aaa, xxx

I know how to remove the line "The Following lines containing str* ...":
ln=`grep -n 'The Following lines containing' | cut -d',' -f 1`
sed $lnd file1

I also know how to remove the lines with str* in it with awk:
awk 'BEGIN { FS=";" } { if ( $2 ~ /str*/ ) { print $0 } }' file1 > outputF

However, how to do this without removing the lines with str* before the line "The Following lines containing str* have to be removed" ? (the first line in file1 for example).

Any help would be greatly appreciated!!
Reply With Quote
Forum Sponsor
  #2  
Old 09-27-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
Note sure if this works for gnu sed only...
Code:
sed '/The Following lines/,${/str/d}' file1
Reply With Quote
  #3  
Old 09-28-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 472
awk!!

hi,
Hope this one can help you.

Code:
echo input the begin row
read line
nawk -v l=$line '
{
if ($0 ~ /str/)
{
        if (NR<=l)
        print $0
}
else
print $0
}' filename
Reply With Quote
  #4  
Old 09-28-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
A cunning approach would be to generate a patch file at install time that would then remove the lines you added, that could be done by savinging a backup of the original and doing a diff at installation time, then deleting the backup. Then at uninstall time you apply the patch.
Reply With Quote
  #5  
Old 09-28-2007
Registered User
 

Join Date: Sep 2007
Posts: 53
Ygor:
That doesn't work on my sunOS 5.10. But thanks the same.

summer_cherry:
It works perfect! -and I've learned a lot about this nawk utility today. Thanks for your help!

porter:
That was what I did- however, I wanted the installation program to be able to run as many times as the users like without the need of uninstallation in between- and every time it runs it should be as clean as the first time... I thought that'd make it all a bit more dynamic...
Thanks for the suggestion!!
Reply With Quote
  #6  
Old 09-29-2007
Registered User
 

Join Date: Feb 2007
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 187
Hey,

To delete a line where a pattern matches, you can use sed.

sed '/^[Mm]ango/d' file1.txt

Above command will delete all the lines which starts with the word Mango or mango.

Similarly you can search the pattern in a line, if that will be found then delete the line.

Reply With Quote
  #7  
Old 09-29-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Try the following :

Code:
$ cat bluemoon.sh
awk '
/The Following lines containing [^[:space:]]*\* have to be removed/ {
   str = $0;
   sub(/.*The Following lines containing /, "", str);
   sub(/\* have to be removed.*/, "", str);
}
! str || $0 !~ str
' bluemoon.dat
$ cat bluemoon.dat
xxx string2 xxxx
.....
...The Following lines containing string* have to be removed...
xxx string1 xxxx
xxx string2 xxxxx
xxxx aaa, xxx
xxxxx string4 xxx

$ bluemoon.sh
xxx string2 xxxx
.....
str=<string>
xxxx aaa, xxx

$
Jean-Pierre.

Last edited by aigles; 09-30-2007 at 12:58 AM. Reason: Debug print statement removed
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0