The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
How to remove the specific lines from file using perl dipakg Shell Programming and Scripting 4 06-11-2008 02:45 AM
Remove duplicates from File from specific location gopikgunda Shell Programming and Scripting 1 04-09-2008 02:16 AM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 09:50 PM
remove specific lines from a file hcclnoodles Shell Programming and Scripting 14 09-07-2006 12:31 PM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-23-2005 12:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2007
bluemoon1 bluemoon1 is offline
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!!
  #2 (permalink)  
Old 09-28-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Note sure if this works for gnu sed only...
Code:
sed '/The Following lines/,${/str/d}' file1
  #3 (permalink)  
Old 09-28-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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
  #4 (permalink)  
Old 09-28-2007
porter porter is offline Forum Advisor  
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.
  #5 (permalink)  
Old 09-28-2007
bluemoon1 bluemoon1 is offline
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!!
  #6 (permalink)  
Old 09-29-2007
varungupta varungupta is offline
Registered User
  
 

Join Date: Feb 2007
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
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.

  #7 (permalink)  
Old 09-29-2007
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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 03:58 AM.. Reason: Debug print statement removed
Sponsored Links
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 07:20 AM.


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