Read file and for each line replace two variables, add strings and save output in another file

Hi All,

I have a file, let's call it "info.tmp" that contains data like this ..

ABC123456
PCX333445
BCD789833

I need to read "info.tmp" and for each line add strings in a way that the final output is

put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)'

where XXX are the first three characters of each line in "info.tmp" and
xxxxxx are the remaining 6 characters of each line in "info.tmp"

So something like ...

GRP="ABC"
SITE="123456"
echo "put /logs/ua/dummy.trigger 'AAA00001.FTP."$GRP".BLA03A01."$SITE"(+1)'" >> trigger.tmp

But considering that I need this line created for each row in "info.tmp" file!

I've tried with

for GRP in `cat info.tmp | cut -c1-3`
do
    for SITE in `cat info.tmp | cut -c4-9`
    {
       echo "put /logs/ua/dummy.trigger 'AAA00001.FTP."$GRP".BLA03A01."$SITE"(+1)'" >> trigger.tmp
    }
done

but of course this duplicates records ... because for each GRP it matches every SITE.

I've also tried

awk '{print substr($0,1,3)}'
awk '{print substr($0,4,9)}

but I don't know how to "concatenate" this result with the other "echoes" I need and I know that after an awk is used, if you use another one, you won't have the full string you had at the first time. Also if I "echo" then the "awk" would take that string and not the content of the file "info.tmp".

Help will be much appreciated!

nawk -v q="'" '{print "put /logs/ua/dummy.trigger " q "AAA00001.FTP." substr($1,1,3) ".BLA03A01." substr($1,4) "(+1)" q}' myFile
1 Like

Thank you, vgersh99!!

This does exactly what I need but unfortunately, my system doesn't support nawk! So I tried it with awk and it works too!

Thank you, thank you, thank you!

You can also do with with pure bash/ksh shell (You were pretty close with your initial attempt):

while read LINE
do
    SITE=${LINE#???}
    GRP=${LINE%$SITE}
    echo "put /logs/ua/dummy.trigger 'AAA00001.FTP."$GRP".BLA03A01."$SITE"(+1)'"
done < info.tmp >> trigger.tmp

One way through sed..

sed "s|\(...\)\(.*\)|put /logs/ua/dummy.trigger 'AAA00001.FTP.\1.BLA03A01.\2(+1)'|" inputfile > outfile

Thank you!
All of these worked perfectly!