-1

File.txt has numerous lines with paths to different files

I want each line of File.txt to become an argument to sed command

Ex : File.txt has following lines

dir1/sub1/a.txt
dir2/sub2/b.txt
dir3/sub3/c.txt

Need to use sed command on all these files in File.txt by splitting File.txt into lines and using foreach command

How can I do that in Linux terminal? Please let me know if there are any other methods as well

1 Answers1

1

You can use xargs like so:

xargs -a File.txt -I '::' echo Hello ::
  • -a File.txt reads items from File.txt (otherwise xargs would work with stdin and pipes)
  • -I '::' makes :: a placeholder for the line read from the file that can be used in the following command

You would put your sed command at the end (instead of echo) and use :: instead of the filenames from File.txt.

I am not sure what you mean with foreach in your question.

Lars Fischer
  • 9,135
  • 3
  • 26
  • 35
  • Thank you very much. I tried this command with {} but it somehow didn't work – Aishwarya Aishu Mar 04 '23 at 14:04
  • I set `::` as placeholder, either use `::` in your sed command line or use `{}` with xargs and its `-I` argument. You could edit your question, that it shows what you are trying to accomplish. At the moment we can only guess how you want to use `sed`. – Lars Fischer Mar 04 '23 at 14:42