bash - 如何使用 sh 迭代 awk 的每个输出?

标签 bash shell awk sh

我有一个 test.sh通过文本文件 awks 的文件 test.bed :

1   31  431
1   14  1234
1   54  134
2   435 314
2   314 414


while read line;do
    echo $line  
    # ... more code ... #
done < <(awk -F '\t' '$1 == 1 {print $0}' test.bed )

使用 bash test.sh 运行它有效,但使用 sh test.sh它给出了语法错误。我怎么能用 sh 做上述事情而不是 bash ?

它给出的错误是
test.sh: line 5: syntax error near unexpected token `<'
test.sh: line 5: `done < <(awk -F '\t' '$1 == 1 {print $0}' test.bed )'

最佳答案

Process Substitutionbash扩展 - 未由 POSIX 指定.这是sh兼容的..

#!/bin/sh

awk -F '\t' '$1 == 1 {print $0}' test.bed  | while read line; do
echo $line
    # ... more code ... #
done

关于bash - 如何使用 sh 迭代 awk 的每个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793460/

相关文章:

bash - Shell 脚本在 bash 中有效,但在 ksh 中无效

parsing - ctags:获取C函数结束行号

linux - 通过命令 shell 脚本使用 tshark 进行转换

javascript - Nodejs && JSON : select value by key

带有导出命令和通过 crontab 通知发送的 Shell 脚本不起作用。导出的变量由命令设置

perl - shell 魔法通缉: format output of hexdump in a pipe

linux - 如何在第 n 行文件的中间插入文本

linux - awk 系统语法抛出错误

linux - 使用 RSYNC 进行错误处理

bash - 使用 AWK 使用来自多个输入文件的字符串作为 CSV 文件中选择列的搜索条件