Sed only digits not in numbers | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Sed only digits not in numbers

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-12-2011
jejeking jejeking is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 13 March 2012, 9:23 AM EDT
Location: Ashburn VA
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Sed only digits not in numbers

Hi,

I have a text file with an array of numbers such as :


Code:
123    1    456   45   9817   1   45

I would like to replace the digit "1" in a text file with "A". So it looks like this:

Code:
123   A   456   45   9817  A  45

If I use sed 's/1/A/g' , I get


Code:
A23   A   456  45  98A7  A 45

I tried several combination such as [ 1 ] and others, to no avail.
What do I need to add to get the wanted result?
Thanks a lot!

Last edited by radoulov; 10-12-2011 at 03:13 PM.. Reason: Code tags!
Sponsored Links
    #2  
Old 10-12-2011
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 2 September 2014, 11:14 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts
Depending on your sed version:


Code:
sed 's/\b1\b/A/g' infile

or:


Code:
sed 's/\<1\>/A/g' infile

If none of them works:


Code:
perl -pe's/\b1\b/A/g' infile

The Following User Says Thank You to radoulov For This Useful Post:
jejeking (10-14-2011)
Sponsored Links
    #3  
Old 10-12-2011
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 September 2014, 2:46 PM EDT
Location: Saskatchewan
Posts: 19,273
Thanks: 774
Thanked 3,237 Times in 3,035 Posts
also possible with awk. Check each record for the string "1" and replace with "A". Won't replace inside a record.


Code:
awk '{ 'for(N=1; N<=NF; N++) if($N == "1") $N="A" } 1' < datafile

The Following User Says Thank You to Corona688 For This Useful Post:
jejeking (10-14-2011)
    #4  
Old 10-12-2011
jejeking jejeking is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 13 March 2012, 9:23 AM EDT
Location: Ashburn VA
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks Radoulov!

The first one worked!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
the smallest number from 90% of highest numbers from all numbers in file Apfik Shell Programming and Scripting 11 05-22-2011 10:54 PM
help: single digits inflated to 2 digits amadain Shell Programming and Scripting 2 01-13-2010 07:38 AM
read numbers from file and output which numbers belongs to which range thepurple Shell Programming and Scripting 6 05-13-2009 06:07 AM
total last digits uwork72 Shell Programming and Scripting 3 04-16-2009 11:28 AM
How to cut last 10 digits off psarava Shell Programming and Scripting 4 08-29-2006 03:52 AM



All times are GMT -4. The time now is 02:58 PM.