我在 unix txt 文件中有以下数据集:
/this/is/a/directory_name/which/i/want:listen= tcp://someurl
/this/is/a/another_directory_name/which/i/want:listen= tcp://someotherurl
基本上预期的输出是:
directory_name <whitespace> tcp://someurl
another_directory_name <whitespace> tcp://someotherurl
下面是我正在尝试但只获取 url 或目录名的命令
cat the_file.txt|awk -F '/' '{print $5}'
cat the_file.txt|awk -F '=' '{print $2}'
有没有办法同时执行上述两个命令并在同一行中获得输出? 非常感谢
最佳答案
只需保留第一个命令并对第二个命令进行调整:split基于 =
的全文并打印第二个结果字段:
$ awk -F'/' '{split($0, a,"="); print $5, a[2]}' file
directory_name tcp://someurl
another_directory_name tcp://someotherurl
如果 =
很多,则最后一个:
$ awk -F'/' '{n=split($0, a,"="); print $5, a[n]}' file
directory_name tcp://someurl
another_directory_name tcp://someotherurl
关于bash - 如何将由不同分隔符分隔的两个不同列提取到同一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388422/