0

I will post my script here

#!/bin/tcsh 

echo 'Running'
set fileN = '2021-02-07-0448-04S.JKH_RR.SAC'
set fileE = '2021-02-07-0448-04S.JKH_RR_BHE.SAC'
set compR=BHR
set compT=BHT
set compR_name=BHR.SAC
set compT_name=BHT.SAC
set fileN_rot = `echo $fileN | awk '{split($0,a,".SAC"); print a[1]}'`
set fileE_rot = `echo $fileE | awk '{split($0,a,".SAC"); print a[1]}'`

echo 'output1'
echo $fileN 
echo $fileE

echo 'output2'
echo $fileN_rot 
echo $fileE_rot 

echo 'output3'
echo $fileE_rot-$compR_name
echo $fileN_rot-$compT_name

The output is:

Running
output1
2021-02-07-0448-04S.JKH_RR_BHN.SAC 2021-02-07-0448-04S.JKH_RR_BHE.SAC

output2
2021-02-07-0448-04S.JKH_RR_BHN
2021-02-07-0448-04S.JKH_RR_BHE

output3
2021-02-07-0448-04S.JKH_RR_BHN
-BHR.SAC
2021-02-07-0448-04S.JKH_RR_BHE-BHT.SAC

echo $fileE_rot-$compR_name giving wrong output. Here the out is copy-pasted from the output file,so -BHR.SAC showing in new line. But in shell terminal it is showing -BHR.SAC07-0448-04S.JKH_RR_BHN. I find it strange.

vgb_backup
  • 49
  • 1
  • 6

1 Answers1

1

Looks like you have some control chars in your strings. Run cat -Ev script to see them and if you see ^Ms in the output then read Why does my tool output overwrite itself and how do I fix it? for how to deal with them.

Don't write scripts in [t]csh, though, as it wasn't designed for that. Writing a script in csh is like digging a hole with a toothbrush - sure you CAN kinda get there in the end but there are better alternatives. See https://www.google.com/search?q=google+csh+why+not.

Having said that, it's not obvious why you're trying to manipulate text in any shell. Shells exist to manipulate (create/destroy) files and processes and sequence calls to tools. The people who invented shell also invented tools such as awk for shell to call when appropriate to manipulate text. So, here is how to really write a shell script to do what you want (the shell part is to call awk to manipulate the text):

$ cat tst.sh
#!/usr/bin/env bash

awk '
    BEGIN {
        print "Running"
        fileN = "2021-02-07-0448-04S.JKH_RR.SAC"
        fileE = "2021-02-07-0448-04S.JKH_RR_BHE.SAC"
        compR = "BHR"
        compT = "BHT"
        compR_name = "BHR.SAC"
        compT_name = "BHT.SAC"

        fileN_rot = fileN
        sub(/\.SAC$/,"",fileN_rot)

        fileE_rot = fileE
        sub(/\.SAC$/,"",fileE_rot)

        print "output1"
        print fileN
        print fileE

        print "output2"
        print fileN_rot
        print fileE_rot

        print "output3"
        print fileE_rot "-" compR_name
        print fileN_rot "-" compT_name
    }
'

$ ./tst.sh
Running
output1
2021-02-07-0448-04S.JKH_RR.SAC
2021-02-07-0448-04S.JKH_RR_BHE.SAC
output2
2021-02-07-0448-04S.JKH_RR
2021-02-07-0448-04S.JKH_RR_BHE
output3
2021-02-07-0448-04S.JKH_RR_BHE-BHR.SAC
2021-02-07-0448-04S.JKH_RR-BHT.SAC

or if there really was some reason to want to do it directly in a shell (e.g. this code is in some loop manipulating files named based on these variables) then:

$ cat tst.sh
#!/usr/bin/env bash

fileN='2021-02-07-0448-04S.JKH_RR.SAC'
fileE='2021-02-07-0448-04S.JKH_RR_BHE.SAC'
compR='BHR'
compT='BHT'
compR_name='BHR.SAC'
compT_name='BHT.SAC'

fileN_rot="${fileN%*.SAC}"
fileE_rot="${fileE%*.SAC}"

echo 'output1'
echo "$fileN"
echo "$fileE"

echo 'output2'
echo "$fileN_rot"
echo "$fileE_rot"

echo 'output3'
echo "${fileE_rot}-${compR_name}"
echo "${fileN_rot}-${compT_name}"

$ ./tst.sh
output1
2021-02-07-0448-04S.JKH_RR.SAC
2021-02-07-0448-04S.JKH_RR_BHE.SAC
output2
2021-02-07-0448-04S.JKH_RR
2021-02-07-0448-04S.JKH_RR_BHE
output3
2021-02-07-0448-04S.JKH_RR_BHE-BHR.SAC
2021-02-07-0448-04S.JKH_RR-BHT.SAC
Ed Morton
  • 188,023
  • 17
  • 78
  • 185