sed - combination of line deletion and pattern matching


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed - combination of line deletion and pattern matching
# 8  
Old 08-05-2012
Quote:
Originally Posted by bakunin
You got it right - almost - but missed by a hairs width.

Any command (like "s", etc.) can be preceeded by a range clause. A range clause can be either a range of lines, determined by line numbers or a starting/ending regexp, or it can be a single line number or regexp. In the second case the command would either apply to the single line number or to all lines matching the regexp:

Code:
1,3 s/x/y/
/^aa/,/^bb/ s/x/y/
1 s/x/y/
/^abc/ s/x/y/

The first line would apply "s/x/y/" only to lines 1-3, the second line would apply it to all lines from a line starting with "aa" to a line starting with "bb".

The third line would apply the command only to line number 1 and the fourth to all lines starting with "abc".

This you did correctly, but range clauses cannot be nested, and

Code:
/^$/d

is also a range clause: one, that applies the command "d" only to lines of the form "/^$/", like in my fourth example. To correct your code you simply have to correctly nest the commands:

Code:
1,3 {
     /^$/ {
          d
     }
}

or, in short form:

Code:
1,3 {;/^$/d;}

I hope this helps.

bakunin
This definitely helps. I like it very much when someone takes the time to explain the solution, so that it can be applied elsewhere with ease. Thanks a lot!
# 9  
Old 08-05-2012
Quote:
This definitely helps. I like it very much when someone takes the time to explain the solution, so that it can be applied elsewhere with ease. Thanks a lot!
You are welcome. In fact, as you can see below, i have learned from this as much, which is why i greatly enjoy it to come here.

First off, i think we have a confusion about the terms "function" and "command". For me, a "command" is a single command, like the "s" (substitute) or the "y", along with everything which is needed to execute it. "s/x/y/p" would be an example for such a command. "function" is what below is called "block of commands". This just to better explain my previous posts.

But probably we should agree to use "commands" and "block of commands" like in the sed man page.

Quote:
Originally Posted by Don Cragun
Check your local sed(1) man page...
My (GNU-sed) man page says the following:

Quote:
Code:
Commands which accept address ranges
       {      Begin a block of commands (end with a }).

This means, IMHO, that

Code:
/regexp/[,/regexp2/] { ...... }

is a correct command, because the command is "{ .... }" (="block of commands"). As there is no exception given which forbids "{ ... }" to appear inside another block of commands it follows that this can be nested.

I suppose there will be some practical limit of such nesting but as far as the standard goes there is none AFAICS. I haven't checked the AIX man page yet, but will do so as soon as i get back to the office.

Btw.: this is a very interesting discussion and i am glad i still can learn something new every time i come here.

bakunin

Last edited by bakunin; 08-05-2012 at 01:12 PM..
# 10  
Old 08-05-2012
Tools

Quote:
Originally Posted by bakunin
You are welcome. In fact, as you can see below, i have learned from this as much, which is why i greatly enjoy it to come here.

First off, i think we have a confusion about the terms "function" and "command". For me, a "command" is a single command, like the "s" (substitute) or the "y", along with everything which is needed to execute it. "s/x/y/p" would be an example for such a command. "function" is what below is called "block of commands". This just to better explain my previous posts.

But probably we should agree to use "commands" and "block of commands" like in the sed man page.



My (GNU-sed) man page says the following:



This means, IMHO, that

Code:
/regexp/[,/regexp2/] { ...... }

is a correct command, because the command is "{ .... }" (="block of commands"). As there is no exception given which forbids "{ ... }" to appear inside another block of commands it follows that this can be nested.

I suppose there will be some practical limit of such nesting but as far as the standard goes there is none AFAICS. I haven't checked the AIX man page yet, but will do so as soon as i get back to the office.

Btw.: this is a very interesting discussion and i am glad i still can learn something new every time i come here.

bakunin
I take it back. Looking at the sed(1) man pages provided in this site's Man Pages section, the OpenSolaris 2009.06 and Linux collection's sed(1) man pages describe
Code:
{ .... }

in a list of command that takes two addresses as described above by bakunin. However, the FreeBSD 8.0, OSX 10.6.2, and POSIX man pages describe it as I described in message #7 in this thread.

In both sets of descriptions:
Code:
s/x/y/

is a command. In POSIX it is also a function. In POSIX, you have functions inside {...}; not commands and the grammar for a function doesn't include any addresses.

I'm going to submit a request to The Austin Group to update the description of awk in a future version of the POSIX standard and the Single UNIX Specification.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

2. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

3. UNIX for Dummies Questions & Answers

Sed: Adding new line after matching pattern

Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. After the line Wf... (5 Replies)
Discussion started by: MIA651
5 Replies

4. Shell Programming and Scripting

Pattern Matching and text deletion using VI

Can someone please assist me, I'm trying to get vi to remove all the occurences of the text in a file i.e. "DEVICE=/dev/mt??". The "??" represents a number variable. Is there a globel search and delete command that I can use? Thank You in Advance. (3 Replies)
Discussion started by: roadrunner
3 Replies

5. Shell Programming and Scripting

How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks. If seeing a string (e.g., TODAY) in the line, replace a string in the line above (e.g, replace "Raining" with "Sunny") and replace a string in the line below (e.g., replace "Reading" with... (7 Replies)
Discussion started by: sprinner
7 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

8. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

9. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

10. UNIX for Dummies Questions & Answers

vi part-pattern matching and deletion

Hi, I have a log file which shows the files which has been changed over the last week. They follow this pattern: old_file_version_number@@new_file_version_number Now I need to know how to delete from each line parts starting from @@. I would be issuing the command inside vi(m). So... (5 Replies)
Discussion started by: vino
5 Replies
Login or Register to Ask a Question