LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-04-2019, 10:59 AM   #16
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750

Quote:
I have a command output as shown below
Not the bash solution requested, but maybe pipe the command output to sed
Code:
<your command> | sed -E 's/^(.*:.{5}).*([[:digit:]]{8}).*/\1\2/'
 
Old 06-04-2019, 03:19 PM   #17
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
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.
 
Old 06-04-2019, 03:28 PM   #18
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by tofino_surfer View Post
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.

Last edited by BW-userx; 06-04-2019 at 03:30 PM.
 
Old 06-04-2019, 03:39 PM   #19
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
Quote:
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
Don't you mean their English ?
 
Old 06-04-2019, 03:43 PM   #20
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by tofino_surfer View Post
Everyone else did seem to understand what the OP was saying.



Don't you mean their English ?
now you're just trying to start an argument to prove yourself right and me wrong, to feed your ego.
puts you on permanent ignore.
 
Old 06-05-2019, 12:23 AM   #21
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Pure Bash solution

If your random text does not contain any digits then you can also use this:
Code:
$ shopt -s extglob
$ <your command>|while read line;do echo "${line// *([^0-9])/ }";done
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.
 
Old 06-05-2019, 03:38 AM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The following prints column #1 and the first column with a big number
Code:
awk '{
for (i=2; i<=NF && $i+0<=90000000; i++);
print $1,$i
}' inputfile
 
Old 06-05-2019, 10:58 AM   #23
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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/"
 
Old 06-05-2019, 11:00 AM   #24
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by allend View Post
Code:
<your command> | sed -E 's/^(.*:.{5}).*([[:digit:]]{8}).*/\1\2/'
Eventually I used this code. Thanks everyone for your help.

@allend if you have the time, it would be interesting to know the logic behind this code. I can only understand a few parts of it, but not everything.

Thanks
 
Old 06-05-2019, 11:03 AM   #25
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
Thanks for the better solution. The second column now became the 5th column. How can I change your code to represent this change?

Thanks
 
Old 06-05-2019, 11:04 AM   #26
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
I forgot to mention, now there is no am, pm in the output. It's all military time. So no need to account for that.
 
Old 06-05-2019, 11:09 AM   #27
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 ..
 
Old 06-05-2019, 11:41 AM   #28
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Quote:
sed -E 's/^(.*:.{5}).*([[:digit:]]{8}).*/\1\2/'
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.
 
1 members found this post helpful.
Old 06-05-2019, 11:46 AM   #29
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
Thank you very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrote Shell in C - piping output from first command to second command, sending to output file issue KarenWest Programming 2 03-31-2016 05:27 PM
Bash Script - Supressing Command Output and Writing to Text File Nexusfactor Programming 4 07-03-2015 01:22 AM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration