I'm sorry, I have problems with English, I can not.
If i may try?
This sed-program consists of two statements which are applied one after the other to every line:
Let us start with the second one as it is easier: it is a "replacement" command and replaces one expression with another. Actually the "s" stands for "substitute":
What does it replace? It replaces a start-of-line (^) followed by a newline character (\n) with nothing. The start-of-line is not really a character, so effectively it deletes a newline character, should it follow a line start but no other newline characters.
The first line is a bit more complicated: basically it is a replacement command too and works the same way as the second line. Now, what does it replace?
This matches one of the strings separated by the escaped pipe-characters, so effectively it matches either "ABC" or "XYZ" or "KMN". Now, what will these strings be replaced with?
The first is a \n, which means a newline character. The second character, &, means what has been matched before. As i said the first expression will match one of three different strings. The string which was matched in the first expression is put here so effectively it replaces the string with itself plus a newline character up front.
The final g is just an option and says that the operation should occur as often as possible and not only for the first opportunity. If you have a substitution command like:
It will replace "a" with "b" but only the first occurence of "a". An input string of "aaa" will become "baa", but with the "g" in place it will become "bbb" because all the "a"s will be replaced, not only the first one. So, to put it all together, this is waht will happen to an input string:
Quote:
Originally Posted by nezabudka
Notice that the use of Extended Regular Expressions as well as the usage of "\n" as a newline character is not covered by a standard-conforming sed.
There are several (similar but not identical) regular expression engines used in UNIX/Linux:
The most basic "regular expressions" although they are usually called "file globs" are used by the shell: i.e. the expression filename* where "*" is expanded to any string of any length is an example of this regexp syntax.
Then there are Basic Regular Expressions or "BRE"s. The syntax of BREs is standardized by POSIX and is used in utilities like sed, grep (in its default mode, see below) and so on.
Notice that the GNU project deviated from this standard and developed their own variant of BREs, the GNU Basic Regular Expressions. The GNU variants of sed, grep and so on use these instead of the POSIX BREs. One example for the difference between the GNU-BREs and POSIX-BREs is the quantifier "+", which means "one or more (of the previous expression". For instance, the regexp:
will match "XaY", "XaaY" and so on, but also "XY". To exclude that latter and restrict the pattern to one or more "a" you would need to write
Notice that the two POSIX variants are understood by all regexp engines, the GNU variant is understood only by GNU-tools.
Then there are Extended Regular Expression or EREs. EREs are basically a superset of BREs but with a few quirks. For instance you do not escape grouping or numerical quantifiers:
There is a POSIX standard for these and they are used in utilities like awk, grep -E (the -E option switches the used regexp engine from BRE to ERE), egrep (this is basically a grep with the -E option set and fixed) and so on.
Again, GNU has its own variant of ERE called GNU-ERE and used in the respective GNU variants of GNU-awk, GNU-egrep, etc. but also GNU-sed when used with the "-E" or the equivalent "-r"-switch.
I hope this helps.
bakunin
These 2 Users Gave Thanks to bakunin For This Post:
I need to split the file contents with multiple rows based on patterns
Sample:
Input:
ABC101testXYZ102UKMNO1092testing
ABC999testKMNValid
Output:
ABC101test
XYZ102U
KMN1092testing
ABC999test
KMNValid
In this ABC , XYZ and KMN are patterns
Continue here./mod]
Please read forum... (1 Reply)
I am starting to write a multi-line awk and using the file below which is
tab-delimited, print only the line with oncomineGeneClass
and oncomineVariantClass and PASS. The script execute but
seems to be printing the entire file, not the desired line. Thank you :).
file
... (8 Replies)
Dear All,
I have the following file tabulated:
ID distanceTSS score
8434 571269 10
10122 393912 9
7652 6 10
4863 1451 9
8419 39 2
9363 564 21
9333 7714 22
9638 8334 9
1638 1231 11
10701 918 1000
6587 32056 111
What I would like to do is the following, create 100 new files based... (5 Replies)
Hello!
I'm trying to process a text file and am stuck at 2 extractions. Hoping someone can help me here:
1. Given a line in a text file and given a keyword, how can I extract the word preceeding the keyword using a shell command/script?
For example: Given a keyword "world" in the line: ... (2 Replies)
Arun kumar something somehting Enterting in to the line
.
.
.
.
Some text text Finshing the sentence
Some other text
.
.
.
.
Again something somehting Enterting in to the line
.
.
.
.
.
.
Again text text Finshing the sentence (6 Replies)
Hi Guys,
I have the following problem. I have original file (org.txt) that looks like this
module v_1(.....)
//arbitrary number of text lines
endmodule
module v_2(....)
//arbitrary number of text lines
endmodule
module v_3(...)
//arbitrary number of text lines
endmodule
module... (6 Replies)
the thing which i require is very very complex.. i tried hard to find the solution but couldnt..
the thing i need to achieve is say i have a file
cat delta.sql
CREATE VIEW Austin
Etc etc
.
.
.
CREATE VIEW Barabara
AS
SELECT blah blah blah
FROM Austin z, Cluster s, Instance i
WHERE... (4 Replies)
I have an application that runs on the server with root privileges and all emails it sends get sent to root (errors, logs, etc), when they should actually go to one of application admins.
I would like to separate these emails from the OS related one sent to root and forward them to that... (2 Replies)
Hi Friends,
I have to create a new log file everyday and append it with content based on some keywords found in another log file.
Here is what I have tried so far...
grep Error /parentfolder/someLogFile.log >> /parentfolder
/Archive/"testlogfile_error_`date '+%d%m%y'`.txt"
grep error... (6 Replies)
Hello everyone,
I am trying to write a script that will capture few lines from a text file based on 2 keywords in the first line and 1 keyword in the last one. It could also be based on the first line only + the folllowing 3 lines.
Could some one help or give directions. Thanks. (4 Replies)