bash sed 用 NULL 替换空白

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14820028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:31:39  来源:igfitidea点击:

sed replace empty space with NULL

linuxbashsed

提问by Deano

Using sed

使用 sed

  1. how can I replace \N with NULL

  2. How can I replace the empty space || with NULL.

    |200|0||0|\N||^A|0|\N||
    
  1. 如何用 NULL 替换 \N

  2. 我怎样才能替换空的空间 || 与 NULL。

    |200|0||0|\N||^A|0|\N||
    

desired output

期望的输出

    |200|0|NULL|0|NULL|NULL|^A|0|NULL|NULL|

采纳答案by Thor

You need a slightly modified version of this. Something like this should work:

你需要略加修改。这样的事情应该工作:

sed ':a; s:|\(\N\)\?|:|NULL|:g; ta'

回答by mikyra

I think the command you are looking for might be called "tr". Try

我认为您正在寻找的命令可能称为“tr”。尝试

tr \n \0

回答by Anew

cat file | sed 's/\N/NULL/g' | awk -F\| '{for(i=2;i<=NF;i++){ if($i==""){ printf "|NULL"} else{  printf "|%s", $i}}; printf "|\n"}'

回答by Mirage

with awk

用 awk

awk 'gsub("\\N","NULL");gsub("\|\|","|NULL|")' temp.txt