LinuxQuestions.org
Register a domain and help support LQ
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 10-28-2011, 05:35 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 54

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!

Best,

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

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
You can use GNU sed to replace the patterns (one digit, two digits, three digits starting with one) like this:
Code:
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):
Code:
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, 06:17 AM   #3
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Hi,

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

Thank you very much, N.A.!

Have a nice day.

Udiubu
 
  


Reply


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


All times are GMT -5. The time now is 10:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration