[英]Using awk to separate an output containing a tab and a “/” separators into a delimited format
I'll appreciate help in converting this output to a pipe delimited我将不胜感激将此 output 转换为 pipe 分隔的帮助
I have the following output我有以下 output
abcde1234 /path/A/file1
test23455 /path/B/file2345
But I would like in但我想在
abcde1234|file1
test23455|file2345
In awk
, If you set FS
as [[:blank:]]+/|/
you can print the first and last fields:在awk
中,如果将FS
设置为[[:blank:]]+/|/
可以打印第一个和最后一个字段:
awk -v FS='[[:blank:]]+/|/' -v OFS='|' '{print $1, $NF}' file
abcde1234|file1
test23455|file2345
Here is a one-liner awk
solution:这是一个单行awk
解决方案:
awk -v FS='[ \t].*/' -v OFS='|' '{$1=$1}1' file
and, a sed
one-liner:以及sed
:
sed 's%[[:blank:]].*/%|%' file
and a pure bash
one和一个纯bash
一个
while read -r; do echo "${REPLY%%[[:blank:]]*}|${REPLY##*/}"; done < file
try to use cut
♀️.尝试使用cut
♀️。
abcde1234 /path/A/file1
test23455 /path/B/file2345
while IFS= read -r line; do
value1=$(echo $line | cut -d ' ' -f1)
value2=$(echo $line | cut -d '/' -f4)
printf "$value1 $value2\n"
done < <(cat list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.