Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Sed only digits not in numbers

Shell Programming and Scripting


Closed Linux or Unix Question    
 
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 03: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 Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 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: 25 May 2015, 12:57 PM EDT
Location: Saskatchewan
Posts: 20,654
Thanks: 894
Thanked 3,653 Times in 3,416 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 Linux or Unix Question

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 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 01:31 PM.