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

时间:2015-09-11 18:01:11

标签: 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,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不是最佳选择 - 我问这个问题所以我不必这样做

4 个答案:

答案 0 :(得分:2)

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

测试:

$ cat operation.sh 
echo "This script has $# arguments"
echo "$@"

$ awk -F, -v ORS=, '$1==1{print $2}' file | sed 's/,$//' | xargs ./operation.sh 
This script has 1 arguments
lakin,girding,instructorship,nonsegregation

$ cat file
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

如果没有xargs,那就是:

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

答案 1 :(得分:1)

awk的替代方法是在bash中使用命令替换来填充包含文件内容的数组,然后再将所有行连接到一个逗号分隔的字符串中以通过到operation.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

<强>输出

$ bash csvlist.sh
1,lakin,2,chesterfield,3,sparkplug,4,unscrawling, ..<snip>.. 5,salvia

答案 2 :(得分:1)

假设:

$ 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

Perl:

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

答案 3 :(得分:0)

过滤并通过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
$

更新:用双引号括起来。