如何使用 cut 命令 -f 标志作为反向

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

这是一个名为 a.txt 的文本文件,

ok.google.com  
abc.google.com

我想分别选择每个子域
cat a.txt |剪切-d“。” -f1(选择确定从左侧
cat a.txt |剪切-d“。” -f2 (它选择 google 从左侧

有什么办法,这样我就可以从右侧获得结果
猫 a.txt |剪切(这样它就可以选择com从右侧

This is a text file called a.txt

ok.google.com  
abc.google.com

I want to select every subdomain separately
cat a.txt | cut -d "." -f1 (it select ok From left side)
cat a.txt | cut -d "." -f2 (it select google from left side)

Is there any way, so I can get result from right side
cat a.txt | cut (so it can select com From right side)

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

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

发布评论

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

评论(4

耳钉梦 2025-01-27 10:14:06

可能有几种方法可以做到这一点,我现在能想到的一种方法是使用 rev + cut + rev 解决方案。这将通过 rev 命令反转输入,然后将字段分隔符设置为 . 并按照从左到右的顺序打印字段(但实际上由于使用,它们被反转了rev),然后再次将此输出传递给 rev 以获取其实际顺序。

rev Input_file | cut -d'.' -f 1 | rev

There could be few ways to do this, one way which I could think of right now could be using rev + cut + rev solution. Which will reverse the input by rev command and then set field separator as . and print fields as per they are from left to right(but actually they are reversed because of the use of rev), then pass this output to rev again to get it in its actual order.

rev Input_file | cut -d'.' -f 1 | rev
清风挽心 2025-01-27 10:14:06

您可以使用awk打印最后一个字段:

awk -F. '{print $NF}' a.txt

-f。将记录分隔符设置为“”。

$ nf是最后一个字段

,您可以直接将文件作为参数提供,因此您可以避免著名的“ 对其他字段的无用使用

,但是从最后一个字段来计算,您可以使用@sundeep在评论中建议的表达式或在用户指南中所述的表达式,
4.3非稳定字段编号。例如,要获取域,在tld之前,您可以从字段数nf

awk -F. '{ print $(NF-1) }' a.txt

You can use awk to print the last field:

awk -F. '{print $NF}' a.txt

-F. sets the record separator to "."

$NF is the last field

And you can give your file directly as an argument, so you can avoid the famous "Useless use of cat"

For other fields, but counting from the last, you can use expressions as suggested in the comment by @sundeep or described in the users's guide under
4.3 Nonconstant Field Numbers. For example, to get the domain, before the TLD, you can substract 1 from the Number of Fields NF :

awk -F. '{ print $(NF-1) }' a.txt
琉璃梦幻 2025-01-27 10:14:06

您可以将 sed 与量词一起使用,以重复分组值直到字符串末尾。

  • ( 启动群组
    • \.[^[:space:].]+ 匹配 1 个点和 1 个以上出现的任意字符(空格或点除外)
  • ){1} 关闭组,然后量词
  • $ 字符串结尾

示例

sed -E 's/(\.[^[:space:].]+){1}$//' file

输出

ok.google
abc.google

如果量词为 {2},则输出将为

ok
abc

You might use sed with a quantifier for the grouped value repeated till the end of the string.

  • ( Start group
    • \.[^[:space:].]+ Match 1 dot and 1+ occurrences of any char except a space or dot
  • ){1} Close the group followed by a quantifier
  • $ End of string

Example

sed -E 's/(\.[^[:space:].]+){1}$//' file

Output

ok.google
abc.google

If the quantifier is {2} the output will be

ok
abc
自此以后,行同陌路 2025-01-27 10:14:06

根据获取值后您想要执行的操作,您可以使用 bash 将域拆分为其组件数组:

#!/bin/bash

IFS=. read -ra comps <<< "ok.google.com"

echo "${comps[-2]}"
# or for bash < 4.2
echo "${comps[${#comps[@]}-2]}"
google

Depending on what you want to do after getting the values then you could use bash for splitting your domain into an array of its components:

#!/bin/bash

IFS=. read -ra comps <<< "ok.google.com"

echo "${comps[-2]}"
# or for bash < 4.2
echo "${comps[${#comps[@]}-2]}"
google
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文