xargs 和 cut:将 csv 的“cut”字段获取到 bash 变量

发布于 2025-01-14 06:20:40 字数 579 浏览 4 评论 0 原文

我将 xargscut 结合使用,但我不确定如何将 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

我很困惑,我似乎无法实现这一目标。

I am using xargs in conjuction with cut but I am unsure how to get the output of cut to a variable which I can pipe to use for further processing.

So, I have a text file like so:

test.txt:

/some/path/to/dir,filename.jpg
/some/path/to/dir2,filename2.jpg
...

I do this:

cat test.txt | xargs -L1 | cut -d, -f 1,2
/some/path/to/dir,filename.jpg

but what Id like to do is:

cat test.txt | xargs -L1 | cut -d, -f 1,2 | echo $1 $2

where $1 and $2 are /some/path/to/dir and filename.jpg

I am stumped that I cannot seem to able to achieve this..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2025-01-21 06:20:40

试试这个:

cat test.txt | awk -F, '{print $1, $2}'

来自 man xargs

xargs [-L 数字] [实用程序 [参数 ...]]

-L数字
为读取的每个数字个非空行调用实用程序

来自 man awk :

awk 扫描每个输入文件,查找与 prog 或一个或多个指定文件中按字面指定的一组模式中的任何一个匹配的行作为-f progfile

因此,您不必使用 xargs -L1,因为您无需传递 utility 进行调用。

同样来自 man awk

-F fs 选项将输入字段分隔符定义为正则表达式 fs

所以awk -F,可以代替cut -d,部分。

这些字段表示为 $1$2、...,而 $0 表示整行。

因此,$1 用于第一列,$2 用于第二列。

动作是一系列语句。声明可以是以下之一:

print [ 表达式列表 ] [ > 表情]

空的表达式列表代表$0

print 语句将其参数打印在标准输出上(如果 > file>> file 则打印在文件上)存在或在管道上(如果 | cmd 存在),由当前输出字段分隔符分隔,并由输出记录分隔符终止。

将所有这些放在一起,cat test.txt | awk -F, '{print $1, $2}' 将实现您想要的。

Try this:

cat test.txt | awk -F, '{print $1, $2}'

From man xargs:

xargs [-L number] [utility [argument ...]]

-L number
Call utility for every number non-empty lines read.

From man awk:

Awk scans each input file for lines that match any of a set of patterns specified literally in prog or in one or more files specified as -f progfile.

So you don't have to use xargs -L1 as you don't pass the utility to call.

Also from man awk:

The -F fs option defines the input field separator to be the regular expression fs.

So awk -F, can replace the cut -d, part.

The fields are denoted $1, $2, ..., while $0 refers to the entire line.

So $1 is for the first column, $2 is for the second one.

An action is a sequence of statements. A statement can be one of the following:

print [ expression-list ] [ > expression ]

An empty expression-list stands for $0.

The print statement prints its argument on the standard output (or on a file if > file or >> file is present or on a pipe if | cmd is present), separated by the current output field separator, and terminated by the output record separator.

Put all these together, cat test.txt | awk -F, '{print $1, $2}' would achieve that you want.

筱武穆 2025-01-21 06:20:40

您可能想说这样的话:

#!/bin/bash

while IFS=, read -r f1 f2; do
    echo ./mypgm -i "$f1" -o "$f2"
done < test.txt
  • IFS=, read -r f1 f2test.txt 中逐一读取一行,
    用逗号分割该行,然后分配变量 f1f2
    到田野。
  • echo .. 行用于演示目的。更换
    使用 $f1$f2 与您想要的命令对齐。

You may want to say something like:

#!/bin/bash

while IFS=, read -r f1 f2; do
    echo ./mypgm -i "$f1" -o "$f2"
done < test.txt
  • IFS=, read -r f1 f2 reads a line from test.txt one by one,
    splits the line on a comma, then assigns the variables f1 and f2
    to the fields.
  • The line echo .. is for the demonstration purpose. Replace the
    line with your desired command using $f1 and $f2.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文