1

I have three different file sets say 10234.su, 10234.ph and 10234.th. I have a shell script using which I can extract some data from 10234.su and can write to 10234.ph and 10234.th, but it works for only one file at a time. I need a modification which can automatically read the files one by one (xxx.su) and write appropriately (xxx.ph and xxx.th). All the three files have always the same names. Exapmle - 10234.su, 10238.su, 10242.su etc. I have a lot of .su files with different names which I want to read one by automatically.

My script is as follows -

rm hdrfile binary 

for i in *.su; do 
sugethw <"$i" output=geom key=cdp >hdrfile
a2b <hdrfile n1=994 >binary
echo "$i -> sugethw <"$i" output=geom key=cdp >hdrfile a2b <hdrfile n1=994 >binary"

for j in *.ph; do 
sushw <"$j" infile=binary key=cdp >test1.su
rm "$j"
mv test.su "$j"
echo "$j -> sushw <"$j" infile=binary key=cdp >test.su rm "$j" mv test.su "$j""

for k in *.th; do
sushw <"$k" infile=binary key=cdp >test2.su
rm "$k"
mv test.su "$k"
echo "$k -> sushw <"$k" infile=binary key=cdp >test.su rm "$k" mv test.su "$k""

rm hdrfile binary
done
done
done 

Thanks!

0

Try the following bash shellscript.

After testing, that it acts on all files, you must remove my echo lines and the cp lines and uncomment your real action lines with sugethw, a2b and sushw.

I don't think you need to remove the files before moving, because mv will overwrite unless you use mv -i.

#!/bin/bash

for filnam in *.su
do
 tmp=${filnam%%.su}
 i="$filnam"
 j="${tmp}.ph"  # guessing they need not be nested
 k="${tmp}.th"  # guessing they need not be nested

 echo "$tmp.hdr" > hdrfile  # replacing lines with commands unknown to me
 echo "$tmp.bin" > binary   # replacing lines with commands unknown to me
 #sugethw <"$i" output=geom key=cdp >hdrfile
 #a2b <hdrfile n1=994 >binary
 echo "$i -> sugethw <\"$i\" output=geom key=cdp >hdrfile a2b <hdrfile n1=994 >binary"

 cp binary test1.su  # replacing line with command unknown to me
 #sushw <"$j" infile=binary key=cdp >test1.su
 #rm "$j"
 mv test1.su "$j"
 echo "$j -> sushw <\"$j\" infile=binary key=cdp >test.su rm \"$j\" mv test.su \"$j\""

 cp binary test2.su  # replacing line with command unknown to me
 #sushw <"$k" infile=binary key=cdp >test2.su
 #rm "$k"
 mv test2.su "$k"
 echo "$k -> sushw <\"$k\" infile=binary key=cdp >test.su rm \"$k\" mv test.su \"$k\""
 echo "---"
done 

rm hdrfile binary
2
  • It worked very well. Thank you very much for your quick reply!! Dec 31 '18 at 11:07
  • @BrijBhushanSingh, You are welcome. I am glad that I could help :-)
    – sudodus
    Dec 31 '18 at 11:17

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.