【问题标题】:Bash: Multiple Lines from File Into One Command ArgumentBash:从文件到一个命令参数的多行
【发布时间】:2015-09-12 04:08:13
【问题描述】:

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

测试用例:

[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 并不是最佳选择——我在问这个问题,所以我不必这样做

【问题讨论】:

  • 您的 foo,bar 测试似乎涵盖了这种可能性,但由于我无法重现这一点,您系统上的 $IFS 的值是多少?
  • @swornabsent 是的,我确实走上了 $IFS 路线。它显然设置为换行符:
  • @msw 我正在尝试:选择以“1”开头的所有行中的单词;将它们用逗号分隔;将它们用作 operation.sh 的参数
  • 为什么这里的每个答案都投了反对票?

标签: bash shell


【解决方案1】:

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/,$//')"

【讨论】:

  • 感谢您考虑这个问题。我认为您响应的关键部分是管道或在解析脚本周围加上双引号。管道或引号似乎强制将结果视为字符串。我可以在我的 grep/tr/sed 解析脚本周围加上 pip 或双引号,也可以得到一个参数。太好了,谢谢!
【解决方案2】:

给定:

$ 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

【讨论】:

  • 感谢您考虑这个问题。哇,如果我能在 Perl 中思考,我肯定会更有效。
【解决方案3】:

awk 的替代方法是在 bash 中使用 命令替换 来用文件的内容填充数组,然后再次将所有行连接成一个逗号分隔的字符串以传递给 @ 987654322@:

#!/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

【讨论】:

    【解决方案4】:

    通过 awk 过滤并获取单词,然后使用 paste 将它们连接起来。

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

    更新:用双引号括起来。

    【讨论】:

    • 感谢您考虑这个问题!这是对粘贴的一种非常好的使用——我一直在寻找一种新的使用方式。不幸的是,直到我将输出放在@user000001 的答案中的双引号中,它才对我有用。
    猜你喜欢
    • 2020-03-29
    • 2015-07-07
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多