Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.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
Linux 使用args从分隔文件捕获列_Linux_Bash_Sed_Awk - Fatal编程技术网

Linux 使用args从分隔文件捕获列

Linux 使用args从分隔文件捕获列,linux,bash,sed,awk,Linux,Bash,Sed,Awk,我有以下文件: 123 | xyz | abc | abc | abc | 567 321 | xyz | abc | abc | abc | 765 我写这个脚本是为了捕获first | last列: #!/bin/bash line=$(cat input.txt | sed 's/ //g') args=(${line//|/ }) x="${args[0]}" y="${args[11]}" echo $x echo $y 结果是: 123 765 如何获得以下结果: 123 |

我有以下文件:

123 | xyz | abc | abc | abc | 567
321 | xyz | abc | abc | abc | 765
我写这个脚本是为了捕获
first | last
列:

#!/bin/bash
line=$(cat input.txt | sed 's/ //g')
args=(${line//|/ })
x="${args[0]}"
y="${args[11]}"
echo $x
echo $y
结果是:

123
765
如何获得以下结果:

123 | 567
321 | 765
谢谢

您可以使用awk:

awk -F '[| ]+' '{print $1, $NF}' OFS=' | ' file
123 | 567
321 | 765
在纯BASH中执行此操作:

while IFS=' | ' read -a arr
    echo "${arr[0]} | ${arr[@]:(-1)}"
done < file
123 | 567
321 | 765
而IFS='|'read-a arr
回声“${arr[0]}|${arr[@]:(-1)}”
完成<文件
123 | 567
321 | 765

您可以使用
sed

sed 's/|.*|/|/' input.txt

在bash中有一种方法:

$ cat file 
123 | xyz | abc | abc | abc | 567
321 | xyz | abc | abc | abc | 765
$while IFS=“|”读取-ra行;做
回显“${line[0]}${line[${{line[@]}-1]}”
完成<文件
123 | 567
321 | 765

我可以用
切割

$ cut -f1,6 -d"|" input.txt
123 | 567
321 | 765

谢谢,但我已经用:
awk-F'|“{print$1”|“$6$11}”input.txt
awk'{print$1,$10,$11}'input.txt
我想使用post和
sed
awk
中编写的bash结构,但正如您可以看到的那样,使用这个简单的
awk
您不需要任何其他东西。如果您想在bash中这样做,它需要一个循环。您可以使用
${arr[2]}
${arr[1]}轻松获得第3列或第2列
非常感谢你,greatThank你..但是我如何更改这部分的脚本:
sed的///g'
这是一个练习
sed的//.*;s///g'input.txt
sed“s//|.*s//|//'input.txt
在本例中直接将空格包含在第一个模式中(没有其他空格)
$ cut -f1,6 -d"|" input.txt
123 | 567
321 | 765