SED:用下划线引号内更换空间 [英] sed: replace spaces within quotes with underscores

查看:164
本文介绍了SED:用下划线引号内更换空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入(例如,从使用ifconfig run0扫描在OpenBSD),有由空格分隔的某些领域,但有些领域本身包含空格(幸运的是, ,包含空格等领域始终用引号括起来)。

我需要引号内的空间,隔板的空间区别开来。我们的想法是用下划线引号内更换空间。

示例数据:

 %猫的/ tmp / ifconfig_scan | fgrep一样NWID |切-f3
Websense的NWID陈6 BSSID 00:22:7F:XX:XX:XX59分贝54M short_ preamble,short_slottime
NWID合勤瓒8 BSSID抄送:5D:4E:XX:XX:XX5分贝54M隐私,short_slottime
NWID的myTouch 4G热点瓒11 BSSID D8:B3:77:XX:XX:XX49分贝54M隐私,short_slottime

这并不最终处理的方式我想,因为我还没有与下划线引号但内更换的空间:

 %猫的/ tmp / ifconfig_scan | fgrep一样NWID |切-f3 | \\
    切-s -d''-f 2,4,6,7,8 |排序-n -k4
的myTouch热点11 BSSID D8:B3:77:XX:XX:XX
合勤8抄送:5D:4E:XX:XX:XX5分贝54M
Websense的6 00:22:7F:XX:XX:XX59分贝54M


解决方案

对于 SED -only解决方案(我不一定提倡),请尝试:

 回声A BC D EF GH I'| \\
SED':A; S / ^ \\(\\([^] *[^] *[^] * \\)* [^] *[^] * \\)/ \\ 1_ /; TA
A Bc_d_eF Gh_i

翻译:


  • 开始在该行的开头。

  • 查找模式垃圾的垃圾,重复零次或更多次,其中垃圾没有报价,然后按垃圾的垃圾空间

  • 替换最后空间_

  • 如果成功了,跳回到起点。

I have input (for example, from ifconfig run0 scan on OpenBSD) that has some fields that are separated by spaces, but some of the fields themselves contain spaces (luckily, such fields that contain spaces are always enclosed in quotes).

I need to distinguish between the spaces within the quotes, and the separator spaces. The idea is to replace spaces within quotes with underscores.

Sample data:

%cat /tmp/ifconfig_scan | fgrep nwid | cut -f3
nwid Websense chan 6 bssid 00:22:7f:xx:xx:xx 59dB 54M short_preamble,short_slottime
nwid ZyXEL chan 8 bssid cc:5d:4e:xx:xx:xx 5dB 54M privacy,short_slottime
nwid "myTouch 4G Hotspot" chan 11 bssid d8:b3:77:xx:xx:xx 49dB 54M privacy,short_slottime

Which doesn't end up processed the way I want, since I haven't replaced the spaces within the quotes with the underscores yet:

%cat /tmp/ifconfig_scan | fgrep nwid | cut -f3 |\
    cut -s -d ' ' -f 2,4,6,7,8 | sort -n -k4
"myTouch Hotspot" 11 bssid d8:b3:77:xx:xx:xx
ZyXEL 8 cc:5d:4e:xx:xx:xx 5dB 54M
Websense 6 00:22:7f:xx:xx:xx 59dB 54M

解决方案

For a sed-only solution (which I don't necessarily advocate), try:

echo 'a b "c d e" f g "h i"' |\
sed ':a;s/^\(\([^"]*"[^"]*"[^"]*\)*[^"]*"[^"]*\) /\1_/;ta'
a b "c_d_e" f g "h_i"

Translation:

  • Start at the beginning of the line.
  • Look for the pattern junk"junk", repeated zero or more times, where junk doesn't have a quote, followed by junk"junk space.
  • Replace the final space with _.
  • If successful, jump back to the beginning.

这篇关于SED:用下划线引号内更换空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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