Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash while循环中的sed命令不';t写入输出_Bash_Sed_While Loop - Fatal编程技术网

Bash while循环中的sed命令不';t写入输出

Bash while循环中的sed命令不';t写入输出,bash,sed,while-loop,Bash,Sed,While Loop,我有这个输入文件 gb|KY798440.1| gb|KY842329.1| MG082893.1 MG173246.1 我想得到“|”之间的所有字符,或者如果没有“|”的话,得到整行之间的所有字符。这是一个期望的输出,看起来像 KY798440.1 KY842329.1 MG082893.1 MG173246.1 我写道: while IFS= read -r line; do if [[ $line == *\|* ]] ; then sed 's/.*\|\(.*\)\

我有这个输入文件

gb|KY798440.1|
gb|KY842329.1|
MG082893.1
MG173246.1
我想得到“|”之间的所有字符,或者如果没有“|”的话,得到整行之间的所有字符。这是一个期望的输出,看起来像

KY798440.1
KY842329.1
MG082893.1
MG173246.1
我写道:

while IFS= read -r line; do
    if [[ $line == *\|* ]] ; then
    sed 's/.*\|\(.*\)\|.*/\1/' <<< $line >> output_file
    else echo $line >> output_file
     fi
done < input_file
(注意:
空行
表示实际的空行-它实际上并不写“空行”)

sed命令仅适用于一个示例(即,
sed的/*\\\\\(.*\)\\\\\\\\\\\/\1/'输出文件
似乎有效。

$ sed 's/^[^|]*|\||[^|]*$//g' file
输出:

KY798440.1
KY842329.1
MG082893.1
MG173246.1
你可以

sed '/|/s/[^|]*|\([^|]*\)|.*/\1/' input


为什么要使用while/read循环而不是让sed处理文件呢?哦,那太尴尬了。我想我试过了……不过谢谢!!
sed '/|/s/[^|]*|\([^|]*\)|.*/\1/' input
awk 'NF>1 {print $2} NF < 2 { print $1}' FS=\| input
sed -e 's/[^|]*|//' -e 's/|.*//' input