Single sed command for multiple replacements?

I am using sed to replace text in files. I want to give sed a file that contains all the lines to be searched for and replaced in a given file.

It looks at the .h and .cpp files. In each file, it looks for the names of the files that are included in it. If found, it replaces, for example, "ah" "<ah>" (without quotes).

script:

For /F %%y in (all.txt) do 
   for /F %%x in (allFilesWithH.txt) do
       sed -i s/\"%%x\"/"\<"%%x"\>"/ %%y

      

  • all.txt - List of files to substitute in them
  • allFilesWithH.txt - all include names to look for

I don't want to run sed multiple times (like the number of filenames in the input.txt file.), But I want to run a separate sed command and pass input.txt to it as input.

How can i do this?

PS I run sed from the VxWorks Development shell, so it doesn't have all the commands the Linux version does.

+2


source to share


3 answers


sed

itself has no way of reading filenames from a file. I'm not familiar with the VxWorks shell, and I suppose this is due to the lack of answers ... So here are some things that will work in bash - maybe VxWorks will support one of those things.

sed -i 's/.../...' `cat all.txt`

sed -i 's/.../...' $(cat all.txt)

cat all.txt | xargs sed -i 's/.../...'

      



Indeed, it is not necessary to repeatedly call sed

several times if it completes the task:

cat all.txt | while read file; do sed -i 's/.../.../' $file; done

for file in $(cat all.txt); do   # or `cat all.txt`
    sed -i 's/.../.../' $file
done

      

+4


source


You can exclude one of the loops, so sed

you only need to call it once per file. using a parameter -f

to specify more than one replacement:

For /F %%y in (all.txt) do 
    sed -i -f allFilesWithHAsSedScript.sed %%y

      

allFilesWithHAsSedScript.sed

comes from allFilesWithH.txt

and will contain:



s/\"file1\"/"\<"file1"\>"/
s/\"file2\"/"\<"file2"\>"/
s/\"file3\"/"\<"file3"\>"/
s/\"file4\"/"\<"file4"\>"/

      

(The article Common Streams: Sed by Example, Part 3 has many example sed scripts with explanations.)

Don't use confuSed (pun intended).

+5


source


What I would do is change allFilesWithH.txt to sed command using sed.

(When to force sed. I would use Perl instead, it can search for * .h files as well.)

0


source







All Articles