UNIX Bash-从文件中的特定字符串中删除双引号 [英] UNIX Bash - Removing double quotes from specific strings within a file

查看:256
本文介绍了UNIX Bash-从文件中的特定字符串中删除双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我已经格式化了,因为我以前没有在这里发布过.

Apologies if I've formatted this terribly as I've not posted here before.

我正在尝试编辑文件,以删除双引号,该双引号包裹在多个不同长度的字符串周围.其中一些字符串包括大写字母和空格,通常我只使用全局搜索并替换,但是有些字符串不能按需删除双引号.

I'm attempting to edit a file to remove double quotation marks that are wrapped around multiple strings of varied lengths. Some of these strings include capitalisation and white-space, normally I would just use a global search and replace, however, some of the strings CAN'T have the double quotes removed as they're required.

有关文件的摘录在这里:

An extract of the file in question is here:

"tplan"."external_plan_ref" "Plan ID",
            'CMP' CMP,
            "bd"."NAME" "Business Divison",
            "reg"."NAME" "Region",
            placeholder1 "Placeholder 1",
            "ct"."COUNTRY_NAME" "COUNTRY",
            city "City",
            placeholder2 "Placeholder 2",
            placeholder3 "Placeholder 3",
            placeholder4 "Placeholder 4",

后的换行字符串.是需要删除双引号的字符串.例如:

The wrapped string after the . are the strings which require the double quotes removed. Ex:

."NAME"

我尝试使用awk并用正则表达式sed来确定需要替换的内容,然后再替换它,但是我没有运气,并且一直努力将其包围.任何建议或建议将不胜感激.谢谢!

I've attempted to use awk and sed with a regex to identify what needs replacing and then for it to be replaced, but I've had no luck and have struggled to wrap my head around it. Any advice or recommendations would be truly appreciated. Thank you!

样本输出:

 "tplan".external_plan_ref "Plan ID",
            'CMP' CMP,
            "bd".NAME "Business Divison",
            "reg".NAME "Region",
            placeholder1 "Placeholder 1",
            "ct".COUNTRY_NAME "COUNTRY",
            city "City",
            placeholder2 "Placeholder 2",
            placeholder3 "Placeholder 3",
            placeholder4 "Placeholder 4",

推荐答案

这可能对您有用(GNU sed):

This might work for you (GNU sed):

sed 's/\."\([^"]*\)"/.\1/g' file

匹配一个句点,后跟一个双引号的字符串,并替换为一个句点,并且该字符串减去双引号.

Match on a period, followed by a double quoted string(s) and replace by a period and the string less the double quotes.

句号必须加引号/转义,否则匹配任何字符.

N.B. The period needs to be quoted/escaped otherwise it matches any character.

这篇关于UNIX Bash-从文件中的特定字符串中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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