1

I am using following command to replace yyyymmdd to YYYYMMDDHH24MISS in my file:

sed -e 's/\('yyyymmdd'\)/\('YYYYMMDDHH24MISS'\)/g' filename

After I run the command in PuTTY, it displays the file with replaced values, but they do not reflect if I more the file.

I tried using -i , but it says

sed: illegal option -- i

Can someone please suggest how do I replace the given code in multiple files and save them?

  • 2
    How you're using the -i option? Can you please update your question with the line that contains the -i flag please? – tachomi Feb 4 '16 at 16:15
  • 4
    The -i option is not POSIX conformant: what system / flavor of Unix is the sed command being run on (Linux? BSD? OSX?) – steeldriver Feb 4 '16 at 16:27
  • 2
    As steeldriver said, you need to tell us i) what operating system you are connecting to and ii) show us the exact command you ran. Also, this has nothing to do with your issue but you don't need the \( or the ' or any of that. Your command can be written simply as sed -e 's/yyyymmdd/YYYYMMDDHH24MISS/g (you can even omit the -e on some systems). – terdon Feb 4 '16 at 16:53
6

Try this:

sed 's/yyyymmdd/YYYYMMDDHH24MISS/g' filename > changed.txt

Or, to keep the same filename:

sed 's/yyyymmdd/YYYYMMDDHH24MISS/g' filename > changed.txt && mv changed.txt filename
2

Your sed command only sends its result to the standard output. You would have to redirect it in a subsequent command (NOT in the same command, like sed 'sedcommand' file > file, as this would erase the file before processing it).

You also can pipe the commands to ed instead of using sed :

for file in $filelist ; do
  echo -e '%s/yyyymmdd/YYYYMMDDHH24MISS/g\nw' | ed $file
done

which substitutes on every line (%) then, after a separating newline (\n), writes the modified file in place (w).

  • 1
    sed(1) and ed(1) are mostly compatible, command-wise. – vonbrand Feb 4 '16 at 16:51

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.