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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression 1

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
US
Reading mail, I cannot get a regular expression to parse:
Code:
>U  1 root              Fri May 28 10:40  14/563
 U  2 root              Mon Oct 18 13:01  11/311
 U  3 root              Mon Oct 18 13:05  11/311
 U  4 root              Mon Oct 18 13:05  11/311

This doesn't work:
Code:
sed -n 's/^[>A-Z][  ]\(\d\)[ ]\(.*\)\(\s+\)\(.*\)[ ]\(.*\)[ ]\(.*\)[ ]\(.*\)[  ]\(.*\)/\1/p'

Thanks
 
Well, this is the awk forum... but since sed is closely related I guess I'll let that slide. :)

This should work:

Code:
sed -n 's/^[> ][A-Z]  \([0-9][0-9]*\) \(.*\)  *\(.*\) \(.*\) \(.*\) \(.*\)  \(.*\)$/\1/p'

[ul][li]You had too many square brackets where they weren't required. [ ] does not match 2 spaces, it matches just one of the characters between the brackets, so it will only match one space. Similarly [>A-Z] will only match one character in total, so it would never match the lines that begin with a space.[/li]
[li]Most sed implementations don't understand Extended Regular Expressions, so they don't know about + for one-or-more repetitions.[/li]
[li]Most sed implementations don't understand the GNU \s and \d character classes (are you using GNU sed here?)[/li]
[li]There is no need to surround subsequent expressions in \( ... \) if you do not plan to capture them in the replacement.[/li]
[/ul]

I think awk '/^[> ][A-Z] [[:digit:]]+/{print $2}' would be a simpler solution anyway.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top