使用 cut 命令将两条 echo 行合并为一行

Merge the two echo lines into one line using cut command

现在分两行打印,需要单行打印

echo $line cut -d "/" -f5 
echo $line | cut -d "/" -f9

我需要一行中的 f5 和 f9 值。

f5 --> domain_name
f9 --> service_name

预期输出:

domain_name service_name
domain_name service_name
domain_name service_name
domain_name service_name

man cut 教育我们:

-f, --fields=LIST
       select  only  these  fields;   also print any line that contains 
       no delimiter character, unless the -s option is specified

所以,在中使用逗号:

$ cut -d / -f 5,9 file 

是正确答案。但是,输出分隔符也将是 /

domain_name/service_name

除非你单独定义。 man cut,我们来了:

--output-delimiter=STRING
       use STRING as the output delimiter the default is to use the input delimiter

所以:

$ cut -d / -f 5,9 --output-delimiter=\  file  # or --output-delimiter=" "

应该输出:

domain_name service_name

您也可以通过 awk 命令实现此目的:

echo $line | awk -F"/" '{ print , }'

-F:这用于select分隔符,在本例中为/。然后我们打印第 5 和第 9 列。