Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If you want to hack and trim use egrep with a posix or perl regex with appropriate switches. YMMV
man regex - posuix REs
man pcre - perl REs (more powerful imho).
ok, let's try to explain, but first, here is the man page of the command sed: http://linux.die.net/man/1/sed
you can find the sed script between the ' chars.
/status/ means I want to search for lines containing the text status.
in {} there are two commands to execute on the current line (which should now contain the text status).
The first command is the s, means substitute, the syntax is: s/search text/replace text/. ^ is the beginning of the line, .* means anything, \( and \) means grouping, . means any char and finally $ means end of line. The replacement string is \1 which means the first group found - the text between \( and \). So the full line will be replaced with the grouped text, and a \n is added.
The second command is p which means print the text.
The last command is a d which means I want to delete the line and go to the next one. It will be executed for every line, the /status/ search expression works only for the commands inside {}.
There's no need to make it that complicated. Just use the "-n" option to silence output by default, then add the "p" modifier directly to the substitute command. That way only lines that match will be printed.
You can also use the "-r" option to explicitly enable extended regex, so that there's no need to escape the parentheses.
Since the "." represents any character, we really don't want to use it when we actually want to match "/". Unfortunately, this is the default delimiter for the "s" command. However, sed allows you to use any ascii character as the delimiter, so just choose one that's not found in the expression itself. I prefer using "|" myself.
Next, the regex only really needs to contain enough of the string to ensure a unique match, and the starting and ending anchors are also superfluous here, as the regex assumes them. Of course, it doesn't hurt to leave them in either.
Finally, it's probably safer to replace the ".*" (a string of characters of any length) with "[^<]*" (a string of "not <" of any length), to avoid any possible issues with regex greediness.
Code:
sed -rn '/status/ s|.*status>([^<]*)</ns0:stat.*|\1|p' logfile
BTW, I don't see where the line-ending issue could be coming from. sed always appends a newline to each line of output anyway.
Yes, the line-break is an easy fix. But what I didn't understand in my last post was why it wasn't including one to start with. sed works by placing the line into the pattern buffer minus the newline that delimited it, performs it's edits on the buffer contents, then adds a newline back to the output. So I was thinking it always added a newline to the output.
Besides, I got a newline in all of my test runs.
I've figured it out now though.
The reason you didn't get one is because the line operated on is the last one in the file, and there's no final newline after it (pretty much the only place that could happen). So I guess sed only inserts a newline in the output if there was one in the input. Something new learned!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.