Sed only digits not in numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed only digits not in numbers
# 1  
Old 10-12-2011
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!
# 2  
Old 10-12-2011
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

This User Gave Thanks to radoulov For This Post:
# 3  
Old 10-12-2011
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

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-12-2011
Thanks Radoulov!

The first one worked!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Beginners Questions & Answers

Using sed or awk to replace digits in files

Hello; I am not good at file and stream editing. I need to replace a few digits in two files. The lines in files looks like this: Line in the first file, /dw300/data/obe/2019273.L800JR.1909.273 Line in second file, 1|2019273.L800JR.1909.273 I will write a function to connect to... (7 Replies)
Discussion started by: duke0001
7 Replies

3. UNIX for Dummies Questions & Answers

Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22" Not sure how. I could use if ]; then echo "yes";fi But I think that will only grab the pattern 22 and not the first 2 digits. (5 Replies)
Discussion started by: newbie2010
5 Replies

4. UNIX for Dummies Questions & Answers

Replacing digits using sed

How to replace a character followed by a digit using sed? For example lets say I have this file - a1 3242134 54235435 3241235 a2 3214345 45325626 3125435 a3 4236577 54365376 6865678 . . . a3000 5432534 32546546 3254365 I want to replace all... (6 Replies)
Discussion started by: saleheen
6 Replies

5. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

6. Shell Programming and Scripting

Regex - Return numbers of exactly 8 digits

Hi All I am new to this forum and also regex. I am using bash scripting and have a file like this "0012","efgh","12345678","adfdf", "36598745" "87654321","hijk","lmno" I want the ouput to be 12345678 36598745 87654321 Criteria like this - number - 8 carachters long Please let... (21 Replies)
Discussion started by: buttseire
21 Replies

7. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

8. Shell Programming and Scripting

stripping out digits from a string with sed

i want to parse a string and only display the digits in that string... How would i accomplish this with sed command. For example. input string: " 033434343 dafasdf" output string: 03343434 Thanks (2 Replies)
Discussion started by: timmylita
2 Replies

9. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

10. Shell Programming and Scripting

sed: removing any and all trailing digits?

We have a large number of oracle database related scripts that utilize the environment variables $ORACLE_SID and $DBNAME. In a single instance database the $ORACLE_SID is the same as the database name $DBNAME. So we have simply set DBNAME = $ORACLE_SID. However, now that we are clustering with RAC,... (5 Replies)
Discussion started by: Squeakygoose
5 Replies
Login or Register to Ask a Question