0

Been trying to wrap my regex foo around this but not doing too well.

need sed script to format file:

844388240 920009 XXXXXXXXXX YYYYYYYYYYYYYYYYYYYYYYY
800450220 910003 AAAAAAAAAAAAAAAAAA BBBBB
736458376 947883 HHHH GGGGG
800450220 910003 AAAAAAAAAAAAAAAAAA YYYYYYYYYYYYYYYYYYYYYYY

9 digits(space)6 digits(space)up-to-15characters(space)up-to-15characters

result in:

844388240 920009 XXXXXXXXXX YYYYYYYYYYYYYYY
800450220 910003 AAAAAAAAAAAAAAA BBBBB
736458376 947883 HHHH GGGGG
800450220 910003 AAAAAAAAAAAAAAA YYYYYYYYYYYYYYY

each of the 3rd and 4th columns are truncated at 15 characters, however shorter values are passed untouched.

2 Answers 2

4

One way to do it with sed:

sed 's/\([^ ]\{15\}\)[^ ]*/\1/g'

Same thing with perl:

perl -lpe 's/\S{15}\K\S*//g'

With awk:

awk '{ for(i=1; i<=NF; i++) $i=substr($i, 0, 15)} 1'

With vim:

:%s/\v\S{15}\zs\S*//g

With cut:

tr ' ' '\n' <file.txt | cut -b1-15 | paste -d ' ' - - - -
1
  • Awesome, works like a champ. I was missing a few things on the regex. Thanks Sato....
    – Big DR
    Commented Nov 10, 2016 at 18:31
0
sed -r '
s/ /\n/g;
s/^(.{,15}).*$/\1/M3; 
s/^(.{,15}).*$/\1/M4; 
s/\n/ /g
' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .