Print text between 2 strings for the entire file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print text between 2 strings for the entire file
# 1  
Old 11-22-2015
Hammer & Screwdriver Print text between 2 strings for the entire file

hey guys,

for the following output:
Code:
starting
open
open
close
close
starting
close
starting
open
close
close
starting
open
open
close
open

i m able to get the text between "starting" and "close" and move them to a diffrent file like so:

Code:
sed -n '/starting/{:L;N; /close/bK; bL; :K;p;q}' >>somefile.txt

somefile.txt:
Code:
open
open
close
close
close

my question is how do i go about doing so for the entire file?
any help will be great
Thanks!!!
# 2  
Old 11-22-2015
boaz733,
See this link on several different solutions for printing lines between 2 strings:

How to print only lines in between two strings using awk

If the file you labeled as "for the following output:" is your input file, then your output should look like this:
Code:
awk '/starting/ {flag=1;next} /close/{flag=0} flag {print $0}' inputfile
open
open
open
open
open

This User Gave Thanks to mjf For This Post:
# 3  
Old 11-22-2015
i actully managed to solve it with a loop:
it might be ugly but it works.

Code:
#!/bin/bash
x=`wc -l < datafile`
while [ $x -ge 1 ]
do
sed -n '/starting/{:L;N; /close/bK; bL; :K;p;q}' datafile>>outputfile
last_line=`grep -n -m2 starting datafile| tail -1 | cut -f1 -d:`
last_line=$(($last_line - 1))  
sed -i '1,'$last_line'd' datafile
x=`wc -l < datafile`
done

Output is :
Code:
starting
open
open
close
starting
close
starting
open
close
starting
open
open
close

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Cut a word between two strings and repeat the same in the entire file

in the below data i need to search for the word typeMismatch and then traverse back to find the filename of that particular mismatch. Like this we have to get all the file names which has error in them. How can i acheive this. I tried use sed or awk but not able to achevie the same. Sample... (2 Replies)
Discussion started by: ATWC
2 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

Input file needs to match a column and print the entire line

I have a file with class c IP addresses that I need to match to a column and print the matching lines of another file. I started playing with grep -if file01.out file02.out but I am stuck as to how to match it to a column and print the matching lines; cat file01.out 10.150.140... (5 Replies)
Discussion started by: lewk
5 Replies

4. Shell Programming and Scripting

Print text between 2 identical strings

hey, i m having a hard time trying to print only the first occurrence between 2 idenicale strings. for the following output: please help me im a noob please im a noob help me noob please help me im a noob please im a noob help me noob (3 Replies)
Discussion started by: boaz733
3 Replies

5. UNIX for Dummies Questions & Answers

How to print the text between two strings in unix.

Hi Team, Would you please help me for the below scenario. I want to print the text between "PREF:" AND "AVAIL:" in the below example. For example:- TEST_TAF PREF: RAC1 RAC2 RAC3 ...... AVAIL: RAC4 Output will be :-RAC1,RAC2,RAC3............. Thanks in Advance Shoan ... (5 Replies)
Discussion started by: shoan
5 Replies

6. Shell Programming and Scripting

[Help me!] Print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

7. Shell Programming and Scripting

[Help me!] print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

8. UNIX for Dummies Questions & Answers

Print entire columns into new file

Dear All, I am having trouble obtaining the 3 outfiles (as shown below) from a single input file. Could you please help?? INPUT: filename a b c 1 4 2 3 3 2 4 2 7 OUTPUT: outfile1 (a.dat) 1 (2 Replies)
Discussion started by: chen.xiao.po
2 Replies

9. Shell Programming and Scripting

Transpose an entire text file

Hello all, I want to transpose the rows of a file to the columns (every characters include spaces), i.e.: input: abcdefg 123 456 output: a1 b2 c3 d e4 f5 g6 I wrote a script: #!/bin/csh -f (15 Replies)
Discussion started by: heavenfish
15 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question