passing file names to a script which takes multiple arguments which are modified names of the input file using xargs

huangapple go评论58阅读模式
英文:

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 &#39;s/analysis/analysis_q/g&#39; &lt;&lt;&lt;&quot;$f1&quot;)
    run_this &quot;$f1&quot; &quot;$f2&quot;
done

For xargs, I would remove the prefix and use the variable part only.

printf &quot;%s\n&quot; analysis-{A,B,C}*-[123][abc].csv |
sed &#39;s/^analysis-//&#39; |
xargs --interactive -I {} run_this analysis-{} analysis_q-{}

答案2

得分: 1

使用sedxargs可以这样做:

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 &#39;%s\n&#39; analysis-*.csv |
    sed -E &#39;s/^(analysis)(.*)/_q /&#39; |
    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

这种方法完全不使用lssedxargs。这是纯粹的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 &quot;${f/analysis/analysis_q}&quot; &quot;$f&quot;
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.

huangapple
  • 本文由 发表于 2023年4月20日 00:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定