简体   繁体   English

使用awk或sed将管道定界符替换为空格

[英]replace a pipe delimiter with a space using awk or sed

I have a pipe delimited file with a sample lines like below; 我有一个用管道分隔的文件,带有下面的示例行;

/u/chaintrk/bri/sh/picklist_autoprint.sh|-rwxrwxr-x|bdr|bdr|2665|Oct|23|14:04|3919089454
/u/chaintrk/bri/sh/generate_ct2020.pl|-rwxrwxr-x|bdr|bdr|15916|Oct|23|14:04|957147508

is there a way that awk or sed can transform the lines into the output like below where the pipe between the month and the date was replaced by space? awk或sed是否有办法将行转换为输出,如下所示,其中月份和日期之间的管道被空格替换了?

/u/chaintrk/bri/sh/picklist_autoprint.sh|-rwxrwxr-x|bdr|bdr|2665|Oct 23|14:04|3919089454
/u/chaintrk/bri/sh/generate_ct2020.pl|-rwxrwxr-x|bdr|bdr|15916|Oct 23|14:04|957147508

With GNU sed: 使用GNU sed:

sed -E 's/(\|[A-Z][a-z]{2})\|([0-9]{1,2}\|)/\1 \2/' file

Output: 输出:

/u/chaintrk/bri/sh/picklist_autoprint.sh|-rwxrwxr-x|bdr|bdr|2665|Oct 23|14:04|3919089454
/u/chaintrk/bri/sh/generate_ct2020.pl|-rwxrwxr-x|bdr|bdr|15916|Oct 23|14:04|957147508

If you want to edit file "in place" add sed's option -i . 如果要“就地”编辑文件,请添加sed的选项-i

Yes, it is possible to change a "|" 是的,可以更改“ |” with an space. 与空间。
The real problem is to identify which of the field(s) to change. 真正的问题是确定要更改哪个字段。

Are those always the 6th and 7th? 那些总是第六和第七吗? If so, this works: 如果是这样,这可行:

awk -vFS='|' '{sub($6"|"$7,$6" "$7)}1' file

Are those with a text Upper-lower-lower followed by a 1 or 2 digits ? 那些文本的Upper-lower-lower后跟1 or 2 digits吗? If so, this other works: 如果是这样,其他工作原理:

gawk '{c="[|]([[:upper:]][[:lower:]]{2})[|]([0-9]{1,2})[|]";print gensub(c,"|\\1 \\2|",1,$0)}' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM