Issue
I have a script that accepts two files as parameters (run_this file1 file2
). I have bunch of files that I select with
ls analysis-{A,B,C}*-[123][abc].csv
and for each of those I also need to pass the corresponding file that only differs in name by prefix: instead of analysis
, it starts with analysis_q
. I tried many options I know but sed
seems to refuse cooperation with xargs
(and I know of no other simple way)
one of the things I tried:
ls analysis-{A,B,C}*-[123][abc].csv | xargs --interactive -I '{}' run_this `$(echo {} | sed 's/analysis/analysis_q/g')` {}
I am hoping to produce
run_this analysis_q-A-1a.csv analysis-A-1a.csv
run_this analysis_q-A-1b.csv analysis-A-1b.csv
and so forth.
Solution
Just a plain old loop.
for f1 in analysis-{A,B,C}*-[123][abc].csv; do
f2=$(sed 's/analysis/analysis_q/g' <<<"$f1")
run_this "$f1" "$f2"
done
For xargs, I would remove the prefix and use the variable part only.
printf "%s\n" analysis-{A,B,C}*-[123][abc].csv |
sed 's/^analysis-//' |
xargs --interactive -I {} run_this analysis-{} analysis_q-{}
Answered By - KamilCuk Answer Checked By - Willingham (WPSolving Volunteer)