[SOLVED] bash remove random text from command output
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.
BW-userx have a look at the solution posted by allend
Quote:
Not the bash solution requested, but maybe pipe the command output to sed
Since sed is more powerful for text processing there is no need to apologize.
Code:
<your command> | sed -E 's/^(.*:.{5}).*([[:digit:]]{8}).*/\1\2/'
This sed one liner does exactly what the OP requested which was extract the time at the start of the line and the 8-digit number and discard everything else.
Code:
$ echo 8:15am random text here 99629360 random text here | sed -E 's/^(.*:.{5}).*([[:digit:]]{8}).*/\1\2/'
8:15am 99629360
allend understood the problem fully and did in one line what you failed to do in five very long and sometimes angry posts with convoluted scripts.
Quote:
and he OP clearly stated, "I need to remove all random text from the second column."
yes one then needs to guess what the second column really is, seeing that the keys words used here is random text, and second column , that is where I'd start, on the second random and remove all of it from there, because he too use the word ALL in the start of his sentence.
the use of the words "remove all" means what in conjunction with the rest of the sentence "random text from the second column"?
the guessing part is, is it is really random text?
If it is to be what you are trying to imply then a less confusing sentence for you would then be to say. "I need to just remove the second worand he OP clearly stated, "I need to remove all random text from the second column."
yes one then needs to guess what the second column really is, seeing that the keys words used here is random text, and second column , that is where I'd start, on the second random and remove all of it from there, because he too use the word ALL in the start of his sentence.
the use of the words "remove all" means what in conjunction with the rest of the sentence "random text from the second column"?
the guessing part is, is it is really random text?
If it is to be what you are trying to imply then a less confusing sentence for you would then be to say. "I need to just remove the second word random from the string." Which is no doubt more explicit.d random from the string." Which is no doubt more explicit.
Last edited by tofino_surfer; 06-04-2019 at 03:31 PM.
BW-userx have a look at the solution posted by allend
Since sed is more powerful for text processing there is no need to apologize.
Code:
<your command> | sed -E 's/^(.*:.{5}).*([[:digit:]]{8}).*/\1\2/'
allend understood the problem fully and did in one line what you failed to do in five very long and sometimes angry posts with convoluted scripts.
This sed one liner does exactly what the OP requested.
and this quip at me with the use of someone elses work no less, a different solution then mine or others, after many have already tried to basically understand what OP is actually saying, is because why?
a good program is one that works, so its no real sweat off my @(#@s
You really need to properly work on your self esteem.
and this quip at me with the use of someone elses work no less, a different solution then mine or others, after many have already tried to basically understand what OP is actually saying, is because why?
Everyone else did seem to understand what the OP was saying.
Quote:
if the OP actually means just remove the second word random from the string, then he or she really needs to work on there English more as well as Linux anything
If there are digits in random text then it will break. I consider allend's 'sed' solution as more robust, if the second number always consists of exactly eight digits. It is an easy fix, though, if it does not.
I am posting this solution mainly because I like its simplicity - no RegEx backreferencing and no convoluted loops, KISS principle.
As the OP has not returned and we are now playing code golf, I will say that I was expecting to be slapped for my sed, as it will fail if the string represented by "random text here" contains a colon character, due to the greedy nature of matching in sed.
A better solution that tightens the regular expression to match the time
Code:
sed -E "s/^([0-9]+:[0-5][0-9][ap]m).*([0-9]{8}).*/\1 \2/"
Getting the quick answer is nice, but have you tried playing around with sed, awk, (regular expressions') regex? there is a vast amount of information to help you learn as well.
if you look at that link it should help you figure out that sed command ..
sed is shorthand for stream editor. It operates on the stream from command output.
The -E option calls for interpretation of extended regular expressions. The s/<what to match>/<what to replace>/ construction is the sed substitute option.
You wanted a match on eight consecutive digit characters, so [[:digit:]]{8}). This can also be written [0-9]{8} as suggested by syg00.
You also wanted a match on the time at the start of a line, so ^, the anchor for the start of a line, then .*:.{5} to match 0 or more characters followed by a colon followed by five more characters. The parentheses around the expressions tells sed to keep those as back references.
The back references are used in the <what to replace> part of the sed substitute option, hence the \1 (first back reference) and \2 (second back reference).
The ' characters protect the sed substitute option from interpretation by the shell where the command is run.
'info sed' will provide a more complete explanation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.