Delete 2 strings from 1 line with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete 2 strings from 1 line with sed?
# 1  
Old 06-04-2013
Delete 2 strings from 1 line with sed?

Hi guys, I wonder if it's possible to search for a line containing 2 strings and delete that line and perhaps replace the source file with already deleted line(s).

What I mean is something like this:

sourcefile.txt

Code:
line1: something 122344 somethin2 24334 45554676
line2: another something 9998898 34343424
line3: big small something 122121 23323 545454 657676767

and now I'd like to search for a line containg "something" and "122344" which should find and delete just the 1st line.

then the outputfile.txt will contain just the 2 lines without the matched 2 strings:

Code:
line1: another something 9998898 34343424
line2: big small something 122121 23323 545454 657676767

How would you do that?

Thanks.
# 2  
Old 06-04-2013
Does "something" always occur before "122344" ?
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 06-04-2013
This removes all line containing both something and 122344
Code:
awk '!(/something/ && /122344/)' infile
another something 9998898 34343424
big small something 122121 23323 545454 657676767

This User Gave Thanks to Jotne For This Post:
# 4  
Old 06-04-2013
Answer to query

Hello,

Could you please create a script as follows for same.

Code:
 
$ cat test121.ksh
while read line
do
a=`echo $line | grep -i "something 122344"`
if [[ -n $a ]]
then
echo "Line deleted that have something 122344 together."
else
echo $line >> test13
fi

done < "test12"
$


Where test12 kis the file which have all your data and test13 wil be the new file after running the script.

Please get back to me in case you have any queries.


Thanks,
R. Singh

Last edited by radoulov; 06-04-2013 at 09:36 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 06-04-2013
Thanks guys, I'll test that, and I'm wondering is it possible to use also the sed command?
# 6  
Old 06-04-2013
Try this: something should occur before 122344 strings in order to delete the line

sed '/something.*122344/d' infile
This User Gave Thanks to juzz4fun For This Post:
# 7  
Old 06-04-2013
Same as juzz4fun post but with awk
Code:
awk '!/something.*122344/' infile

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

2. Shell Programming and Scripting

Delete duplicate strings in a line

Hi, i need help to remove duplicates in my file. The problem is i need to delete one duplicate for each line only. the input file as follows and it is not tab delimited:- The output need to remove 2nd word (in red) that duplicate with 1st word (in blue). Other duplicates should remained... (12 Replies)
Discussion started by: redse171
12 Replies

3. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 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

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

6. Shell Programming and Scripting

How can i delete the content between all the occurences of two strings using sed or awk command

Hi. I have to delete the content between all the occurrences of the xml tags in a single file. For example: * The tags <script>.....................</script> occurs more than once in the same file. * It follows tagging rules meaning a start tag will be followed by an end tag. Will not have... (9 Replies)
Discussion started by: satheeshkumar
9 Replies

7. Shell Programming and Scripting

Using Bash/Sed to delete between identical strings

Hi. I'm hoping that someone can help me with a bash script to delete a block of lines from a file. What I want to do is delete every line between two stings that are the same, including the line the first string is on but not the second. (Marked lines to match with !) For example if I... (2 Replies)
Discussion started by: Zykr
2 Replies

8. Shell Programming and Scripting

delete repeated strings (tags) in a line and concatenate corresponding words

Hello friends! Each line of my input file has this format: word<TAB>tag1<blankspace>lemma<TAB>tag2<blankspace>lemma ... <TAB>tag3<blankspace>lemma Of this file I need to eliminate all the repeated tags (of the same word) in a line, as in the example here below, but conserving both (all) the... (2 Replies)
Discussion started by: mjomba
2 Replies

9. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question