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.
I'd like a sed command to replace all decimal values greater than 0.5 with nothing in a tab delimited text file.
EX: Input text- 0.8765 0.301 0.5 0.11 (note, for simplicity these values are space delim)
This is what I'd like back- 0.301 0.11
There would be one tab before 0.301 and two tabs after it. This way when you paste the text into an excel spreadsheet, there are empty cells where you deleted values.
I thought the sed command would be something like:
sed s/0\.[5-9].*[[:space:]]//g
But this will delete everything.
What I need is something that will start deleting when it sees 0\.[5-9] and stop when it reaches a tab. I know you can use [^character]+ to do this, but it doesn't seem to work with a TAB as the character.
sed /0\.[5-9][^character]+//g
Another idea I had would be to have sed replace from 0.[5-9] to 0. and replace with a tab + 0.
But I also cant get [^0\.]+ to work as it only works with single characters.
Any ideas?
Reallly need this command for my work.
Thanks-
Your first request (replace all decimal values greater than 0.5 with nothing) and your example don't go together. There are no numbers larger then 0.5 (0.1 is biggest).
I actually just figured it out. My sed command is:
sed 's/0\.[5-9][0-9]*\([^0-9]\)/\1/g'
Only problem is that it doesn't replace decimal strings in the last field. It would be nice, but I think I can just paste a dummy column at the end, or add a tab or something.
Thanks for your help. Couldn't of figured this out otherwise.
if you have 0.55 0.25 0.501 0.95 0.55 delimited by tabs
the this will keep the tabs and eliminate the ones under .5 :
sed 's/0*\.[0-4][0-9]*//g' old file > newfile
I'm really new to unix/sed, so pardon my ignorance, but:
What does the |$ mean/ literally do?
And why did the backreference (/...)/ work before, but with the addition of |$ it doesn't and (...) must be used instead?
I'm really new to unix/sed, so pardon my ignorance, but:
What does the |$ mean/ literally do?
And why did the backreference (/...)/ work before, but with the addition of |$ it doesn't and (...) must be used instead?
The overall logic is to find the desired number pattern followed by any character that is:
not a number ..... [^0-9]
OR
the end of the line ..... $
The | is the "alternation operator"---a fancy way of saying "or". This operator works only with extended regexes, so I switched to that mode using the -r flag. Once I did this, then the backreference uses (...) instead of \(...\).
If you are now confused, you are a member of a large community....
I think that the version from flewis777 is actually good enough for the sample data we have seen so far (except that the logic is backwards---you want to delete the numbers that are 0.5 and larger)---i.e. as long as you don't have anything except numbers between the tabs, my version is overkill.
Further, there are things that the proposed regex solutions might not catch. What if you have say 09.155. That, theoretically is 9.155. Might not happen with your case, BUT nothing is impossible.
Last edited by ghostdog74; 12-13-2009 at 12:44 AM.
With all due respect, SED is an entirely appropriate solution for this. No doubt there are some arguments to favor AWK---especially if the problem is more generalized---but that does not make the SED solution incorrect.
In our motorcycle shop--almost 50 years ago--we had a mantra: "If it works, it's OK." Still true today---and will continue to be true for all time.
I would be hard-pressed to think of a computing problem that has only one solution.......
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.