Sheenas
Sheenas

Reputation: 57

SED command: How -i and -e work

I'm using the sed command for the first time to try and replace one file with another file in a txt/fsf file. Can somebody help me understand how -i and -e work here?

With my code, I am iterating through various folders and copying and exsisting file to each folder and renaming the title. It seems that when I omit the -e, then I get errors at the sed command doesn't work. However, with this current code, the cp command is working just fine until I added the sed line. Now the cp command is outputting two files per folder ${i}_design.fsf and ${i}_design.fsf-e

I am not sure what the -e file is. The file seems to be identical to the original design.fsf file. Is this related to the sed command?

#!/bin/sh
# 
Folders=(CONTROL GROUP1 GROUP2)
SC=(CPAKS_02 CPAKS_03 CPAKS_04 CPAKS_05 CPAKS_06 CPAKS_07) 
data_source=/Users/sheena/Desktop/test

cd ${data_source}

for j in ${Folders[@]}; do
  for i in ${SC[@]}; do
    cd ${data_source}/${j}/${i}
    cp ${data_source}/design.fsf ${data_source}/${j}/${i}/${i}_design.fsf
      for k in ${i}_design.fsf; do
        sed -i -e 's,'/Users/sheena/Desktop/DTI/CPAKS_03/fmri','${data_source}/${j}/${i}/${i}_fmri.nii',' ${i}_design.fsf
      done  
  done
done

Upvotes: 3

Views: 2841

Answers (1)

John1024
John1024

Reputation: 113864

You are using BSD (OSX).

-i tells sed to change the file in place with the option of creating a backup file. For BSD sed, unlike GNU (Linux) sed, the -i option requires an argument. The argument specifies the suffix used for the backup file. If you don't want a backup file, use sed -i '' -e .... The empty argument, '', tells BSD sed not to save a backup.

In other words, in your case, the sed -i -e ... command was interpreted as specifying a -e suffix for the backup file. If you don't want a backup, use sed -i '' -e ...

Upvotes: 4

Related Questions