adding zeroes to all single digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding zeroes to all single digits
# 1  
Old 06-15-2008
adding zeroes to all single digits

I have got a file that contains both single and double digits like this
10 3 48 39 20 6 8
and i want to add zeros in front of every single digit to become double digits like this
10 03 48 39 20 06 08

I tried using Sed
sed 's/\([0-9]\)/0&/g' filename
or
sed 's/\([0-9]\)*/0&/g' filename
but i ended up have zeros added to every thing in the file can someone pls help me
thank you for you help
# 2  
Old 06-15-2008
Code:
awk '{ for (i=1; i<=NF; i++) $i=sprintf("%02d", $i); print; }' filename

# 3  
Old 06-15-2008
thank you
your help was great
# 4  
Old 06-16-2008
another way, if you still want to work with sed:
Code:
cat filename | tr ' ' '\n' | sed -e 's/^\([0-9]\)$/0\1/' | tr '\n' ' '

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. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. Shell Programming and Scripting

Adding single value from one file into column of second file

I have two files with the following content: file1.txt looks like this (it always only have one single row): ABC file.txt looks like this (may have tons of rows): 123 456 789 I want to make a bash script that gives the following output: ABC 123 ABC 456 ABC 789 So it takes the... (9 Replies)
Discussion started by: Zooma
9 Replies

4. Shell Programming and Scripting

Adding content of two file in a single file column wise

Hi, I am trying to get the file in particular pattern using shell script. I have to add one column to some other file. For example consider two file as below. File1: name1 name2 name3 File2: Add1 age1 Add2 age2 Add3 age3 I want this two file in a single file format something like... (3 Replies)
Discussion started by: diehard
3 Replies

5. Shell Programming and Scripting

Bash shell adding extra single quotes

AIX 6.1 bash shell #!/bin/bash -x STATEMENT="cvs commit -m \"This is\" ../PBP/EIR.ENTRY" echo $STATEMENT exit 0 This is the output + STATEMENT='cvs commit -m "This is" ../PBP/EIR.ENTRY' + echo cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY cvs commit -m "This is" ../PBP/EIR.ENTRY + exit... (26 Replies)
Discussion started by: hpodhrad
26 Replies

6. 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

7. 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

8. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

9. Shell Programming and Scripting

adding single word to multiple line.

I have following problem. <File A> contains let say 5 lines but can be changed. cat dog fish car if I want to add word to each line then how do I go about it? I used paste -d but that requires two files having same number of lines but in my case <File A> changes and I just need to... (6 Replies)
Discussion started by: paulds
6 Replies

10. Solaris

adding a user in single user mode

Just got a solaris 8 blade 150 box with no users, only a root account. no one seems to know the password. I'd like to add one user. So I booted into single user mode via cdrom and added one. Can't seem to login using the new account, though. Here's what I'm using: # useradd -d /tmp/"user" -m... (1 Reply)
Discussion started by: ECBROWN
1 Replies
Login or Register to Ask a Question