如何使用 cut 命令 -f 标志作为反向
这是一个名为 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 separatelycat 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能有几种方法可以做到这一点,我现在能想到的一种方法是使用
rev
+cut
+rev
解决方案。这将通过rev
命令反转输入,然后将字段分隔符设置为.
并按照从左到右的顺序打印字段(但实际上由于使用,它们被反转了rev
),然后再次将此输出传递给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 byrev
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 ofrev
), then pass this output torev
again to get it in its actual order.您可以使用
awk
打印最后一个字段:-f。
将记录分隔符设置为“”。$ nf
是最后一个字段,您可以直接将文件作为参数提供,因此您可以避免著名的“ 对其他字段的无用使用”
,但是从最后一个字段来计算,您可以使用@sundeep在评论中建议的表达式或在用户指南中所述的表达式,
4.3非稳定字段编号。例如,要获取域,在tld之前,您可以从字段数
nf
:You can use
awk
to print the last field:-F.
sets the record separator to "."$NF
is the last fieldAnd 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
:您可以将 sed 与量词一起使用,以重复分组值直到字符串末尾。
(
启动群组\.[^[:space:].]+
匹配 1 个点和 1 个以上出现的任意字符(空格或点除外)){1}
关闭组,然后量词$
字符串结尾示例
输出
如果量词为
{2}
,则输出将为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 stringExample
Output
If the quantifier is
{2}
the output will be根据获取值后您想要执行的操作,您可以使用 bash 将域拆分为其组件数组:
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: