查找制表符 (\t) 之间的文本作为分隔符

tac*_*omi 3 sed awk text-processing

我认为这会很简单,但不知道如何去做。

设想

我有一个.csv带有id_user, text,id_group列的单个文件,其中每一列都由tabs如下分隔:

"123456789"        "Here's the field of the text, also contains comma"        "10"
"987456321"        "Here's the field of the text, also contains comma"        "10"
"123654789"        "Here's the field of the text, also contains comma"        "11"
"987456123"        "Here's the field of the text, also contains comma"        "11"
Run Code Online (Sandbox Code Playgroud)

如何找到文本?

试图

awk

我正在寻找一种指定print $n分隔符的方法,如果可以的话,一个选项将是

$ awk -d '\t' '{print $2}' file.csv | sed -e 's/"//gp'
Run Code Online (Sandbox Code Playgroud)

选项-d的分隔符printsed取出的分隔符在哪里"

Tho*_*hor 10

制表符分隔符

你不需要sedor awk,一个简单的cut就可以:

cut -f2 infile
Run Code Online (Sandbox Code Playgroud)

awk

如果要使用 awk,提供定界符的方法是通过-F参数或作为FS=后缀:

awk -F '\t' '{ print $2 }' infile
Run Code Online (Sandbox Code Playgroud)

或者:

awk '{ print $2 }' FS='\t' infile
Run Code Online (Sandbox Code Playgroud)

所有情况下的输出:

cut -f2 infile
Run Code Online (Sandbox Code Playgroud)

引号分隔符

如果文件中的双引号一致,即字段中没有嵌入双引号,则可以将它们用作分隔符并避免在输出中使用它们,例如:

cut -d\" -f4 infile
Run Code Online (Sandbox Code Playgroud)

awk

awk -F\" '{ print $4 }' infile
Run Code Online (Sandbox Code Playgroud)

两种情况下的输出:

awk -F '\t' '{ print $2 }' infile
Run Code Online (Sandbox Code Playgroud)