免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3273 | 回复: 17

[文本处理] 如何选择性地对文本中的字母替换成指定字符串? [复制链接]

论坛徽章:
1
白羊座
日期:2014-11-13 10:19:16
发表于 2014-09-15 11:50 |显示全部楼层
本帖最后由 iocg 于 2014-09-15 11:51 编辑

一个文本,我想替换abcde这五个字母,对一个英文短文加密:
假设  
a  -->  %as:nd
b  -->  as~bz3
c  -->  a,s%cd
d  -->  m9~7cd
e  -->  xbs%ka

例如下面的文本
You'll plenty of mean~s of reac,hing w%-hat tey trb%cks one in your bow! On top of that, Just !about any, Any time you bo!nk, Leave her with a per%son's controls
但是有个条件,在单词(以两边空格为界)中含有%或者!符号时这个单词的字母都不转换



论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2014-09-15 12:57 |显示全部楼层
本帖最后由 jason680 于 2014-09-15 19:04 编辑

回复 1# iocg


Would you like this ...
   
$ perl -lane 'BEGIN{%h=qw/a %as:nd b as~bz3 c a,%scd d m9~7cd e xbs%ka/}{for(@F){(!m/[%!]/)&&s/([a-e])/$h{$1}/g}print join " ",@F}' FILE
You'll plxbs%kanty of mxbs%ka%as:ndn~s of rxbs%ka%as:nda,%scd,hing w%-hat txbs%kay trb%cks onxbs%ka in your bow! On top of th%as:ndt, Just !about %as:ndny, Any timxbs%ka you bo!nk, Lxbs%ka%as:ndvxbs%ka hxbs%kar with %as:nd per%son's a,%scdontrols

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
发表于 2014-09-15 14:51 |显示全部楼层
本帖最后由 bulletmarquis 于 2014-09-15 15:00 编辑

回复 1# iocg

  1.     cat abc
  2. You'll plenty of mean~s of reac,hing w%-hat tey trb%cks one in your bow! On top of that, Just !about any, Any time you bo!nk, Leave her with a per%son's controls


  3. cat abc|awk '
  4.   BEGIN{Arr["a"]="%as:nd";Arr["b"]="as~bz3";Arr["c"]="a,s%cd";Arr["d"]="m9~7cd";Arr["e"]="xbs%ka";RS=" ";ORS=" "}
  5.   {
  6.     if ($0!~/!/&&$0!~/%/)
  7.     {
  8.       split($0,arrA,"");
  9.       l=length(arrA);
  10.       $0="";
  11.       for (i=1;i<=l;i++)
  12.       {
  13.         if (arrA[i] in Arr)
  14.         {
  15.           $0=$0Arr[arrA[i]]
  16.         }
  17.         else
  18.         {
  19.           $0=$0arrA[i]
  20.         }
  21.       }
  22.     }
  23.   };
  24.   1
  25.   '
  26. You'll plxbs%kanty of mxbs%ka%as:ndn~s of rxbs%ka%as:nda,s%cd,hing w%-hat txbs%kay trb%cks onxbs%ka in your bow! On top of th%as:ndt, Just !about %as:ndny, Any timxbs%ka you bo!nk, Lxbs%ka%as:ndvxbs%ka hxbs%kar with %as:nd per%son's a,s%cdontrols
复制代码

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
发表于 2014-09-15 15:02 |显示全部楼层
回复 2# jason680


    为啥perl写出来比我用awk写的短这么多。。是我想的太复杂了么?

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
发表于 2014-09-15 15:16 |显示全部楼层
本帖最后由 bulletmarquis 于 2014-09-15 15:24 编辑

@yestreenstars@waker@jason680回复 1# iocg


    另一种写法却有问题,求大神指点原因:
  1. echo -n $(cat abc)|awk '
  2.   BEGIN{a="%as:nd";b="as~bz3";c="a,s%cd";d="m9~7cd";e="xbs%ka";RS=" ";ORS=" "}
  3.   function rep(a)
  4.   {
  5.     _cmd_="echo -n "a"|sed \"s/a/"a"/g;s/b/"b"/g;s/c/"c"/g;s/d/"d"/g;s/e/"e"/g\"";
  6.     _cmd_|getline _rst_;
  7.     close(_cmd_);
  8.     return _rst_
  9.   };
  10.   {if ($0!~/!/&&$0!~/%/){$0=rep($0)}}
  11.   NF+=0;1
  12.   '
复制代码
用这种写法,有两个问题:
1、解析的时候,第一个单词会报错
  1. sh: -c: line 0: unexpected EOF while looking for matching `"'
  2. sh: -c: line 1: syntax error: unexpected end of file
复制代码
如果不用echo -n的话,最后一个单词也会报同样错误。。。

2、用sed解析的时候,因为把c替换为a,s%cd时,替换出的结果也有字符d,会被下一轮的d的替换再转一次,这种情况能否避免?

求大神指点迷津,拜谢!

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2014-09-15 15:18 |显示全部楼层
回复 4# bulletmarquis

就这个问题而言
Perl比较容易实现你的想法(你要的功能)
   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2014-09-15 15:32 |显示全部楼层
本帖最后由 jason680 于 2014-09-15 15:33 编辑

回复 5# bulletmarquis

1. variable 'a' ?

....
  BEGIN{a="%as:nd";b="as~bz3";c="a,s%cd";d="m9~7cd";e="xbs%ka";RS=" "}
  function rep(a)
  {
    _cmd_="echo -n "a"|sed \"s/a/"a"/g;s/b/"b"/g;s/c/"c"/g;s/d/"d"/g;s/e/"e"/g\"";
...   

2. Do you know what happen when variable 'a' is "You'll"

    _cmd_="echo -n "a"|sed \"s/a/"a"/g;s/b/"b"/g;s/c/"c"/g;s/d/"d"/g;s/e/"e"/g\"";

   echo -n You'll | sed .....

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
发表于 2014-09-15 15:39 |显示全部楼层
本帖最后由 bulletmarquis 于 2014-09-15 15:39 编辑

回复 7# jason680


    这里忘了改了,实际测试的时候,把数组名由a改成了arr

    另外有单引号会有问题么?直接拿sed测试是可以的啊
  1. echo $a
  2. abc'def

  3. echo $a|sed "s/c/k/"
  4. abk'def

  5. echo $a|sed "s/c'd/'kkk'/"
  6. ab'kkk'ef
复制代码

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2014-09-15 15:49 |显示全部楼层
本帖最后由 jason680 于 2014-09-15 15:52 编辑

回复 8# bulletmarquis


you have to debug by yourself with your awk code
Note: Don't use shell code and say "here is no issue"....   

$ awk '
  BEGIN{a="%as:nd";b="as~bz3";c="a,s%cd";d="m9~7cd";e="xbs%ka";RS=" ";ORS=" "}
  function rep(arr)
  {
    _cmd_="echo -n "arr"|sed \"s/a/"a"/g;s/b/"b"/g;s/c/"c"/g;s/d/"d"/g;s/e/"e"/g\"";
    #_cmd_|getline _rst_;
    print _cmd_; exit
   
  };
  {if ($0!~/!/&&$0!~/%/){$0=rep($0)}}
  
  ' FILE
echo -n You'll|sed "s/a/%as:nd/g;s/b/as~bz3/g;s/c/a,s%cd/g;s/d/m9~7cd/g;s/e/xbs%ka/g"

BTW, it's not a good idea to call system call to change string
because awk has the sub, gsub, and gensub function to do that
http://www.gnu.org/software/gawk/manual/gawk.html#String-Functions

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
发表于 2014-09-15 15:54 |显示全部楼层
回复 9# jason680


    确实。。。大神说的对,应该在awk里面测试一下print的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP