Neo Neo - 3 months ago 14
Bash Question

Unix shell script to find string in a file then append by adding characters or number

Is anyone can help me to add zeros in a string within the file?

For example: I have test.dat and it contains like below:

1 FirstName LastName A +2015042113:00:00000285000FSCRD INV 0
2 FirstName LastName B +2015042220:00:00000331000SSESA INV 0

And I would like to create a shell script to edit one of the string in the fie, by inserting three (3) zeros in the middle of the string.

So the output expected will be: I bold the added three zeros as below.

01 FirstName LastName A +2015042113:00:00000000285000FSCRD INV 0

02 FirstName LastName B +2015042220:00:PM000000331000SSESA INV 0

Answer

You can use sed with regex:

sed 's/\(:..:..\)/\1000/' test.dat 

it looks for the pattern :..:.. (dot is any character, so it look for "colon,any char,any char,colon,any char,any char") and add 3 zeros to it

Comments