Using sed on files with certain extension inside shell script

KLMM

I am trying to use sed to find files with certain extension and then replace occurrences of a certain string with another.

No directory mentioned using current directory
*.h
sed: can't read *.h: No such file or directory
*.C
sed: can't read *.C: No such file or directory
*.cc
sed: can't read *.cc: No such file or directory
un.cpp

This is the code in my script:

for file in *.{h,C,cc,cpp}
       do
          echo $file;
          sed -i -e 's/${1}/${2}/g' $file;
       done

I am having a similar issue when I am trying to use sed on sub folders recursively using find:

find ./*.{h,C,cc,cpp} -type f -exec sed -i -e 's/${2}/${3}/g' {} \;

Thank you

András Korn

find . -type f -a \( -name "*.h" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \) -a -exec sed -i -e "s/${2}/${3}/g" {} +

Should work.

In your first script the problem was that if there is no match for e.g. *.h, bash will pass the literal *.h to sed, which will then think it's a filename, but since no such file exists, it'll fail.

In the 2nd case, with find(1), you were having the shell look for matching filenames in the current directory and then pass them to find(1).

In the sed statement you need double quotes instead of single quotes for the shell to perform variable expansion within the quoted string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TCSH Shell Script: substitute a variable using sed inside file

To get the file extension of the files present in each row using shell script

Bash Script checking for files with certain extension

SED statement to replace variable inside Shell Script

shell script to delete certain files and rename others

Looking for files of certain extension using lambda

Moving files with a certain extension using grep

Download files using Shell script

Find files using shell script

How to creat a symbolic link to all files with a specific extension from a specific directory using a shell script?

Finding a file extension in a string using shell script

Adding a line to a file using sed in a shell script

replacing values in shell script using sed

removing a character in a certain context (using shell script)

Copy all files from certain file type extension in script

Updating text in XML file using a certain node (Shell script)shell

Shell using sed to remove file extension from find results

Naming files in certain way using Bash script

Shell script to create a folder with files name and copy files inside

Using if and shell variables inside an awk script

Shell script: Count number of files in a particular type extension in single folder

Shell script to check for the presence of one or more files with a specific extension?

Finding files with certain extension

How to modify JSON files inside a zip file using shell script/ ANT script/ java code/ python code? which of these is efficient and easy?

Create shell script to remove certain files from specified directories

Create new files in Shell script using VIM

Multiple files rename using linux shell script

Count files and directories using shell script

Adding size of files using shell script