Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split string / add spaces in different positions 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
0
0
PL
I need to split long string (best with sed or awk), adding space after 3, 7, 12 and 19 character, for example input string:

Code:
23092034920934iqke029i39i2340234i0293

and expected output:
Code:
[b]230 9203 49209 34iqke0 29i39i2340234i0293[/b]



Code:
230 
   9203 
       49209
            34iqke0
                   29i39i2340234i0293
1234567890123456789012345678901234567890
 
ok, I've done following command which does the trick

Code:
$ echo "23092034920934iqke029i39i2340234i0293 "|sed 's/.../&\ /;s/......../&\ /;s/............../&\ /;s/....................../&\ /'
230 9203 49209 34iqke0 29i39i2340234i0293
 
Hi

I am quite late at this party, but maybe you will find some of these alternatives abit more readable :
Code:
[blue]master #[/blue] echo "23092034920934iqke029i39i2340234i0293 " | sed 's/.\{19\}/& /;s/.\{12\}/& /;s/.\{7\}/& /;s/.\{3\}/& /'
230 9203 49209 34iqke0 29i39i2340234i0293 

[blue]master #[/blue] echo "23092034920934iqke029i39i2340234i0293 " | sed -r 's/.{19}/& /;s/.{12}/& /;s/.{7}/& /;s/.{3}/& /'
230 9203 49209 34iqke0 29i39i2340234i0293 

[blue]master #[/blue] echo "23092034920934iqke029i39i2340234i0293 " | sed 's/./& /19;s/./& /12;s/./& /7;s/./& /3' [gray]# (*)[/gray]
230 9203 49209 34iqke0 29i39i2340234i0293 

[blue]master #[/blue] echo "23092034920934iqke029i39i2340234i0293 " | cut --output-delimiter=' ' -c -3,4-7,8-12,13-19,20-
230 9203 49209 34iqke0 29i39i2340234i0293 

[blue]master #[/blue] echo "23092034920934iqke029i39i2340234i0293 " | gawk -vFIELDWIDTHS='3 4 5 7 18' '$1=$1'
230 9203 49209 34iqke0 29i39i2340234i0293
The marked [gray](*)[/gray] solution should work with any standard Sed. All others assume implementation supporting GNU-specific extensions.

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top