Bash:从文件到一个命令参数的多行

Bash:从文件到一个命令参数的多行,bash,shell,Bash,Shell,我需要将一个文件中的多行作为一个逗号分隔的参数传递到脚本中。每当我尝试将处理文件的输出作为单个字符串使用时,逗号都会变成分隔符。我该怎么做 测试用例: [user@host]$ #Here is a word list, my target words are on lines starting with "1," [user@host]$ cat word_list_numbered.txt 1,lakin 2,chesterfield 3,sparkplug 4,unscrawling 5,

我需要将一个文件中的多行作为一个逗号分隔的参数传递到脚本中。每当我尝试将处理文件的输出作为单个字符串使用时,逗号都会变成分隔符。我该怎么做

测试用例:

[user@host]$ #Here is a word list, my target words are on lines starting with "1,"
[user@host]$ cat word_list_numbered.txt
1,lakin
2,chesterfield
3,sparkplug
4,unscrawling
5,sukkah
1,girding
2,gripeful
3,historied
4,hypoglossal
5,nonmathematician
1,instructorship
2,loller
3,containerized
4,duodecimally
5,oligocythemia
1,nonsegregation
2,expecter
3,enterrologist
4,tromometry
5,salvia
[user@host]$ #Here is a mock operation, it just outputs the number of args, I want all selected words as one argument
[user@host]$ cat operation.sh
echo "This script has $# arguments"
[user@host]$ #Here is a script that outputs the needed words as comma delimited
[user@host]$ grep '^1,' word_list_numbered.txt | tr -d '1,' | tr '\n' ',' | sed 's/,$//'        lakin,girding,instructorship,nonsegregation[user@host]$
[user@host]$ #Here is the operation script receiving that comma delimited list
[user@host]$ ./operation.sh $(grep '^1,' word_list_numbered.txt | tr -d '1,' | tr '\n' ',' | sed 's/,$//')
This script has 4 arguments
[user@host]$ #oops, too many arguments
[user@host]$ ./operation.sh foo,bar
This script has 1 arguments
[user@host]$ ./operation.sh foo bar
This script has 2 arguments
[user@host]$
详情:

  • 所需的单词以1开头
  • 所有单词都应作为一个逗号分隔的参数传递给operation.sh
  • 我无法控制word_list_numbered.txt的格式,也无法控制operation.sh是否需要将所有单词作为一个逗号分隔的参数
  • 多次运行operation.sh不是最佳选择--我问这个问题,所以我不必这样做

awk和xargs的组合如何

 awk -F, -v ORS=, '$1==1{print $2}' file | xargs ./operation.sh
或者如果您介意后面的逗号:

 awk -F, -v ORS=, '$1==1{print $2}' file | sed 's/,$//' | xargs ./operation.sh
测试:


如果没有xargs,它将是:

./operation.sh "$(awk -F, -v ORS=, '$1==1{print $2}' file | sed 's/,$//')"

awk
的另一种替代方法是在bash中使用命令替换用文件内容填充数组,然后再次将所有行连接到一个逗号分隔的字符串中,以传递给
操作。sh

#!/bin/bash

## function simulating operation.sh
operation() { printf "%s\n" "$1"; }

a=( $(<word_list_numbered.txt) )
b="${a[0]}$(printf ",%s" ${a[@]:1} )"

operation $b

exit 0
#/bin/bash
##函数模拟操作.sh
操作(){printf“%s\n”“$1”}
a=($)(给定:

在Perl中:

$ echo "$tgt" | perl -F',' -lane '$A[++$#A]=$F[1] if $F[0]=="1"; END{ print join(",", @A) }'
lakin,girding,instructorship,nonsegregation

通过awk过滤和获取单词,并使用粘贴将它们连接起来

$ awk -F ',' '$1==1{print $2}' word_list_numbered.txt  | paste -s -d ',' -
lakin,girding,instructorship,nonsegregation
$ ./operation.sh "$(awk -F ',' '$1==1{print $2}' word_list_numbered.txt  | paste -s -d ',' - )"
This script has 1 arguments
$

更新:用双引号括起来。

你的foo,bar测试似乎涵盖了这种可能性,但由于我无法重现这种可能性,你的系统上的
$IFS
值是多少?@swornabsent是的,我确实使用了$IFS路线。它显然设置为newline:@msw我正在尝试:选择所有以“1”开头的行中的单词;将它们连接起来,以逗号分隔;将它们作为操作参数应用。为什么这里的每个答案都会被否决?感谢您考虑这一点。我认为您的回答的关键部分是在解析脚本周围添加管道或双引号。管道或引号似乎会强制将结果视为字符串。我可以添加管道或者在我的grep/tr/sed解析脚本周围加上双引号,也得到一个参数。太好了,谢谢!谢谢你考虑这个问题。哇,如果我能用Perl思考,我肯定会更有效。谢谢你考虑这个问题!这是粘贴的一个很好的用法--我一直在寻找一种新的使用方法。不幸的是,它直到我将输出放在@user000001的答案中的双引号中,它才对我起作用。
$ echo "$tgt"
1,lakin
2,chesterfield
3,sparkplug
4,unscrawling
5,sukkah
1,girding
2,gripeful
3,historied
4,hypoglossal
5,nonmathematician
1,instructorship
2,loller
3,containerized
4,duodecimally
5,oligocythemia
1,nonsegregation
2,expecter
3,enterrologist
4,tromometry
5,salvia
$ echo "$tgt" | perl -F',' -lane '$A[++$#A]=$F[1] if $F[0]=="1"; END{ print join(",", @A) }'
lakin,girding,instructorship,nonsegregation
$ awk -F ',' '$1==1{print $2}' word_list_numbered.txt  | paste -s -d ',' -
lakin,girding,instructorship,nonsegregation
$ ./operation.sh "$(awk -F ',' '$1==1{print $2}' word_list_numbered.txt  | paste -s -d ',' - )"
This script has 1 arguments
$