Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my script, I am taking a text file and going through the file line by line and replacing the string "test" with "true", then redirect it to a new file. Here's my code:

cat $FILENAME | while read LINE
do
echo  "$LINE" | sed -e `s/test/true/g` > $NEWFILE
done

However when I execute the script I get the following errors:

/home/deploy/KScript/scripts/Stack.sh: line 46: s/test/true/g: No such file or directory
sed: option requires an argument -- e
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent

                 suppress automatic printing of pattern space

  -e script, --expression=script

                 add the script to the commands to be executed
  -f script-file, --file=script-file

                 add the contents of script-file to the commands to be executed

  -i[SUFFIX], --in-place[=SUFFIX]

                 edit files in place (makes backup if extension supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
         (avoids change of input file ownership)

  -l N, --line-length=N

                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.

  -r, --regexp-extended

                 use extended regular expressions in the script.

  -s, --separate

                 consider files as separate rather than as a single continuous
                 long stream.

  -u, --unbuffered

                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

Can you please help me find what I am doing wrong ?

share|improve this question
 
If $FILENAME only contains a single filename, you can get rid of the loop and simply do this: sed -e 's/test/true/g' $FILENAME > $NEWFILE. –  doubleDown Jun 13 '13 at 9:54
 
The whole script seems wrong to me - sed operates on input file line-by-line on itself, so all you need is just sed 's/test/true/g' $FILENAME > $NEWFILE. Your current script will result in $NEWFILE having just one line - last one to be exact. –  aragaer Jun 13 '13 at 11:49
 
Guys thanks but i found the error the syntax is correct and i am piping the input thorugh Echo works well too. @aragaer only mistake being i used "`" instead of "'" :) –  Riddle Jun 14 '13 at 7:46
add comment

4 Answers

For errors like this, put set -x in the line before echo "$LINE" | sed -e 's/test/true/g' > $NEWFILE

set +x
echo  "$LINE" | sed -e `s/test/true/g` > $NEWFILE

Bash will then print the command line before it executes it, quoting the arguments. This should give you an idea why it fails.

Make sure you use the correct quote characters. ` (backtick) and ' (single quote) are different things. The first one will try to execute the command s/test/true/g and pass the result of this command to sed

share|improve this answer
add comment

When using sed, you should quote replacement parameters in single or double quotes.

For example, this should work:

echo "$LINE" | sed -e "s/test/true/g" > $NEWFILE
share|improve this answer
 
thanks ! @mvp got the mistake ! –  Riddle Jun 14 '13 at 7:47
add comment

sed can work by reading a file and applying a script to every line, which is excatly what you are doing in your script. So you just want to:

sed -e 's/test/true/g' "$FILENAME" > "$NEWFILE"

remark: the -e parameter is optional here as you only have one script.

share|improve this answer
add comment

The recent edit seems to have changed the command you used, but you used backticks in your command:

sed -e `s/test/true/g`

Use:

sed -e 's/test/true/g'

instead.

share|improve this answer
 
Well spotted. Reverted question to the original quoting. –  viraptor Jun 13 '13 at 9:44
 
@devnull thanks ! i found it :) –  Riddle Jun 14 '13 at 7:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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