使用剪切命令将两条回波线合并为一行

问题描述 投票:0回答:2

现在我要分别打印两行,我需要将其打印在一行中。

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
unix echo line cut
2个回答
0
投票

您也可以通过awk命令来实现:

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

-F:用于选择在此情况下为/的定界符。然后我们在打印第5列和第9列。


0
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.