Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed help

Status
Not open for further replies.

gfunk123

IS-IT--Management
May 22, 2001
143
GB
sed -n '/[0-9]\{10\}E[1-2]/p' $filename | sort >> $filename.sorted

Could somebody please give me a breakdown of what exactly each part of this sed statement does, I have inherited a production script, and know that basically it sorts a file that looks like this

0304053456e17347983740284738495
6354053454e27347983770267638494
2304053456e17347983740284738495
2304053456e27347983740284738495

Notice the 31st/32nd character (including the 20 spaces) there is an e1 or e2) as far as I can determine, its taking all the e1's, putting them into a file called $filename.Sorted ($filename variable is created earlier in the script) Then taking all the e2's and appending them to the end of that file. .In the above example the destination file looks like this

0304053456e17347983740284738495
2304053456e17347983740284738495
6354053454e27347983770267638494
2304053456e27347983740284738495

Note the e1's are at the top and all e2,s are at the bottom. Im specifically interested in the first portion "sed -n '/[0-9]\{10\}e[1-2]/p' "

Any help on this would be greatly appreciated
 
-n switch output OFF
[0-9] look for a digit
\{10\} the digit has to be repeated 10 times
e[1-2] followed by e1 or e2
p print if found


------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Thanks

I have 2 further questions if you dont mind

a) how can I incorporate a space into the digit section 0-9, the reason i am doing this is that we have discovered some entries that have numbers and spaces before the e1/e2. eg

03040534 e17347983740284738495


2) how does it run all the way through the document taking the e1s first and then the e2s on the second bypass if the 'e[1-2]' bit means it can be either, hope this makes sense,

cheers
 
a) To allow spaces in the preceeding set of numerics, put a space into [0-9] reg. exp. for [0-9 ].

b) If you're asking to sort everything, but have the e1's done first (and sorted) followed by e2's (sorted) then this should work:

sed -n '/\([0-9 ]\{10\}\)\(e[12]\)/{
s//\2\1/
p
}' $filename |
sort -n |
sed 's/\(..\)\(.\{10\}\)/\2\1/' >> $filename.sorted

It just puts the e[12] segment first, sorts and then puts the segment back to its original position.

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
The sed commands just prints lines that match the pattern specified. It is the 'sort' command which organises the output alphabetically (so e1 precedes e2). If you try the sed command on its own (without the 'sort > $filename') you will see that the output is in the same order as the original file.

A quick comment on the sed command:

[0-9]{10} match any line with 10 numbers (in range 0-9)
e match any line with 'e1' or 'e2'
[1-2] match any number in the range 1-2
/<xyz>/p print any line with a pattern matching <xyz>

So, your sed command says:

Print only those lines which have 10 numbers followed
by an 'e' character followed by a number in the range
1-2

(The -n instructs sed not to print anything else it finds
in the file, not actually required for this command)
 
First of all - The -n switch IS neccessary. sed's normal activity for un-matched lines is to pass them on. If you do not use the -n switch to stop this, the instruction will output each line that did not match, and 2 copies of each line that did.

Second, the instruction as it is given here will NOT produce the output shown, even after changing the E to e!

The actual output of running the statement on the file sample would have the last two line reversed from what you gave. I.e. it sorts completely the output of the sed instruction.

Was the &quot;E&quot; in your instruction a typo or is the instruction exactly as you gave it? If the latter is true, and $filename does not have a capital E in it, then this statement does nothing at all! The contents of $filename.sorted are entirely the result of the rest of the program and not this line. That would explain why the &quot;e2&quot; lines are not sorted.
 

True about the -n switch, in my test code, I had to fix a couple of things, including the typo 'E' character, and my test data only carried the test lines.

The '-n' switch tells sed to print *only* matching lines. This must have been a bad day for me, I hang my head in shame.





 
roylec: a little attention
the opt '-n' say to sed DO NOT PRINT
the flag 'p' in matching pattern say to sed PRINT
depending of your sed version:
sed -fn sedcmd is equivalent to
sed -f sedcmd IF the first line of sedcmd is a '#n'
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top