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



现在我分别打印两行,我需要单行打印。

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

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

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

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

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

最新更新