Remove every line with specific string, and also the one above and below it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove every line with specific string, and also the one above and below it
# 1  
Old 05-21-2014
Remove every line with specific string, and also the one above and below it

I would like to identify every line with a specific string, in this case: "Mamma".
I would like to remove that line, and also the line above it and below it. So the below

Code:
Where are all amazing Flats
Look At The Great Big White
Hey There Hot Mamma
You Are So hot Baby
I wish You were Mine
Hee How Yee Haw
Hey There Hot Mamma
Wait A minute
Hee Haw Yee Haw
Hey There Hot Mamma
A song Is Here
Make me a List

Becomes

Code:
Where are all amazing Flats
I wish You were Mine
Make me a List


I was wondering if anyone could help me out? I've googled but it is a little complex for Google

---------- Post updated at 05:14 PM ---------- Previous update was at 05:07 PM ----------

I think this might work

sed -n '/Mamma/{N;s/.*//;x;d;};x;p;${x;p;}' file | sed '/^$/d'

Last edited by jim mcnamara; 05-21-2014 at 06:10 PM.. Reason: code tags
# 2  
Old 05-21-2014
An awk approach by scanning file twice:
Code:
awk 'NR==FNR{if($0~/Mamma/) A[NR]=$0;next}!(A[FNR-1]) && !(A[FNR]) && !(A[FNR+1])' file file

# 3  
Old 05-22-2014
Perhaps ed or ex would be simpler:
Code:
ed -s file <<-"EOF"
	g/Mamma/.-1,.+1d
	1,$p
EOF

And, if you want to modify the input file instead of just print the file contents with the specified lines deleted, change the:
Code:
	1,$p

to:
Code:
	w

These 4 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 05-22-2014
Hi.

Solution with a relative of grep, cgrep:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate match+previous+successor, then invert to delete; cgrep.
# See: http://sourceforge.net/projects/cgrep/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C column cgrep

FILE=${1-data1}

pl " Input data file $FILE (columnized):"
column $FILE

pl " Results:"
cgrep -D -V -1 +1 Mamma $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
column - ( /usr/bin/column, 2007-11-20 )
cgrep ATT cgrep 8.15

-----
 Input data file data1 (columnized):
Where are all amazing Flats	Hey There Hot Mamma
Look At The Great Big White	Wait A minute
Hey There Hot Mamma		Hee Haw Yee Haw
You Are So hot Baby		Hey There Hot Mamma
I wish You were Mine		A song Is Here
Hee How Yee Haw			Make me a List

-----
 Results:
Where are all amazing Flats
I wish You were Mine
Make me a List

The cgrep code is at the URL noted in the script; it needs to be compiled.

Best wishes ... cheers, drl
# 5  
Old 05-22-2014
Another way to do it in awk:
Code:
awk '/Mamma/ { s=x ; getline ; next } s { print s } { s=$0 } END { print s }' file

# 6  
Old 05-22-2014
Assuming no duplication in records, could you get away with two grep commands?:-
Code:
grep -B1 Mamma infile > tempfile
grep -vf tempfile infile > outfile


Have I missed the point in trying to make it too simple?
Code:
# cat input_file                                                  
Where are all amazing Flats
Look At The Great Big White
Hey There Hot Mamma
You Are So hot Baby
I wish You were Mine
Hee How Yee Haw
Hey There Hot Mamma
Wait A minute
Hee Haw Yee Haw
Hey There Hot Mamma
A song Is Here
Make me a List

# grep -B1 Mamma input_file>/tmp/tfile$$                          

# cat /tmp/tfile$$                                                
Look At The Great Big White
Hey There Hot Mamma
--
Hee How Yee Haw
Hey There Hot Mamma
--
Hee Haw Yee Haw
Hey There Hot Mamma

# grep -vf /tmp/tfile$$ input_file      
Where are all amazing Flats
You Are So hot Baby
I wish You were Mine
Wait A minute
A song Is Here
Make me a List

#




Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Remove line with specific character

HI Input :- Aog:0rt__dev_8 LAAXU24 vs.3 LAA40l0 ** LAAXU241 ** Output :- Aog:0rt__dev_8 LAAXU24 vs.3 Delete the line with ** (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

4. UNIX for Dummies Questions & Answers

How to remove a string from a specific column in a file

Hello, A basic query. How can I remove a string from a specific column. For example, remove "abcd" just from column 2 in example file: abcd abcd1 abcd abcd2 abcd abcd3 to get output: abcd 1 abcd 2 abcd 3 Thank you!:) (4 Replies)
Discussion started by: auburn
4 Replies

5. Shell Programming and Scripting

Remove a specific line from grep output string

Dear All I want to search string "1000" from input file and if it found i want remove line that contain 1000 and also remove 3 line above it and 2 line below it. INPUT FILE: BHAT-D 2 aaa ID CODE GS UPDATE MODE LANG MCO MCL NUMPAGES 50 ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

6. Shell Programming and Scripting

remove a specific line in a LARGE file

Hi guys, i have a really big file, and i want to remove a specific line. sed -i '5d' fileThis doesn't really work, it takes a lot of time... The whole script is supposed to remove every word containing less than 5 characters and currently looks like this: #!/bin/bash line="1"... (2 Replies)
Discussion started by: blubbiblubbkekz
2 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. UNIX for Dummies Questions & Answers

How to remove columns with specific string?

If I have a data test.txt with 1000 columns such as: id sex gene1 gene2 gene2.dl gene3 gene4 gene4.dl ....... 1 1 AA AT AT TT AT AT ....... 2 1 AG TT TT TA AA AA ....... 3 2 AA AT AT TT AT ... (2 Replies)
Discussion started by: AMBER
2 Replies

9. Shell Programming and Scripting

Remove Line that contains specific string

I am looking for a way to remove any line in a text file that contains the string "Mac address". I guess you would grep and sed, but I am not sure how to do this. Thanks for you help. (3 Replies)
Discussion started by: CBarraford
3 Replies
Login or Register to Ask a Question