英文:
passing file names to a script which takes multiple arguments which are modified names of the input file using xargs
问题
I have a script that accepts two files as parameters (run_this file1 file2
). I have a 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.
英文:
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.
答案1
得分: 2
只是一个普通的循环。
for f1 in analysis-{A,B,C}*-[123][abc].csv; do
f2=$(sed 's/analysis/analysis_q/g' <<<"$f1")
run_this "$f1" "$f2"
done
对于xargs,我将删除前缀并仅使用变量部分。
printf "%s\n" analysis-{A,B,C}*-[123][abc].csv |
sed 's/^analysis-//' |
xargs --interactive -I {} run_this analysis-{} analysis_q-{}
英文:
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-{}
答案2
得分: 1
使用sed
和xargs
可以这样做:
printf '%s\n' analysis-*.csv |
sed -E 's/^(analysis)(.*)/_q /' |
xargs -n2 echo run_this
当你对输出满意时,移除echo
。
输出
run_this analysis_q-A-1a.csv analysis-A-1a.csv
run_this analysis_q-A-1b.csv analysis-A-1b.csv
英文:
Like this, using sed
and xargs
:
printf '%s\n' analysis-*.csv |
sed -E 's/^(analysis)(.*)/_q /' |
xargs -n2 echo run_this
Remove echo
when you are happy with the output.
Output
run_this analysis_q-A-1a.csv analysis-A-1a.csv
run_this analysis_q-A-1b.csv analysis-A-1b.csv
答案3
得分: 1
Bash可以很好地对变量进行搜索和替换,这称为变量扩展或变量替换:
for f in analysis-{A,B,C}*-[123][abc].csv
do
echo run_this "${f/analysis/analysis_q}" "$f"
done
一旦这对您有效,删除echo
,脚本将运行。表达式${f/analysis/analysis_q}
获取了$f
的值,将analysis替换为analysis_q。
这种方法完全不使用ls
、sed
或xargs
。这是纯粹的bash。
英文:
Bash can do search and replace on variable just fine, it is called variable expansion or variable substitution:
for f in analysis-{A,B,C}*-[123][abc].csv
do
echo run_this "${f/analysis/analysis_q}" "$f"
done
Once this works for you, remove the echo
and script will be run. The expression ${f/analysis/analysis_q}
takes the value of $f
, replace analysis with analysis_q.
This method does not use ls
, sed
, or xargs
at all. It is pure bash.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论