Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Sed only digits not in numbers

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 04:13 PM.. Reason: Code tags!
Sponsored Links
    #2  
Old Unix and Linux 10-12-2011
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 16 December 2015, 5:59 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 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 Unix and Linux 10-12-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 December 2016, 5:18 PM EST
Location: Saskatchewan
Posts: 21,658
Thanks: 1,008
Thanked 3,980 Times in 3,697 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 Unix and Linux 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:54 PM
help: single digits inflated to 2 digits amadain Shell Programming and Scripting 2 01-13-2010 08:38 AM
read numbers from file and output which numbers belongs to which range thepurple Shell Programming and Scripting 6 05-13-2009 07:07 AM
total last digits uwork72 Shell Programming and Scripting 3 04-16-2009 12:28 PM
How to cut last 10 digits off psarava Shell Programming and Scripting 4 08-29-2006 04:52 AM



All times are GMT -4. The time now is 08:15 PM.