0

I'm trying to rename commands in a bash script. If I run for example:

echo /home/scientist/mySalesData/campaignData_1482386214.24417.csv  | sed 's/\(.*\)\(_.*\)/mv \"&" \"\1.csv\"/' | bash   

It works fine and gives me campaignData.csv in the directory /home/scientist/mySalesData/ .

However, if I put this in a bash script as follows:

for f in /home/scientist/SalesData/*; do
if [ -f "$f" ]; 
  cp "$f" /home/scientist/SalesForce/SalesData/Backups/                                                                
  echo $f$ | sed 's/\(.*\)\(_.*\)/mv \"&" \"\1.csv\"/' | bash |        
fi 
done 

I get:

mv: cannot stat '/home/scientist/SalesData/campaignData_1482386214.24417.csv$': No such file or directory          

Any help would be much appreciated!

2
  • 1
    You have a simple typo. Change $f$ to "$f" and add the missing then. Commented Dec 22, 2016 at 6:42
  • What I get for doing this late at night. Commented Dec 22, 2016 at 6:43

2 Answers 2

2
cd "$srcdir"
for f in *; do
  if [ -f "$f" ]; then
    cp "./$f" "$dstdir/${f%_*}.csv"
  fi
done

The % is the strip shortest suffix pattern operator.

Sign up to request clarification or add additional context in comments.

1 Comment

oh that's neat. Learned something new with that operator.
2

You have a trailing $ here:

echo $f$

remove that (and quote the expansion):

echo "$f"

You could use here string too:

sed ... <<<"$f"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.