delete the line with a particular string in a file using sed command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete the line with a particular string in a file using sed command.
# 1  
Old 05-08-2012
delete the line with a particular string in a file using sed command.

Hello,

I want to delete all lines with given string in file1 and the string val is dynamic.
Can this be done using sed command.

Sample:
Code:
vars="test twinning yellow"
 
for i in $vars
do
grep $i file1  [search for test in file1]
if[ $? -ne 0 ]
then
echo "Do Nothing"
else
sed `/$i/d` file1
fi
done

Using the above code the particular line is not getting deleted.
Can someone suggest me in correcting above code.
OR
Can someone provide with other solution.

Thanks in advance.

Last edited by Scrutinizer; 05-09-2012 at 03:58 AM.. Reason: code tags
This User Gave Thanks to PrasadAruna For This Post:
# 2  
Old 05-08-2012
How about this:

Code:
grep -Ev "test|twinning|yellow" file1

or this using your for loop + sed method:

Code:
vars="test twinning yellow"
for i in $vars
do
    args=$args" -e /$i/d"
done
 
sed $args file1


Last edited by Chubler_XL; 05-08-2012 at 10:08 PM..
# 3  
Old 05-09-2012
using sed it deletes only temperorily, how to make it permanent

Hello Chubler,

Thank you for your reply.

The post looks cool but it deletes the respective lines temperorily and displays the other lines in the file to console. BUt when again we open the file the deleted lines will reappear.(Will not be deleted)

So is tehre any solution where i want the lines to be deleted permanently, also if possible i want to keep track of deleted lines.

As per above sample:

Code:
vars="test twinning yellow"
for i in $vars
do
    args=$args" -e /$i/d"
done
 
sed $args file1

The sed command used here wont delete the lines with strings test, twinning and yellow. When we open the file we still see them in the file.
Is there any way where we can delete it permanently and maintain some track what all we deleted.

Thank you very much Smilie

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use [code]...[/code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 05-09-2012 at 03:58 AM..
# 4  
Old 05-09-2012
For the sed solution use the --in-place flag:
Code:
vars="test twinning yellow"
for i in $vars
do
    args=$args" -e /$i/d"
done
sed $args --in-place=.bak file1

This will write the original version to file1.bak and update file1 inplace


For the grep solution you could:
Code:
mv file1 file1.bak
grep -Ev "test|twinning|yellow" file1.bak > file1

# 5  
Old 05-09-2012
It worked and served my requirement

Thank You Chubler.

It worked and fits to my requirement.

I am a learner and just today I wrote my first shell script, can you suggest me any reference docs/books or any ebooks for learning unix shell scripting.

Thank you Smilie
# 6  
Old 05-09-2012
you can use awk also

Code:
awk '$0!~/test|twinning|yellow/' input.txt > output.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete line from file using sed command?

hello Team, I want to delete below linux using sed command but I am getting error.sed -i '/url=/status.cgi?hostgroup=/d' 3 error:sed: -e expression #1, char 32: unknown option to `s' Could you please help me with correct syntax. My line contain / character because of that I am getting... (4 Replies)
Discussion started by: ghpradeep
4 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Shell Programming and Scripting

sed command to delete everything after > on line

I have a large data file that I need to edit. There are lines like, > <IDNUMBER> (ST000002) > <SUPPLIER> (ST000002) > <IDNUMBER> (ST000004) > <SUPPLIER> (ST000004) and I need to delete everything after the >, excepting the end of line. > <IDNUMBER> > <SUPPLIER> > <IDNUMBER> > ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

6. Shell Programming and Scripting

Sed find exact string and delete line with variable

All, I am trying to read in a variable and search a file then delete based on that string, but i want to match exact word. This works but it matches all, i don't want to match anthing that contains the string, just the exact string. sed -i "/$feedname/d" file I tried sed... (1 Reply)
Discussion started by: markdjones82
1 Replies

7. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

8. Shell Programming and Scripting

sed command to delete line

HI, Im using the below commmand to delete lines having "(" at the start of a line. sed -e '/^(/d' But i need a command to delete lines that has only "(" in it and no other strings. infile - asdf asdf ( asdf ( asd outfile (1 Reply)
Discussion started by: dvah
1 Replies

9. Shell Programming and Scripting

SED delete only first line command - Help need

Can any one help me how the sed delete only first line command (D) works. sed -f sedcmds File.txt In this how the sed delete only first line command (D) works to delete the blank lines appearing more than ones leaving only one blank and deleting others blanks. While doing step by step when... (2 Replies)
Discussion started by: gwgreen1
2 Replies

10. Shell Programming and Scripting

using sed command to delete a string spanning multiple lines

file1 contains the following data sssssssssss firstline secondline pppppppppp ssssssssss Using sed comamnd i am trying to delete firtsline secondline. so, output should be sssssssssss pppppppppp ssssssssss I tried in the following the way, but it is not working. sed ... (9 Replies)
Discussion started by: radha.kalivar
9 Replies
Login or Register to Ask a Question

Featured Tech Videos