Find a string in textfile, erase $num lines after that string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string in textfile, erase $num lines after that string
# 1  
Old 09-09-2010
Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern:

Code:
STRING1
UNIQUE_STRING1
STRING2
STRING3
STRING4
STRING5

STRING1
UNIQUE_STRING2
STRING2
STRING3
STRING4
STRING5

STRING1
UNIQUE_STRING3
STRING2
STRING3
STRING4
STRING5

I would like to somehow find for example UNIQUE_STRING3 and delete the line before, UNIQUE_STRING3, and the 4 lines after that string.
STRING1 to 5 is identical and repeated lots of time in this textfile, so I can not search for them.
Of course the script should be in sh, not bash.

Can someone help me? Smilie

Last edited by radoulov; 09-09-2010 at 10:54 AM.. Reason: Code tags, please!
# 2  
Old 09-09-2010
Try:
Code:
awk '"UNIQUE_STRING3"==$2{$0=$2}1' FS="\n" RS= ORS="\n\n" infile


Last edited by Scrutinizer; 09-09-2010 at 11:12 AM..
# 3  
Old 09-09-2010
No, doesnt seem to work.
Havent figured out exactly what that line tries to do yet, but it doesnt work when I test it.
# 4  
Old 09-09-2010
I get:
Code:
STRING1
UNIQUE_STRING1
STRING2
STRING3
STRING4
STRING5

STRING1
UNIQUE_STRING 2
STRING2
STRING3
STRING4
STRING5

UNIQUE_STRING3

Are you on Solaris? use /usr/xpg4/bin/awk or nawk instead of awk
# 5  
Old 09-09-2010
No, Im on freebsd.
I think there is a nawk available in ports, I will try that.
# 6  
Old 09-09-2010
I don't have sh to test this. (sh is linked to bash in Cygwin).
See if these work.

Code:
$
$
$ cat f11
STRING1
UNIQUE_STRING1
STRING1
STRING1
STRING1
STRING1
 
STRING2
UNIQUE_STRING2
STRING2
STRING2
STRING2
STRING2
 
STRING3
UNIQUE_STRING3
STRING3
STRING3
STRING3
STRING3
$
$
$ cat f11.sh
#!/usr/bin/bash
str="$1"
num=1
while read LINE
do
  if [ -z "$LINE" ]; then
    echo $LINE
    print_block="y"
    num=0
  elif [ "$num" -eq "2" -a "$LINE" = "$str" ]; then
    print_block="n"
  elif [ "$num" -eq "2" -a "$LINE" != "$str" ]; then
    echo $x
    echo $LINE
  elif [ "$num" -gt "2" -a "$print_block" != "n" ]; then
    echo $LINE
  elif [ "$num" -eq "1" ]; then
    x=$LINE
  fi
  num=`expr $num + 1`
done <f11
$
$ . f11.sh UNIQUE_STRING2
STRING1
UNIQUE_STRING1
STRING1
STRING1
STRING1
STRING1
 

STRING3
UNIQUE_STRING3
STRING3
STRING3
STRING3
STRING3
$
$
$ ## With Perl's "slurp" mode
$ perl -lne 'BEGIN{$/="\n\n"} while(/([\w\n]+)/mg){print $1,"\n" if !/UNIQUE_STRING2/}' f11
STRING1
UNIQUE_STRING1
STRING1
STRING1
STRING1
STRING1
 
STRING3
UNIQUE_STRING3
STRING3
STRING3
STRING3
STRING3
 

$
$
$ ## Using Perl arrays
$ perl -lne 'if (!/^$/){push @x,$_} elsif(/^$/ or eof) {if ($x[1] ne "UNIQUE_STRING2"){print for (@x)}; @x=()}
             END{if ($x[1] ne "UNIQUE_STRING2"){print for (@x)}}' f11
STRING1
UNIQUE_STRING1
STRING1
STRING1
STRING1
STRING1
STRING3
UNIQUE_STRING3
STRING3
STRING3
STRING3
STRING3
$
$
$

tyler_durden
# 7  
Old 09-09-2010
u can try?

Code:
# sed -E -e 'N;/UNIQUE_STRING3/P;/UNIQUE_STRING3/,+4d' infile
STRING1
UNIQUE_STRING1
STRING2
STRING3
STRING4
STRING5

STRING1
UNIQUE_STRING2
STRING2
STRING3
STRING4
STRING5

STRING1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

4. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

5. Shell Programming and Scripting

Find the position of lines matching string

I have a file with the below format, GS*8***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE** GS*9***** ST*2 H* J* RMR*567889(This is the unique number to locate this row) L* SE* GE***** (16 Replies)
Discussion started by: Muthuraj K
16 Replies

6. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

Find a string and place two blank lines

Hi friends, I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string Example: aaa 11 bbb 1 2 3 aaa 22 bbb 4 5 6 Output (4 Replies)
Discussion started by: shaliniyadav
4 Replies

9. Shell Programming and Scripting

Script to find string and return next 2 lines

I am trying to create a script to search for a string within a file, and if found, return the next two lines. Example file:- msj mh query return this 1 return this 2 mjk mhj query return this 3 return this 4 So the script would identify the string "query" and then return the lines... (10 Replies)
Discussion started by: daveaasmith
10 Replies

10. Shell Programming and Scripting

find string, then get the next 3 lines in a file

Hi all. HPUX - /bin/sh (posix) I am parsing a 3 field flat file, space deliminted example data.file acct dining mem open 0 50 dep 50 0 close 255 0 acct plus mem open 100 100 dep 50 0 close 323 0 I would like to find strings, then write the next 3 lines out to another file ... (8 Replies)
Discussion started by: lyoncc
8 Replies
Login or Register to Ask a Question