Fetching a set of lines with start and end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetching a set of lines with start and end
# 1  
Old 04-03-2012
Fetching a set of lines with start and end

Hi Folks,

Need help in fetching a group of lines with a start and end strings. Example is shown below.

start#morning
tea
jog
breakfast
end

start#afternoon
lunch
work
chat
end

start#evening
snacks
tea
chat
end

i need to fetch lines "start" till "end" wherever the word "tea" is available. so my output will be like below.

start#morning
tea
jog
breakfast
end

start#evening
snacks
tea
chat
end

Thanks in advance for your help guys.
# 2  
Old 04-03-2012
Code:
awk  '/tea/ {print "start" $0 "end"}' RS='start|end' file

# 3  
Old 04-03-2012
Code:
awk '/tea/' RS= FS="\n" yourfile

or

Code:
awk '/tea/' RS= FS="\n" ORS="\n\n" yourfile

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 04-03-2012
Hi Guys,

Thanks much for the reply.

Please let me know how do i give input from another file to this awk. I tried the below but didn't work.

for i in `cat input.txt`
do
awk '/$i/ {print "start" $0 "end"}' RS='start|end' file.txt >> output.txt
done
# 5  
Old 04-03-2012
You can give a try to

Code:
awk 'NR==FNR{a[$0];next}{for(i in a) if($0~i) {print $0;next}}' input.txt RS= FS="\n" ORS="\n\n" yourfile

I haven't tried it so it may require some fix
# 6  
Old 04-03-2012
To get shell variables into awk it's easiest to use -v.

Similar to:
Code:
while read srch
do
   awk -vval=$srch '$0 ~ val {print "start" $0 "end"}' RS='start|end' file.txt
done << input.txt >> output.txt


Last edited by CarloM; 04-03-2012 at 01:10 PM.. Reason: see below...
# 7  
Old 04-03-2012
Quote:
Originally Posted by CarloM
To get shell variables into awk it's easiest to use -v.

Similar to:
Code:
while read srch
do
   awk -vval=$srch '$0 ~ srch {print "start" $0 "end"}' RS='start|end' file.txt
done << input.txt >> output.txt

You meant :
Code:
...
awk -vval=$srch '$0 ~ val .... 
...

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

3. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

4. Shell Programming and Scripting

Find between lines start and end: awk with bash variables

I have a file as follows: 0 1056 85540 414329 774485 1208487 1657519 2102753 2561259 3037737 3458144 3993019 4417959 4809964 5261890 5798778 6254146 I want to find all lines between a specified start and end tag. (6 Replies)
Discussion started by: jamie_123
6 Replies

5. Shell Programming and Scripting

Get the lines from logfile within start and end date

Hi guys, I am having the below logfile,date in yyyy-mm-dd 2013-08-02 *some content* 2013-08-02 *some content* 2013-08-02 *some content* 2013-08-03 *some content* 2013-08-05 *some content* from the above logfile i need to get the lines between the two timestamps,if i give... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. Programming

Returning start/end indices

I have an array of distances and a len, for example len = 323 dist = I want to calculate the start and end index values around each distance in the array with a length of len from it. Values in dist are stored in ascending order. (4 Replies)
Discussion started by: kristinu
4 Replies

8. Shell Programming and Scripting

Perl regex using /START/../END/

I need help with perl code. I have a data file with lots of data example: data.txt file START DATA1 sjdfh kjhdf DATA2 sdkjfhk jds dshfgdf ... Around 20 - 30 lines END LDDDD awdkjasd a sdkahgdk jasdh SOME CRAP Some EMPTY lines etc START DATA1 sjdfh kjhdf DATA2 sdkjfhk jds... (2 Replies)
Discussion started by: chakrapani
2 Replies

9. Shell Programming and Scripting

fetching Month end using cal

Hi , Can someone please assist to get monthend using cal utility and monthend shouldn't be saturday or sunday. Cheers, gehlnar (2 Replies)
Discussion started by: gehlnar
2 Replies

10. UNIX for Advanced & Expert Users

Complete dump from start to the end

Hi all... I was accidentally wipe off my iphone and lost all the data in it:(. So I tried to use dd to create an image from iphone and ssh to the Mac by using: dd if=/dev/disk | ssh user@MacIP of='image.img' Then I mount this image on Mac/Windows and run recovery software because most of the... (1 Reply)
Discussion started by: seraphc
1 Replies
Login or Register to Ask a Question