xargs 和 cut:将 csv 的“cut”字段获取到 bash 变量
我将 xargs
与 cut
结合使用,但我不确定如何将 cut
的输出获取到一个变量,我可以通过管道将其用于进一步加工。
所以,我有一个像这样的文本文件:
test.txt
:
/some/path/to/dir,filename.jpg
/some/path/to/dir2,filename2.jpg
...
我这样做:
cat test.txt | xargs -L1 | cut -d, -f 1,2
/some/path/to/dir,filename.jpg
但我想做的是:
cat test.txt | xargs -L1 | cut -d, -f 1,2 | echo $1 $2
其中 $1 和 $2 是 /some/path/to/dir< /code> 和
filename.jpg
我很困惑,我似乎无法实现这一目标。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
来自
man xargs
:来自 man awk :
因此,您不必使用
xargs -L1
,因为您无需传递utility
进行调用。同样来自
man awk
:所以
awk -F,
可以代替cut -d,
部分。因此,
$1
用于第一列,$2
用于第二列。将所有这些放在一起,
cat test.txt | awk -F, '{print $1, $2}'
将实现您想要的。Try this:
From
man xargs
:From
man awk
:So you don't have to use
xargs -L1
as you don't pass theutility
to call.Also from
man awk
:So
awk -F,
can replace thecut -d,
part.So
$1
is for the first column,$2
is for the second one.Put all these together,
cat test.txt | awk -F, '{print $1, $2}'
would achieve that you want.您可能想说这样的话:
IFS=, read -r f1 f2
从test.txt
中逐一读取一行,用逗号分割该行,然后分配变量
f1
和f2
到田野。
echo ..
行用于演示目的。更换使用
$f1
和$f2
与您想要的命令对齐。You may want to say something like:
IFS=, read -r f1 f2
reads a line fromtest.txt
one by one,splits the line on a comma, then assigns the variables
f1
andf2
to the fields.
echo ..
is for the demonstration purpose. Replace theline with your desired command using
$f1
and$f2
.