![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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!! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Note sure if this works for gnu sed only...
Code:
sed '/The Following lines/,${/str/d}' file1
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
$
Last edited by aigles; 09-30-2007 at 12:58 AM. Reason: Debug print statement removed |
||||
| Google The UNIX and Linux Forums |