Searching pattern and reformatting

Hi,

I have a file whose sample data is as below:

Rak 654 JM30 20100215 387898263 7867556789
RPT 654 TM38 20100215 827384652 9786735628 RNP
DCB 342 TM38 20100215 876782372 2323786527
Rak 654 JM30 20100215 328564893 8862573828
RNN 684 SK20 20100215 926738429 8283927457 RPP
TKS 20100215 873927382 7283556278
TPM 666 RS29 8273334256 RTK NFM

Now what I need to do is to print the numeric string whose length is 10 digits and then print the rest of the line. The output of the above sample should be:

7867556789 Rak 654 JM30 20100215 387898263 7867556789
9786735628 RPT 654 TM38 20100215 827384652 9786735628 RNP
2323786527 DCB 342 TM38 20100215 876782372 2323786527
8862573828 Rak 654 JM30 20100215 328564893 8862573828
8283927457 RNN 684 SK20 20100215 926738429 8283927457 RPP
7283556278 TKS 20100215 873927382 7283556278
8273334256 TPM 666 RS29 8273334256 RTK NFM

Please help. Thanks in advance.

Regards

Hi.

$ cat TestScript
awk '
  { for( I = 1; I <= NF; I++ ) if ( $I ~ /^[0-9]{10}$/ ) $1 = $I FS $1 } 1
' file1

$ ./TestScript
7867556789 Rak 654 JM30 20100215 387898263 7867556789
9786735628 RPT 654 TM38 20100215 827384652 9786735628 RNP
2323786527 DCB 342 TM38 20100215 876782372 2323786527
8862573828 Rak 654 JM30 20100215 328564893 8862573828
8283927457 RNN 684 SK20 20100215 926738429 8283927457 RPP
7283556278 TKS 20100215 873927382 7283556278
8273334256 TPM 666 RS29 8273334256 RTK NFM

Hi scott,

i thought about the same solution in solaris but below part does not work in solaris, is there any good way to do it in solaris? maybe Lenght function? or no choice rather than using 10 times [0-9] :slight_smile:

/^[0-9]{10}$/

Hi EAGL�.

It should work with /usr/xpg4/bin/awk, and with nawk too.

Failing that, try escaping the { } .... \{ \}

Cheers.

I realized that the problem was nawk, not my code. Really interesting it didnt work with nawk only with /usr/xpg4/bin/awk :confused:

/usr/xpg4/bin/awk '{for(i=1;i<=NF;i++) if($i~/^[0-9]{10}$/) {print $i" "$0}}' file

ok but this worked too:

nawk '{for(i=1;i<=NF;i++) if(length($i)==10) {print $i FS $0}}' file

I've noticed more and more things that don't work with nawk, but do with /usr/xpg4/bin/awk.

length() is OK, but it doesn't tell you if it's digits (i.e. numbers) or not.

Ok Scott thanks a lot for this usefull knowledge, Im going to use /usr/xpg4/bin/awk for guaranteed solutions anymore. Besides you right length will work there for fields with 10 ALNUMs, not only digits.

Thanks Scottn..... the script worked on my system with /usr/xpg4/bin/awk
It would be really nice of you if you could please explain me the way this awk is working specially the 1 in the end, whats its purpose ?

/usr/xpg4/bin/awk '
  {for(I=1;I<=NF;I++) if ($I ~ /^[0-9]{10}$/) $1=$I FS $1}1' file

1 at the end of the code means the whole line, its also represented by $0.
code searchs for the 10 digits field by checking each field of each line of the file. If 10 digits of field is found then first column is rearranged with this part

$1=$I FS $1

namely it (10 digits field) is placed before first field as you wanted.

regards

Thanks EAGLC

Try:

sed  's/\(.*\)\([0-9]\{10\}\)\(.*\)/\2 &/' < file