how to remove specific lines from a file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 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!!
# 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.

Smilie
# 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

$

Jean-Pierre.

Last edited by aigles; 09-30-2007 at 05:58 AM.. Reason: Debug print statement removed
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #681
Difficulty: Medium
In 2011, the Solaris 11 kernel source code was leaked to WikiLeaks.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Remove duplicate consecutive lines with specific string

Hello, I'm trying to remove the duplicate consecutive lines with specific string "WARNING". File.txt abc; WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 bcd; abc; 123 123 123 WARNING 1234 WARNING 2345 WARNING 2345 efgh; (6 Replies)
Discussion started by: Mannu2525
6 Replies

3. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

4. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

6. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies

7. Shell Programming and Scripting

how to remove specific lines from a file (reprise)

Hello, I've to change the shell in /etc/passwd for some users . I've the list of users but I'm not able to modify the file with scripting . I'm working on a Sol10 . Can anyone help me ? tnks (7 Replies)
Discussion started by: gogol_bordello
7 Replies

8. Shell Programming and Scripting

How to remove the specific lines from file using perl

Can anyone tell me what could be the solution to following : I have one .txt file which contains some "seed" information. This seed may appear multiple time in the file so what I want do is if this seed appears again in the file then that line should be removed. Please provide the script code... (4 Replies)
Discussion started by: dipakg
4 Replies

9. Shell Programming and Scripting

remove specific lines from flat file using perl

Hi, Here is wat im looking for.. i have a flat file which looks like this.. 00 * * * * .. .. * * text text text COL1 COL2 ----- ----- 1 a (12 Replies)
Discussion started by: meghana
12 Replies

10. Shell Programming and Scripting

remove specific lines from a file

Hi there I have a file with a variable amount of rows but the 45th, 46th and 47th charachter of each line is the status field which is a three digit code ie 001, 002, 003 etc. My question is this..I need to strip all the records/lines with 002's out of the file completely and put them into... (14 Replies)
Discussion started by: hcclnoodles
14 Replies

Featured Tech Videos