0

I want to run a program requiring comma-separated arrays of files as follows:

program -input1 A.txt,B.txt,C.txt -input2 D.txt,E.txt,F.txt -out out.txt

My bash attempt:

I'm running command from a folder called ProgramFolder and this folder also contains file1.txt and file2.txt. file1.txt and file2.txt contain names of files for the program, such as:

File1:
A.txt,B.txt,C.txt


File2:
D.txt,E.txt,F.txt

But "A.txt, B.txt,C.txt" and "D.txt, E.txt,F.txt" are in a different folder: source 1 and source2 as shown below.

## file sources
source1= ~/Band/folder1/
source2= ~/Band/folder1/

I have tried running the program this way without success:

program -input $source1$(cat file1.txt) -input2 $source2$(cat file2.txt) -out out.txt

What is the best way to write a bash for running the program?

1
  • The easiest way is probably to include the paths in File1.txt and File2.txt and then use program -input1 $(cat File1.txt) -input2 $(cat File2.txt)
    – Wayne_Yux
    Aug 27, 2015 at 10:52

1 Answer 1

3

You may use sed inside the two command substitutions to process the original files prior their listing within the arguments of the program (thanks to steeldriver for the simplified pattern):

program -input "$(sed "s@^\|,@&$source1@g" file1.txt)" -input2 "$(sed "s@^\|,@&$source2@g" file2.txt)"

I changed the sed commands' default delimiter (/) to @ because both $source1 and $source2 will expand to a path containing /; nontheless | is needed for the alternation.

sed commands breakdown:

  • s: asserts to perform a substitution
  • @: starts the pattern
  • ^\|,: matches either the start of the line or a , character
  • @: stops the pattern / starts the replacement string
  • &: backreference which will expand to the match
  • $sourceN: expands to the content of $sourceN
  • @: stops the replacement string / starts the modifiers
  • g: substitutes every match in the line

Sample output of the sed commands for the given values of $source1, $source2 and for the given contents of file1.txt and file2.txt:

user@debian ~/tmp % echo $source1
~/Band/folder1/
user@debian ~/tmp % echo $source2
~/Band/folder2/
user@debian ~/tmp % cat file1.txt 
A.txt,B.txt,C.txt
user@debian ~/tmp % cat file2.txt 
D.txt,E.txt,F.txt
user@debian ~/tmp % sed "s@^\|,@&$source1@g" file1.txt
~/Band/folder1/A.txt,~/Band/folder1/B.txt,~/Band/folder1/C.txt
user@debian ~/tmp % sed "s@^\|,@&$source2@g" file2.txt
~/Band/folder2/D.txt,~/Band/folder2/E.txt,~/Band/folder2/F.txt
4
  • 1
    Does the match pattern really need to be that complicated? how about just sed "s@^\|,@&$source1@g" File1? Aug 27, 2015 at 12:00
  • @steeldriver Indeed... it doesn't: It was the result of many wrong attempts. Thanks! Updated
    – kos
    Aug 27, 2015 at 12:14
  • Many thanks to you two, Kos and steeldriver. The solution is perfect. Just what I wanted!
    – user27976
    Aug 27, 2015 at 13:47
  • @user27976 Glad that it helped :)
    – kos
    Aug 27, 2015 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .