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:
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
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:
or, in short form:
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!
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:
This means, IMHO, that
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.
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
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
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:
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.
'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)
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)
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)
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)
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)
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)
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)
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)
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)
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)