剪切问题(unix)

发布于 2024-09-04 02:06:01 字数 289 浏览 2 评论 0原文

我在 cut 方面遇到了奇怪的问题 我编写了脚本,其中有 row:

... | cut -d" " -f3,4 >! out

cut 收到此数据(我用 echo 检查过)

   James             James              033333333 0              0.00

,但我在 out 中收到空行,有人可以解释为什么吗?

I've got strange problem with cut
I wrote script, there I have row:

... | cut -d" " -f3,4 >! out

cut recieves this data (I checked it with echo)

   James             James              033333333 0              0.00

but I recieve empty lines in out, can somebody explain why?

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

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

发布评论

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

评论(4

救星 2024-09-11 02:06:01

您需要压缩空格序列,以便每个空格字符串都被单个空格替换。 tr 命令的 -s(挤压)选项非常完美为了这:

$ ... | tr -s " " | cut -d" " -f3,4 >! out

You need to compress out the sequences of spaces, so that each string of spaces is replaced by a single space. The tr command's -s (squeeze) option is perfect for this:

$ ... | tr -s " " | cut -d" " -f3,4 >! out
桜花祭 2024-09-11 02:06:01

如果您想要文本文件中的字段,awk 几乎总是答案:

... | awk '{print $3" "$4}'

例如:

$ echo 'James     James      033333333 0              0.00' | cut -d" " -f3,4

$ echo 'James     James      033333333 0              0.00' | awk '{print $3" "$4}'
033333333 0

If you want fields from a text file, awk is almost always the answer:

... | awk '{print $3" "$4}'

For example:

$ echo 'James     James      033333333 0              0.00' | cut -d" " -f3,4

$ echo 'James     James      033333333 0              0.00' | awk '{print $3" "$4}'
033333333 0
吾家有女初长成 2024-09-11 02:06:01

Cut 不会将多个空格视为单个空格,因此它匹配空格之间的“虚无”。

当你省略 >! 时,你会得到空行吗?出 部分?即,您的目标字段正确吗?

如果您的输入字符串使用固定间距,您可能需要使用 cut -c 4-10,15-20 | tr -d ' ' 提取字符组 4-10 和 15-20 并从中删除空格。

Cut doesn't see multiple spaces as single space, so it matches "nothingness" between spaces.

Do you get empty lines when you leave out >! out part? Ie, are you targeting correct fields?

If your input string uses fixed spacing, you might want to use cut -c 4-10,15-20 | tr -d ' ' to extract character groups 4-10 and 15-20 and remove spaces from them..

泼猴你往哪里跑 2024-09-11 02:06:01
... | grep -o "[^ ]*"

将提取字段,每个字段位于单独的行上。然后你可能会头/尾他们。不确定是否再次将它们放在同一条线上。

... | grep -o "[^ ]*"

will extract fields, each on separate line. Then you might head/tail them. Not sure about putting them on the same line again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文