Search from a line to end of list using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search from a line to end of list using sed
# 1  
Old 07-31-2013
Search from a line to end of list using sed

Dear Unix Experts Smilie,

Below is a small section of a large file with the following list:

1. Starts with string " interest" as the heading
2. Followed by a list of activities
3. Ends with a blank line before starting with another different list.

E.g.
Code:
Sporting interest
football
tennis
swimming
golf
 
<next section>
.......
........

Below are the sed commands attempted without success:
Code:
sed -n '/Sporting interest/,/^$/p'
sed -n '/Sporting interest/,/^$/!p'



I need to find a way to determine the immediate last line at the end of sporting interest, before looping between the list of sports.

I am running Solaris 10 on Intel x86 platform.

Your advice would be much appreciated.

Thanks,

George


Last edited by vgersh99; 07-31-2013 at 11:08 AM.. Reason: edited code tags
# 2  
Old 07-31-2013
I've edited the original post fixing the code tags - hope that's what your original input file looks like.
Please edit if otherwise.

What the desired output would look like?
Or what would need to be done with each 'block' (if that's the objective)?
# 3  
Old 07-31-2013
Of course your attempts will not work as there is a space in that empty (better: not-so-empty) line making your regex fail. Either remove that space or include it in the regex!
# 4  
Old 08-02-2013
Still not able to print list of interests

SmilieThanks to both vgersh99 & rudiC for your advices thus far, I want it to be able to print the line / list of sporting interests. Below is my desire output:

Code:
 
football
tennis
swimming
golf

I have tried sed -n '/interest/,/^$/p' but only the heading line sporting interest was printed as opposed to the list of sporting interest.

In addition, I am looking for way to print content between certain pattern strings or first blank line (means end of list) in either sed, awk or both.

Thanks again,

George

Last edited by vgersh99; 08-02-2013 at 11:29 AM.. Reason: fixed code tags - don't use [icode] - use [code]
# 5  
Old 08-05-2013
Your sed cmd works for me. And it will for you, given you adapt either the file or the regex as stated in my post #3
# 6  
Old 08-05-2013
Code:
awk '/^ *$/ {p=0} p; /Sporting interest/ {p=1}'

Code:
sed -n -e '/Sporting interest/!b' -e ':a' -e 'n; /^ *$/b' -e 'p;ba'

# 7  
Old 08-05-2013
Got it partly working

Thanks MadeInGermany for offerring your advice,

Your first suggest below using awk worked provided there are no blank lines between heading (Sporting interest) and the list itself:

Code:
awk '/^ *$/ {p=0} p; /Sporting interest/ {p=1}' interest.txt

However, this awk statement failed when the content of interest.txt include a blank line below:

Code:
Sporting interest
 
football
soccer
golf

Can we improve on this awk to accommodate blank lines between individual items but should stop at the end of this list?

It would be great if you could explain how the awk statement work as well.

Thanks again,

George

Last edited by Scott; 08-05-2013 at 04:35 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - go to file and search a part and set at the end text

Hello, i hope the titel is okay. I want to edit a line in /etc/arno-iptables-firewall/firewall.conf So the Line can contains: OPEN_TCP="436, 25, 80, 110, 143, 443, 465, 587, 993, 995, 21, 20" i want to search in this line: OPEN_TCP="*, *, 80, *, *, 443, *, *, *, *" and i want to... (11 Replies)
Discussion started by: tJAdASwIRDESSEI
11 Replies

2. Shell Programming and Scripting

Search for a string, then append character to end of that line only

I have 2 files that I am working with $ cat file1 server1 server3 server5 server6 server8 $ cat file2 server1;Solaris; server2; SLES; server3;Linux; server4; Solaris; server5;SLES; server6;SLES; server7;Solaris; server8;Linux; (1 Reply)
Discussion started by: snoman1
1 Replies

3. Shell Programming and Scripting

sed removing until end of line

All: Can somebody help me out with a sed command, which removes the the first occurance of ')' until the end of the line If I have the following input ... (5 Replies)
Discussion started by: BeefStu
5 Replies

4. UNIX for Dummies Questions & Answers

Removing end of line using SED

Hello Friends, How can I remove the last two values of this line using sed John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638:9/3/90:45900 The result should look like this: John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638 (3 Replies)
Discussion started by: humkhn
3 Replies

5. Shell Programming and Scripting

sed issue with end of line

Example, Trying to replace text to the end of a line. Text file looks like this PP= 4 PP= 412 PP= 425 I want to replace only the following line: PP= 4 with PP= 2 How can this be done with sed? (3 Replies)
Discussion started by: hanson397
3 Replies

6. Shell Programming and Scripting

search directory-find files-append at end of line

Hi, I have a command "get_data" with some parameters in few *.text files of a directory. I want to first find those files that contain this command and then append the following parameter to the end of the command. example of an entry in the file :- get_data -x -m50 /etc/web/getid this... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

7. Shell Programming and Scripting

delete to end of line with SED

I have a file with a bunch of similar lines in which I want to extract a phrase delimited by the first occurance of a '>' at the beginning and the first occurance of a '<' at the end (you might have guessed these are beginning/end of HTML tags). Using Sed I have managed to delete up to and... (7 Replies)
Discussion started by: coldcanuck
7 Replies

8. Shell Programming and Scripting

Removing character from list line (at the end)

Hi, I have file as shown below. abc, def, abc, xyz, I have to remove ',' from end of last line (xyz,). How can I do that with single command? Is it possible or I have to iterate through complete file to remove that? - Malay (2 Replies)
Discussion started by: malaymaru
2 Replies

9. Shell Programming and Scripting

Number a list at end of line using 'sed'

Hi All I have a script which has produced a list, I have used 'sed' to number my list, but i want to list at end of line with the first line starting at zero (0) and brackets round it ie My List i want Hello (0) this (1) day (2) can (3) be (4) sed '/./=' filename | sed '/./N; s/\n/) /'... (5 Replies)
Discussion started by: chassis
5 Replies

10. UNIX for Dummies Questions & Answers

sed to end of line

I know this is going to be an easy anwer, but I haven't been able to figure this out - even with the help of the previous posts. I want to go from this PROD USER anon; to this TEST; I have coded a few sed commands, and none of them are getting the job done. anon will not always be the... (2 Replies)
Discussion started by: djschmitt
2 Replies
Login or Register to Ask a Question