1747983 Members
4398 Online
108756 Solutions
New Discussion юеВ

Re: Adding column

 
SOLVED
Go to solution
Panpalia
Occasional Advisor

Adding column

i have the file in below format.
0364120000013502000117186270*805
0323120000013502200115594152*805
0438120000013502400107398174*805
0364120000013502500106634595*805

i need the o/p file
03641200000135*02000117186270*805
03231200000135*02200115594152*805
04381200000135*02400107398174*805
03641200000135*02500106634595*805


I tried sed command but unable to do it.

Thanks in Advanced.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Adding column

Hi:

One way:

# perl -pe 's/(.{13})/$1\*/' file

...and if you want to update in-place do:

# perl -pi -e 's/(.{13})/$1\*/' file

Note that Perl counts from zero, so the fisrt column is 0.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Adding column

Hi (again):

Oops, I can't count:

# perl -pe 's/(.{14})/$1\*/' file

# perl -pi -e 's/(.{14})/$1\*/' file

...to update inplace

(or):

sed -e 's/\(.\{14\}\)/\1\*/' file

...as the firt Perl's counterpart.

Regards!

...JRF...
Laurent Menase
Honored Contributor

Re: Adding column

while read a b c
do
e=${b#???}
eval "f=${b%$e}"
echo $a $f\*$e
done fileout

or

awk '{printf("%s %s*%s\n", $1, substr($2,1,3),substr($2,4)) }' fileout

of if it is always 135
while read a b c
do
e=${b#135}
echo $a 135\*$e
done fileout

Steven Schweda
Honored Contributor

Re: Adding column

td192> cat n.dat
0364120000013502000117186270*805
0323120000013502200115594152*805
0438120000013502400107398174*805
0364120000013502500106634595*805

td192> < n.dat sed -e 's/^\(..............\)/\1*/'
03641200000135*02000117186270*805
03231200000135*02200115594152*805
04381200000135*02400107398174*805
03641200000135*02500106634595*805
Panpalia
Occasional Advisor

Re: Adding column

The sed command (sed -e 's/\(.\{14\}\)/\1\*/' file) is working fine.

Now how to remove the added * from Outfile to get initial file
Steven Schweda
Honored Contributor
Solution

Re: Adding column

> Now how to remove [...]

Hire someone who can read "man sed" (and
think)?
James R. Ferguson
Acclaimed Contributor

Re: Adding column

Hi (again):

> The sed command (sed -e 's/\(.\{14\}\)/\1\*/' file) is working fine.

> Now how to remove the added * from Outfile to get initial file

Then:

# sed -e 's/\*//' file

Regards!

...JRF...
Panpalia
Occasional Advisor

Re: Adding column

Thanks!