Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-28-2011, 04:35 AM   #1
Registered: Oct 2011
Posts: 73

Rep: Reputation: Disabled
Search and replace number less then 250

Dear all,

I have a txt file with numbers organized in lines and columns, as below:

23 456 9780
123 56879 45

What I would like to do is simply find all numbers below 200 and replace them with a dot "."

I guess I should use either sed or awk but still don't know how!

Suggestions are highly appreciated!


Old 10-28-2011, 04:51 AM   #2
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
You can use GNU sed to replace the patterns (one digit, two digits, three digits starting with one) like this:
sed -e 's|\b[0-9]\b|.|g; s|\b[1-9][0-9]\b|.|g; s|\b1[0-9][0-9]\b|.|g' -i file
The \b is a character class which means "word boundary"; it does not match any specific character, just the logical word boundary. It's very useful for something like this.

However, awk is usually a better choice for something like this, because it can do it numerically (floating point values, not just nonnegative integers like the regular expression patterns above):
awk '{ for (i = 1; i <= NF; i++)
           if ((1.0 * $i) == $i && $i < 200) $i = "."
       print $0 }' infile > outfile
In the awk code above, there is just one rule which is applied to each record (line). It has a loop over each input field. If the field value is numeric, and if its value is less than two hundred, the field is replaced with a dot.

The difference with the awk code is that any records with changed fields will have their whitespace consolidated and converted to a normal form. If you need to, you can of course work around that with a bit more complex code.
1 members found this post helpful.
Old 10-28-2011, 05:17 AM   #3
Registered: Oct 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled

That worked great!
(I just had to add a semicolumn befor print command).

Thank you very much, N.A.!

Have a nice day.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
search and replace oliverkazaam41 Linux - General 1 10-29-2009 02:15 AM
Vi Search and Replace mortonmorton Linux - Newbie 6 10-22-2009 09:09 AM
Search and replace TL_CLD Slackware 4 12-25-2007 01:45 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Search and replace links xtrude Linux - Newbie 4 01-29-2003 11:55 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:46 AM.

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