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
# 8  
Old 08-05-2013
This one allows 1 blank line after the header line
Code:
awk '!(c && c--) && /^ *$/ {p=0} p; /Sporting interest/ {p=1; c=1}'

It is a sequence of implicit if clauses followed by an { action }.
The p; is an implicit if clause without { action } so the default action is { print }
With explicit if clauses and actions it would give
Code:
awk '{if ((c && c--)==0 && /^ *$/) {p=0}} {if (p!=0) {print}} {if (/Sporting interest/) {p=1; c=1}}'

The variable p indicates if something should be printed.
The variable c holds the number of lines to be not considered for searching for end of section. The (c && c--) let it decrement until 0 but not below 0.
# 9  
Old 08-05-2013
Unless you depict some condition to tell one section from the next, there's no reliable way to "stop at the end of this list" as you require. Blank lines separating lists containing blank lines can work only if you can tell the exact number of blank lines per list.
# 10  
Old 08-06-2013
Near perfect solution

SmilieThank you so much again to both MadeInGermany & rudiC for getting almost everything correct. There is a minor need to print from 4th line as opposed to 1st due to the following reason on the actual data which is slightly different to the simplistic sporting interest example provided:

( i ) Line 1 is blank which I don't need
( ii ) Line 2 is the heading followed by a line of hyphens which act as underline of headers in line 2.

Otherwise, it is working remarkably well even though I still need to digest it properly for the moment.

The current workaround is as follows due to my lack of knowledge in how these Awks work still:

Code:
 
awk '!(c && c--) && /^ *$/ {p=0} p; /Sporting interest/ {p=1; c=1}' | sed -n '4,$p'
 
awk '{if ((c && c--)==0 && /^ *$/) {p=0}} {if (p!=0) {print}} {if (/Sporting interest/) {p=1; c=1}}' | sed -n '4,$p'



I have tried twikking either of the p & c variables without much luck.

Thanks a million for the persistent support,

George

Last edited by gjackson123; 08-06-2013 at 10:19 AM..
# 11  
Old 08-06-2013
Why don't you post a real life example with several sections each differing in composition? And, go to the extremes when it comes to the conditions to be met.
# 12  
Old 08-06-2013
The awk solution is already working

SmilieHi RudiC,

The suggestion provided by MadeInGermany is already working. I simply need to print from line 4 to the end instead. Don't worry about it if this require much more work.

I hesitate to disclose people's confidential data which would require me to make a lot of changes first.

Thanks,

George
# 13  
Old 08-07-2013
You can't really do this unless you have a proper end of section marker. If your end of section is a blank line but one of your values could sometimes be a blank as well then there's no way a script could figure out which one to parse.
# 14  
Old 08-08-2013
No such inconsistencies in my data

SmilieHi konsolebox,

Luckily, I have tested the suggested Awk solution on a few hundreds set of data and have found no such discrepancies you have highlighted. As a result, I am very pleased and greatful for everyone's valuable advices and efforts for providing a working solution.

Thanks,

George
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