bash - 如何将由不同分隔符分隔的两个不同列提取到同一行中

标签 bash shell unix awk scripting

我在 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/

相关文章:

linux - 在 Linux 中查找多个文件并重命名它们

linux - 外包bash shell脚本函数

bash - 在命令替换中转义 AWK 中的反斜杠

python - 奇怪的 Python Cron 作业问题

linux - 如何在 shell 脚本中存储命令返回的值?

linux - 在手册页中使用 <> 来指示可选的空格

linux - 如何打印目录中第一个和最后一个文件名的前四个字符

bash - 在从管道执行的 bash 脚本中使用 read -p

linux - linux cp 中的通配符

Java 迭代日期对象的数组列表并将每个日期转换为其unix时间戳