Inserting space between numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting space between numbers
# 1  
Old 12-18-2010
Inserting space between numbers

Are there any one-liners or short codes to separate 4-digit numbers into 2?

For example, input.txt:
Code:
1234
5678
3091
2851

Output.txt:
Code:
12 34
56 78
30 91
28 51


Last edited by Scott; 12-19-2010 at 02:54 AM..
# 2  
Old 12-18-2010
Try this

Code:
awk '{print substr($0,1,2)" "substr($0,3,4)}' myfile

# 3  
Old 12-18-2010
Thanks, but what if the input.txt file also includes words as well?

Input.txt:
Code:
7038 County Highway East, 1007 Hidden Valley.
6731 Street Del River, 5613 Stones Levee.
8995 Tuleburg Levee, 189 Road Del River.
6366 Hunters Glen, 2204 Fed Medical Center.

Output.txt:
Code:
70 38 County Highway East, 10 07 Hidden Valley.
67 31 Street Del River, 56 13 Stones Levee.
89 95 Tuleburg Levee, 189 Road Del River.
63 66 Hunters Glen, 22 04 Fed Medical Center.

Thanks for any feedback!

Last edited by Scott; 12-19-2010 at 02:54 AM..
# 4  
Old 12-19-2010
Hi.

Code:
$ sed "s/../& /" Input.txt
70 38 County Highway East, 1007 Hidden Valley.
67 31 Street Del River, 5613 Stones Levee.
89 95 Tuleburg Levee, 189 Road Del River.
63 66 Hunters Glen, 2204 Fed Medical Center.

# 5  
Old 12-20-2010
Hi scottn,

Thanks for the input, the sed line worked. However is there a way to also break up the 4-digit numbers after the comma as well? So the output would look like:

70 38 County Highway East, 10 07 Hidden Valley.
67 31 Street Del River, 56 13 Stones Levee.
# 6  
Old 12-20-2010
Code:
sed 's/[0-9][0-9]/& /g' myFile

# 7  
Old 12-20-2010
Thanks vgersh99, code worked perfectly. Is there a simple way to only perform the code on 4-digit numbers and ignore the 3-digit ones?

Output:
70 38 County Highway East, 107 Hidden Valley.
671 Street Del River, 56 13 Stones Levee.
89 95 Tuleburg Levee, 189 Road Del River.
636 Hunters Glen, 22 04 Fed Medical Center.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Remove space before numbers in delimited file

Hi, I have a file which looks like this FORD|1333-1| 10000100010203| 100040507697|0002|356.45|5555| SSSSY|KKKKM|1000005|10| N096|10043| C987 I need the output to look like this FORD|1333-1|10000100010203|100040507697|0002|356.45|5555| SSSSY|KKKKM|1000005|10| N096|10043| C987 The leading... (8 Replies)
Discussion started by: wahi80
8 Replies

3. Shell Programming and Scripting

Help awk/sed: putting a space after numbers:to separate number and characters.

Hi Experts, How to sepearate the list digit with letters : with a space from where the letters begins, or other words from where the digits ended. file 52087mo(enbatl) 52049mo(enbatl) 52085mo(enbatl) 25051mo(enbatl) The output should be looks like: 52087 mo(enbatl) 52049... (10 Replies)
Discussion started by: rveri
10 Replies

4. Shell Programming and Scripting

How to use the sub command to replace a <space> between two numbers

Hi I have file which stores dates. 2008-09-12|2008-09-12<space1>00:00:12|<space2>2008-09-12 Some one please help me on how should I use the sub command to replace only the space which has numbers on both sides. Expected output 2008-09-12|2008-09-1200:00:12|<space2>2008-09-12 --... (4 Replies)
Discussion started by: machomaddy
4 Replies

5. UNIX for Dummies Questions & Answers

insert white space between numbers

Hello all, I have a file with several lines like this: (1,1) (4,10) (8,23) (17, 4) (6,8) etc. and I need this: ( 1 , 1 ) ( 4 , 10 ) ( 8 , 23 ) ( 17 , 4 ) ( 6 , 8 ) How do I insert a space between the left parenthesis and the first number, between the first number and the comma,... (2 Replies)
Discussion started by: MDeBiasse
2 Replies

6. Infrastructure Monitoring

SNMP disk space - inaccurate numbers

on the remote server that im running the snmp command against, below is the information about the specific directory i'm concerned about: SIZE USED AVAIL 673G 483G 157G can someone explain to me why snmp is telling me the size of this filesystem is 176399584? ... (5 Replies)
Discussion started by: SkySmart
5 Replies

7. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

8. Shell Programming and Scripting

Inserting a range of consecutive numbers into a text file

I have a text file in the following format .... START 1,1 2,1 3,1 .. .. 9,1 10,1 END .... I want to change to the output to .... START 1,1 2,1 3,1 .. (4 Replies)
Discussion started by: VNR
4 Replies

9. UNIX for Dummies Questions & Answers

inserting uniq sequential numbers at the start of the file

Hi Unix gurus, I have a file. I need to insert sequential number at the starting of the file. Fields are delimited by "|". I know the starting number. Example: File is as follows |123|4test|test |121|2test|test |x12|1test|test |vd123|5test|test starting number is : 120 ... (7 Replies)
Discussion started by: jingi1234
7 Replies

10. Shell Programming and Scripting

Inserting a space

I am trying to reformat some UK postal codes. I have a csv where field 12 is the postal code. The postal codes length has a maximum of 7. Basically, I would like a bit of code to look at field 12 and if the postal code has a length of 7, then insert a space into the field fourth from the right,... (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question