LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-03-2019, 03:19 PM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
bash remove random text from command output


I have a command output as shown below

Code:
8:15am  random text here 99629360 random text here
9:00am  random text here 99799779 random text here
10:00am  random text here 99102831 random text here
11:45am  random text here 99629320 random text here
12:30pm  random text here 96678497 random text here
2:30pm  random text here 99762314 random text here
3:00pm  random text here 99833711 random text here
5:15pm  random text here 99305212 random text here
6:00pm  random text here 96500528 random text here
7:00pm  random text here 99711372 random text here
This is 2 columns. One shows a time and the other some random text and some random numbers.

I need to remove all random text from the second column. I need to be left with the first column showing times and the second column showing only numbers. No random text.

How can I do this?

Thanks
 
Old 06-03-2019, 05:17 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,591

Rep: Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689Reputation: 2689
Using BASH and the cut comment? (man cut)
There is also some pattern matching ability within bash that might serve. (man ash)

Or, if you want to combine your bash with something other than cut or the internal pattern matching and string handling:
With a simple PERL script?

I would not use AWK/NAWK, but only becauseI like PERL better. IT is certainly up to the task.

What exactly are your limits, and what are the characteristics of the text you are calling random?
I seriously doubt if the text is random, as generating truly random text would be seriously challenging.

Does any of the random text that you want stripped contain digits? If not, there is a clue as to how to craft your pattern matching to pick out the desired fields for display!

What exactly have you tried so far? How did that work for you?
 
Old 06-03-2019, 06:09 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Add sed to the list. If feeling particularly masochistic, grep with perlre ...

The trick is to use regex to keep what you want, rather than go through contortions trying to define the (multiple) "random text" to delete.
 
Old 06-03-2019, 06:26 PM   #4
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
something a simple as cut works.
Code:
#!/bin/bash

while read -r f
do
echo $f
echo $(echo "$f" |  cut -d" " -f1-6)
done < data
considering it is all like data.
Code:
7:00pm random text here 99711372 random text here
7:00pm random text here 99711372
if that is what you are looking for.

Last edited by BW-userx; 06-03-2019 at 06:29 PM.
 
Old 06-03-2019, 10:59 PM   #5
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
ok, lets not focus on removing the random text then.
The similarities in the numbers are that they all start with 99, 96 or 95.
Would this help in keeping just the numbers, instead of focusing on removing the random text?
Also the numbers are always 8 digits.

Last edited by aristosv; 06-03-2019 at 11:00 PM.
 
Old 06-03-2019, 11:03 PM   #6
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
So this works

Code:
sed 's/[^0-9]*//g'
But I need to apply it only to the second column
 
Old 06-03-2019, 11:26 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
As I say, concentrate on what you need, not what you don't need. How good are your regex skills - do you understand back references ? For
example this will select only 8 consecutive digits - the same regex will work in sed.
Code:
grep -Eo "[0-9]{8}" your.file
 
Old 06-04-2019, 12:02 AM   #8
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
Quote:
something a simple as cut works.
#!/bin/bash

while read -r f
do
echo $f
echo $(echo "$f" | cut -d" " -f1-6)
done < data

considering it is all like data.
7:00pm random text here 99711372 random text here
7:00pm random text here 99711372
I highly doubt that the random text referred to by the OP are the three actual words "random text here" which wouldn't really be random. The number of fields in actual random text is unknown.
 
Old 06-04-2019, 06:41 AM   #9
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
I highly doubt that the random text referred to by the OP are the three actual words "random text here" which wouldn't really be random. The number of fields in actual random text is unknown.
do you always take everything out of context to try and prove a point, not paying attention to detail, or even trying to?

You should not take what I did out of context, (or any one for that matter) to try and prove your point, that is being dishonest by obscuring the truth of the matter. You removed my final statement, and the beginning of the OP's statement to try and prove your point. my final statement, "if that is what you are looking for." Which clearly means what?

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 word random from the string." Which is no doubt more explicit.

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. Which that part is conjecture and the person in question is not here to comment on this.

Last edited by BW-userx; 06-04-2019 at 07:04 AM.
 
Old 06-04-2019, 06:50 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I tend to be a reluctant user of "pure" bash, but further testing reveals its regex matching and BASH_REMATCH[] actually works very well here. Without the need for any external program like sed/awk/perl ...

Off the top of my head, here is one solution I came up with.
Code:
while read line ; do if [[ $line =~ ^([^ ]+)[[:space:]].*[[:space:]]([0-9]+)[[:space:]].* ]] ; then echo ${BASH_REMATCH[1]}" "${BASH_REMATCH[2}} ; fi ; done < your.file
 
Old 06-04-2019, 07:06 AM   #11
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 aristosv View Post
ok, lets not focus on removing the random text then.
The similarities in the numbers are that they all start with 99, 96 or 95.
Would this help in keeping just the numbers, instead of focusing on removing the random text?
Also the numbers are always 8 digits.
so you do not really need to remove the random text, but are needing to remove the eight digit numbers from the complete string?
Or are you now wanting to remove everything but the numbers?
Or you just need to remove everything after the 8 digit numbers?

as I stated that if your data is always the same then cut as I did will always work.


what is the actual criteria(s) that someone might have told you that you need to do to complete this task?

Verbatim please.


--- mod: now seeing what others have (just?) done as I was posing this. --- let me go see what they think you are now trying to says.

that BASH_REMATCH

gets this with me
Code:
$ ./bashremove
./bashremove: line 1: ${BASH_REMATCH[1]}" "${BASH_REMATCH[2}}: bad substitution
this gets the second occurrence of the WORD random removed from the strings.
Code:
$ sed 's@random@@2' data
8:15am  random text here 99629360  text here
9:00am  random text here 99799779  text here
10:00am  random text here 99102831  text here
11:45am  random text here 99629320  text here
12:30pm  random text here 96678497  text here
2:30pm  random text here 99762314  text here
3:00pm  random text here 99833711  text here
5:15pm  random text here 99305212  text here
6:00pm  random text here 96500528  text here
7:00pm  random text here 99711372  text here
again what exactly is the results you are looking for?
just keeping the 8 digit numbers, or now removing from the start of the 8 digits, and where exaclly is the second column starting?

Code:
                 column
roll 0  1        2     3   4     5        6      7    8
     1 7:00pm  random text here 99711372  random text here
     2
is that a correct assessment?

try showing us a final product you are looking for so we all can then know what to figure out in how to get that. as a picture speaks a thousands words.

Last edited by BW-userx; 06-04-2019 at 07:50 AM.
 
Old 06-04-2019, 08:25 AM   #12
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
I really didn't mean to cause a whole discussion for this. I though my question was clear, but it seems I was wrong. When I said "random text here" I didn't actually mean that the words "random text here" were in the command output. Random text could be anything. It could be a name, place or food. Sorry if I wasn't clear.

The point is, like I originally said, I need to be left with the first column showing the times and the second column showing only numbers. So no need to touch the first column. I just want to modify the second column so that it only shows the numbers. I have to remove all the text, only from the second column.
 
Old 06-04-2019, 08:30 AM   #13
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 aristosv View Post
I really didn't mean to cause a whole discussion for this. I though my question was clear, but it seems I was wrong. When I said "random text here" I didn't actually mean that the words "random text here" were in the command output. Random text could be anything. It could be a name, place or food. Sorry if I wasn't clear.

The point is, like I originally said, I need to be left with the first column showing the times and the second column showing only numbers. So no need to touch the first column. I just want to modify the second column so that it only shows the numbers. I have to remove all the text, only from the second column.
this is how you show an example of what you're looking for.
example #1
Code:
#before
7:00pm  random text here 99711372 random text here
#after
7:00pm 99711372
removing all random text within the string.

this is what you want?
or this
example #2
Code:
#before
7:00pm  random text here 99711372 random text here
#after
7:00pm 99711372  random text here

Last edited by BW-userx; 06-04-2019 at 08:59 AM.
 
Old 06-04-2019, 08:40 AM   #14
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
the correct is example 1
 
Old 06-04-2019, 09:09 AM   #15
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 aristosv View Post
the correct is example 1
run this and see what you think
Code:
#!/bin/bash

#array of strings

data=(
"8:15am  random text here 99629360 random text here"
"9:00am  random text here 99799779 random text here"
"10:00am  random text here 99102831 random text here"
"11:45am  random text here 99629320 random text here"
"12:30pm  random text here 96678497 random text here"
"2:30pm  random text here 99762314 random text here"
"3:00pm  random text here 99833711 random text here"
"5:15pm  random text here 99305212 random text here"
"6:00pm  random text here 96500528 random text here"
)


for ((i=0;i<${#data[@]};i++))
do


 part1=$( echo ${data[$i]} | sed 's/[A-Za-z]*//g' | fmt -u )
 part2=$( echo ${data[$i]} | sed 's/[A-Za-z]*//g' |  awk '{print $1 " " $2 " " $5}' )
 
 echo "p1 $part1"
 echo "p2 $part2"
 echo
#split the string to keep the am or pm on the leading part of string.

part3=${data[$i]%% *}
part4=$(echo ${data[$i]} | sed 's/[^0-9]*//g')
echo
echo "p3 $part3"
echo "p4 $part4"
echo "
final product is:
$part3 $part4
"

done

Last edited by BW-userx; 06-04-2019 at 09:19 AM.
 
  


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 05:53 AM.

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