分隔符分隔字符串_使用字符串作为分隔符的`cut`吗?

本文探讨了在Linux环境下如何使用字符串作为cut命令的分隔符,详细介绍了使用awk和gawk作为替代方案的方法,并解释了如何通过设置FPAT变量来使用正则表达式作为字段分隔符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分隔符分隔字符串

Is is possible to cut in Linux using a string as the delimiter?

可以在Linux中使用字符串作为分隔符来进行cut吗?

cut --delimiter="delim" -f 1,3

cut reports:

cut报告:

cut: the delimiter must be a single character

The most closest solution that I find is using awk/gawk:

我找到的最接近的解决方案是使用awk / gawk

awk -F 'delim' '{print $1; print $3}'

From the manual:

手册

-F fs
–field-separator fs Use fs for the input field separator (the value of the FS predefined variable).

-F fs
–field-separator fs将fs用作输入字段分隔符(FS预定义变量的值)。

An you can also use regular expression for the delimiter (field separator):

您还可以将正则表达式用作分隔符(字段分隔符):

Similarly, if the FPAT variable is set to a string representing a
regular expression, each field is made up of text that matches that
regular expression. In this case, the regular expression describes the
fields themselves, instead of the text that separates the fields.
Assigning a new value to FS or FIELDWIDTHS overrides the use of FPAT.

同样,如果FPAT变量设置为表示
正则表达式,每个字段均由与其匹配的文本组成
正则表达式。 在这种情况下,正则表达式描述了
字段本身,而不是用文本分隔字段。
为FS或FIELDWIDTHS分配新值将覆盖FPAT的使用。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/cut-using-a-string-as-the-delimiter/

分隔符分隔字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值