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 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 |
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? |
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. |
something a simple as cut works.
Code:
#!/bin/bash Code:
7:00pm random text here 99711372 random text here |
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 this works
Code:
sed 's/[^0-9]*//g' |
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 |
Quote:
|
Quote:
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. |
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 |
Quote:
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 Code:
$ sed 's@random@@2' data 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 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. |
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. |
Quote:
example #1 Code:
#before this is what you want? or this example #2 Code:
#before |
the correct is example 1
|
Quote:
Code:
#!/bin/bash |
All times are GMT -5. The time now is 06:15 PM. |