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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help awk/sed: putting a space after numbers:to separate number and characters.
# 1  
Old 03-05-2013
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
Code:
52087mo(enbatl)
52049mo(enbatl)
52085mo(enbatl)
25051mo(enbatl)


The output should be looks like:

Code:
52087 mo(enbatl)
52049 mo(enbatl)
52085 mo(enbatl)
25051 mo(enbatl)

Thanks a lot,
# 2  
Old 03-05-2013
Try
Code:
 sed 's/^[0-9]*/& /'  file

This User Gave Thanks to mirni For This Post:
# 3  
Old 03-05-2013
Another approach:
Code:
awk '{ match($0,/^[0-9]*/); print substr($0, RSTART, RLENGTH) " " substr($0, RSTART+RLENGTH) } ' file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 03-05-2013
@mirni. That would work in this case, but if if there is also text that begins with a non-digit then as space would be inserted at the start of the line.. To avoid that one could use:
Code:
sed 's/^[0-9][0-9]*/& /' file

@bipinajiith, the same applies to your awk approach..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-05-2013
Yes, I was matching nothing also. Thank you.

I have a feeling lines may start with letters too, re-reading the OP's post:

Quote:
... or other words from where the digits ended.
In which case I would omit the anchor, and let it match the first group of digits:
Code:
sed 's/[0-9][0-9]*/& /' file

# 6  
Old 03-05-2013
Quote:
Originally Posted by Scrutinizer
@mirni. That would work in this case, but if if there is also text that begins with a non-digit then as space would be inserted at the start of the line.. To avoid that one could use:
Code:
sed 's/^[0-9][0-9]*/& /' file

@bipinajiith, the same applies to your awk approach..
Ah yes..that's a consequence of the * trying to match as much as possible but also as early as possible. "Being early" wins over "being greedy", if it can.
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 03-05-2013
Thanks all, it worked. Scrutinizer thanks for explaining 'text that begins with non-digit' as well.

I am wondering what would be the code if the digits stays in the middle:

Example text:
abce1234jklm

Want the output as :
Code:
abce 1234 jklm


Code:
echo "abce1234jklm" | sed 's/[0-9]*/ & /'

Doesn't works ,

Thanks a lot.

---------- Post updated at 01:47 PM ---------- Previous update was at 01:39 PM ----------

ok I am able to figure it out:



Code:
echo "abce1234jklm" | sed 's/^[a-z][a-z]*/& /;s/[0-9][0-9]*/&  /'

abce 1234  jklm


Thanks for all the helps all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

2. Shell Programming and Scripting

How to ignore characters and print only numbers using awk?

Input: ak=70&cat15481=lot=6991901">Kaschau (1820-1840) ak=7078&cat15482=lot=70121">Principauté (1940-1993) ak=709&cat=lot15484=70183944">Arubas (4543-5043)Output: 70 15481 6991901 7078 15482 70121 709 15484 70183944 (11 Replies)
Discussion started by: sdf
11 Replies

3. Programming

putting numbers behind eachother

I want to make a program where you have to insert binary numbers like this: do { iBinary = getche(); }while(iBinary == 1 || iBinary == 0); after you get the numbers I want them to be placed behind eachother so you will get: input: 1 1 0 1 output: 1101 (7 Replies)
Discussion started by: metal005
7 Replies

4. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

5. Shell Programming and Scripting

Insert space between characters using sed

Input: Youcaneasilydothisbyhighlightingyourcode. Putting space after three characters. You can eas ily dot his byh igh lig hti ngy our cod e. How can i do this using sed? (10 Replies)
Discussion started by: cola
10 Replies

6. Shell Programming and Scripting

Concatenating lines of separate files using awk or sed

For example: File 1: abc def ghi jkl mno pqr File 2: stu vwx yza bcd efg hij klm nop qrs I want the reult to be: abc def ghistu vwx yza jkl mno pqrbcd efg hij klm nop qrs (4 Replies)
Discussion started by: tamahomekarasu
4 Replies

7. Shell Programming and Scripting

AWK to separate numbers from logs

Hello friends, Im trying to separate a number from a log, but it seems i need help here awk '/stimated/ {print $5}' mylog.txt gives (1515.45MB). i need pure number part to use in a comparision loop so i want to separate the number part (but only 1515 not 1515.45 ) awk '/stimated/... (6 Replies)
Discussion started by: EAGL€
6 Replies

8. Shell Programming and Scripting

Separate date timestamp use awk or sed command ?

Hi, I have logfile like this : Actually the format is date format : yyyymmddHHMMSS and i want the log become this format yyyy-mm-dd HH:MM:SS for example 2009-07-19 11:46:52 Can somebody help me ? Thanks in advance (3 Replies)
Discussion started by: justbow
3 Replies

9. Shell Programming and Scripting

Putting a character between two other characters?

I need to separate Pascal style identifiers (TheyLookLikeThis) into words separated by an underscore (_). I've tried sed 's//&_&/' but this won't work (obviously). I'd love some help. (4 Replies)
Discussion started by: Ilja
4 Replies

10. Shell Programming and Scripting

how to separate every 10 characters then space then another 10 charactes

suppose u have a file P2122 AAJJSKKSKSLSKSKMSKMS P2123 AASSJMWJNSJNWHNSKSJ P2126 AHJMKNSAJNSKAKMOKALM P3533 KKKSJMAKKLLMLMSLMSPM P2122 JKKSKKMKKSKKKS--------------NSNJ P2123 NSJN---------MSKMKMKKSLLNSLL P2126 JNJSJNWKMSK-------------SKKSMK P3533 JSNSNPWLLL-SKK----SKKKSKKS so the... (2 Replies)
Discussion started by: cdfd123
2 Replies
Login or Register to Ask a Question