与除非在双引号单卡更换空白 - 第二部分 [英] Replacing whitespace with single tab unless in double quotes - Part II

查看:118
本文介绍了与除非在双引号单卡更换空白 - 第二部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该职位<一个href=\"http://stackoverflow.com/questions/33870465/replacing-whitespace-with-single-tab-unless-in-double-quotes\">Replacing除非在双引号中讨论的 AWK 的code单卡的空白与字符串中的所有空格双引号外面采用单制表符代替。两个答案被张贴,将正确的行为表示更换 - 除非若干或者非字母字符的组合是present输入字符串

有谁知道什么$ P $从所需更换pvents命令以及如何克服呢?

 &GT;猫文件
#foo的酒吧名称产品巴兹qux#非字母字符:无
#foo_bar这样的名称。名称= foo的;产品=巴兹qux#非字母字符:下划线,点,等号,分号
#富吧+名称= foo,那么产品=巴兹qux#非字母字符:破折号,再加上,等号,逗号,MULT。空格

所需的输出(当通过管道输送到猫-A

 #^ foo的伊巴尔^ ^ INAME ^ Iproduct我巴兹qux$
#^ foo_bar这样一^ INAME = foo的;产品=巴兹qux$
#富吧^我^ + = INAME富,产品=巴兹qux$

实际输出(当通过管道输送到猫-A

 &GT; AWK -v FPAT =[^] *| [^ [:空白:]] +'-v OFS ='\\ t''{$ 1 = $ 1} 1文件|猫-A
#^ foo的伊巴尔^ ^ INAME ^ Iproduct我巴兹qux$
#^ foo_bar这样一^ INAME = foo的;产品=巴兹^ Iqux$#原文如此!
#富吧^我^ + = INAME富,产品=巴兹^ Iqux$#原文如此!&GT; AWK'{$ 1 = $ 1} 1'= OFS'\\ T'FPAT =[^] +| [^] +'文件|猫-A
#^ foo的伊巴尔^ ^ INAME ^ Iproduct我巴兹qux$
#^ foo_bar这样一^ INAME = foo的;产品=巴兹^ Iqux$#原文如此!
#富吧^我^ + = INAME富,产品=巴兹^ Iqux$#原文如此!


解决方案

它无关的特殊字符,更改为FPAT:

  FPAT ='([^] *| [^ [:空白:]] +)+'

问题是,字段可能包含引号引起来的字符串其它非空间像名称= foo的报价之外的字符;产品=巴兹qux。旧格局并没有描述这些类型的字段。

The post "Replacing whitespace with single tab unless in double quotes" discussed awk code with which all whitespaces in a string outside double quotes were replaced with single tab characters. Two answers were posted that would correctly conduct said replacement -- unless a certain number or combination of non-letter characters is present in the input strings.

Does anyone know what prevents the command from the desired replacement and how to overcome it?

> cat file
# foo bar Name product "baz qux"            # Non-letter chars: none
# foo_bar . Name=foo;product="baz qux"      # Non-letter chars: underscore, dot, equal sign, semicolon
# foo-bar +   Name=foo,product="baz   qux"  # Non-letter chars: dash, plus, equal sign, comma, mult. whitespaces

Desired output (when piped to cat -A):

# foo^Ibar^IName^Iproduct^I"baz qux"$
# foo_bar^I.^IName=foo;product="baz qux"$
# foo-bar^I+^IName=foo,product="baz   qux"$

Actual output (when piped to cat -A):

> awk -v FPAT='"[^"]*"|[^[:blank:]]+' -v OFS='\t' '{$1=$1} 1' file | cat -A
# foo^Ibar^IName^Iproduct^I"baz qux"$
# foo_bar^I.^IName=foo;product="baz^Iqux"$  # sic!
# foo-bar^I+^IName=foo,product="baz^Iqux"$  # sic!

> awk '{$1=$1}1' OFS='\t' FPAT='"[^"]+"|[^ ]+' file | cat -A
# foo^Ibar^IName^Iproduct^I"baz qux"$
# foo_bar^I.^IName=foo;product="baz^Iqux"$  # sic!
# foo-bar^I+^IName=foo,product="baz^Iqux"$  # sic!

解决方案

It has nothing to do with special characters, change FPAT to:

FPAT='("[^"]*"|[^[:blank:]"]+)+'

The problem was that fields may contain strings enclosed between quotes and other non-space characters outside of the quotes like Name=foo;product="baz qux". The old pattern didn't describe these kind of fields.

这篇关于与除非在双引号单卡更换空白 - 第二部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆