0

I have a file named unimplemented.txt which looks like :

verify-sdh-t1-satop-traffic-over-mpls-1for1-xxx,./TDM/SDH.xls
verify-sdh-t1-satop-traffic-over-mpls-1plus1-xxx,./TDM/SDH.xls

How do I use while loop to read above file and generate an output file at /TDM/SDH_check.xls which contains

verify-sdh-t1-satop-traffic-over-mpls-1for1-xxx

I tried doing this but it goes into infinite loop

while IFS="," read dummy casename tpname < "unimplemented.txt"
do
    echo "$casename" > basename $tpname|sed -e 's/\..\+$//'
done
2
  • What do you actually want to do to the data? Remove everything from the first comma to end of line? Is that last part of the input the name of the output file, or is the output file constant? (You have ./TDM/SDH.xls in the input data, but SDH_check.xls as the output file name, so there's that extra _check too
    – ilkkachu
    Nov 7, 2019 at 9:59
  • Yes I want to generate a file in the same path without disturbing the existing file so added _check and also need only data until "," in the output file. Nov 7, 2019 at 10:07

1 Answer 1

0

Seems that there's several problem in your script:

  1. Why do you read dummy casename tpname ? In this line, read is waiting for 3 fields, but you have and want only 2.

  2. When you write echo "$casename" > basename $tpname|sed -e 's/\..\+$//', it does:

    1. echo $casename into basename (and not $basename).
    2. apply the sed substitution to the output of echo... which is nothing.
  3. You use > which will overwrite upon each call to echo. To append, use >>.

Below looks correct for me, but not sure if it is what you want:

gcolangelo@NEO-L196:/tmp/pouet$ cat unimplemented.txt 
verify-sdh-t1-satop-traffic-over-mpls-1for1-xxx,./TDM/SDH.xls
verify-sdh-t1-satop-traffic-over-mpls-1plus1-xxx,./TDM/SDH.xls
verify-sdh-t2-satop-traffic-over-mpls-1for1-xxx,./TDM/SDG.xls
verify-sdh-t2-satop-traffic-over-mpls-1plus1-xxx,./TDM/SDG.xls
gcolangelo@NEO-L196:/tmp/pouet$ cat unimplemented.txt | while IFS=',' read casename tpname ; do echo "$casename"|sed -e 's/\..\+$//' >> $tpname ; done
gcolangelo@NEO-L196:/tmp/pouet$ cat TDM/SDH.xls 
verify-sdh-t1-satop-traffic-over-mpls-1for1-xxx
verify-sdh-t1-satop-traffic-over-mpls-1plus1-xxx
gcolangelo@NEO-L196:/tmp/pouet$ cat TDM/SDG.xls 
verify-sdh-t2-satop-traffic-over-mpls-1for1-xxx
verify-sdh-t2-satop-traffic-over-mpls-1plus1-xxx
3
  • This is what I want but not in TDM/SDH.xls (this is an existing file and do not want to edit it), hence generating new file TDM/SDH_check.xls in same path. Nov 7, 2019 at 10:12
  • So adjust for your need dude.
    – binarym
    Nov 7, 2019 at 10:21
  • Yeah done it. Thanks for the help. Nov 7, 2019 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.