How to insert period after each number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert period after each number?
# 1  
Old 10-16-2012
How to insert period after each number?

stupid question: trying to use sed to do the following...

Code:
$ echo '12345' | sed 's/./&./g'
1.2.3.4.5.

needed this instead: 1.2.3.4.5 but how? please advise

Last edited by ux4me; 10-16-2012 at 07:25 AM..
# 2  
Old 10-16-2012
Like this?
Code:
echo "12345" | sed 's/./&./g;s/\.$//'
1.2.3.4.5


Last edited by elixir_sinari; 10-16-2012 at 07:30 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-16-2012
Try like..
Code:
echo "12345" |sed 's/./&./g;s/.$//'

This User Gave Thanks to bmk For This Post:
# 4  
Old 10-16-2012
thanks.. got it working using either:

Code:
$ echo '12345' | sed "s/./&./g;s/\.$//"
1.2.3.4.5

$ echo '12345' | sed "s/./&./g;s/.$//"
1.2.3.4.5

# 5  
Old 10-16-2012
One more
Code:
echo "12345" | awk -F "" ' {for(i=1;i<=NF;i++){printf("%s%s",$i,i%1?"":".")}}'|awk '{sub(/.$/,"")};1'

# 6  
Old 10-16-2012
Some awks can do this:
Code:
echo "12345" | awk '$1=$1' FS= OFS=.

# 7  
Old 10-16-2012
Slight modification to Scrutinizer's solution to handle the case of strings starting with zero:
Code:
echo "12345"| awk '{$1=$1}1' FS= OFS=.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

2. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

3. UNIX for Dummies Questions & Answers

Replace period in a tab delimited file to a number

I have a file like this. It is tab delimited. Unfortunately, the missing data was filled in with a period "." (see the leading lines 1-5 columns) I want to substitute the periods for misisng data with an integer "-999". however, I do not want the global replace to change the other periods seen... (7 Replies)
Discussion started by: genehunter
7 Replies

4. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

5. Shell Programming and Scripting

Insert new line based on numerical number of column

My input file: Class Number Position Range 1 Initial 50 1 Initial 50 2 Terminal 150 2 Terminal 20 2 Single 10 3 Single 20 4 Double 50 5 Initial 50 5 Initial 60 Class Number... (11 Replies)
Discussion started by: patrick87
11 Replies

6. Shell Programming and Scripting

Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file. I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this. (4 Replies)
Discussion started by: pluto7777
4 Replies

7. Shell Programming and Scripting

How to insert the number to an array

Hi I work in ksh88 and have a file which has several "set -A " statements: set -A PNUM_LSTM 2713 4124 2635 270 2529 2259 2214 set -A PNUM_LSTM $* for pn in ${PNUM_LSTM} etc... I need to add another number, lets say 555, to the first line ONLY so only the the first line will be updated... (2 Replies)
Discussion started by: aoussenko
2 Replies

8. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

9. Shell Programming and Scripting

Insert comma based on max number of column

Hi, I am new to unix shell shell scripting. I have a specific requirement where I need to append comma's based on the max number of column in the file. Eg: If my source file look something like this, sengwa,china tom,america,northamerica smith,america walter My output file... (8 Replies)
Discussion started by: nicholas_ejn
8 Replies

10. UNIX for Dummies Questions & Answers

insert period at the end of each line

file1 contains: this is a test this is a test and only a test this is another test this is another test and only another only i'd like my file to look like this: this is a test. this is a test and only a test. this is another test. this is another test and only another only. (6 Replies)
Discussion started by: tjmannonline
6 Replies
Login or Register to Ask a Question