jackscorrow
jackscorrow

Reputation: 702

SED command works in command line but not inside script [file not found error]

I'm trying to do a really simple task: I want to replace an occurrence of a word in the header of a CSV file with another word. Stop.

I also want this to be done for all CSVs in my current folder.

If I run this in command line it works perfectly

sed -i 's/CLOUD_RESOURCE_CONSUMPTION_RATE/CLOUD_RES_CONSUMPTION_RATE/' *.csv

But if I run it in a shell script like this:

#!/bin/bash
sed -i 's/CLOUD_RESOURCE_CONSUMPTION_RATE/CLOUD_RES_CONSUMPTION_RATE/' *.csv

I get the error: No such file or directory and this file appears in my directory .

What am I doing wrong? I'm sure it's something silly but I can't figure it out.

Upvotes: 0

Views: 1274

Answers (2)

Jon
Jon

Reputation: 3671

I think you have a stray character on the end of the sed line in your bash script. Was the script written using a Windows editor? If so, I suspect there is a carriage return on the end. I can replicate your error if I put a carriage return in.

Try running

dos2unix <scriptname>

and re-running.

If dos2unix doesn't work it wasn't a carriage return, and I'd suggest retyping the whole script into a new file using nano or vim to remove the stray.

Upvotes: 3

Eby Jacob
Eby Jacob

Reputation: 1458

Can you write your script like this

 sed -i 's/CLOUD_RESOURCE_CONSUMPTION_RATE/CLOUD_RES_CONSUMPTION_RATE/' <path to folder having csv files>/*.csv

Upvotes: 1

Related Questions