免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1905 | 回复: 1
打印 上一主题 下一主题

文本格式化一则 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-03 01:27 |只看该作者 |倒序浏览
现在有个例子想做如下需求,source是要格式化的文本,行数不定。

1>第一个格式化需求是,把带有select、from、where关键字的行单独列出
2>第二个格式化需求是,把带有select、from、where关键字的行头和行尾个加上一个注释,关键字行的注释和其他行不一样,
  2.1>在select行行头加111,行尾是222.
   2.2>select和from之间的普通行行头'aaa'行尾'bbb'
   2.3>在from行头注释是333,行尾444,
  2.4>并且还要在from这行前加一整行注释'there will be the from clause'
   2.5>from和where之间的普通行行头ccc行尾ddd
  2.6>where行头555,行尾666
   2.7>where后面到文件末尾,行头eee 行尾fff

这里涉及到的知识点大概是
1> 如何划分这几个部分 select == from from==where where==$
2> 怎么单独处理这些关键字行,并且能整合到一起,是使用数组还是行读呢?
3> .....
Source:

select  a.user_name,
a.table_name,
b.db_name
from abc_table a, test_table b
where a.job_name=b.job_name;

Target1:

select  
a.user_name,
a.table_name,
b.db_name
from
abc_table a,
test_table b
where
a.job_name=b.job_name;


Target2:

'111' select  '222'
'aaa' a.user_name, 'bbb'
'aaa' a.table_name, 'bbb'
'aaa' b.db_name  'bbb'
'there will be the from clause'
'333' from '444'
'ccc' abc_table a, 'ddd'
'ccc' test_table b 'ddd'
'555' where '666'
'eee' a.job_name=b.job_name; 'fff'

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2011-03-03 08:36 |只看该作者
本帖最后由 yinyuemi 于 2011-03-03 08:38 编辑
  1. 1. awk '/^(select|from|where)/||/,/{$1=$1"\n"}/./{sub(" ","");print}' RS="[\n,]"  file>file1
  2. 2.  awk ' /^select/{print "'\''111'\'' "$0" '\''222'\''";v1="'\''aaa'\''";v2="'\''bbb'\''"}
  3. /^from/{print "'\''there will be the from clause'\''\n'\''333'\'' "$0" '\''444'\''";v1="'\''ccc'\''";v2="'\''ddd'\''"}
  4. /^where/{print "'\''555'\'' "$0" '\''666'\''";v1="'\''eee'\''";v2="'\''fff'\''"}
  5. {print v1" "$0" "v2}
  6. ' file1>file2
  7. cat file2
  8. '111' select '222'
  9. 'aaa' select 'bbb'
  10. 'aaa' a.user_name 'bbb'
  11. 'aaa' a.table_name 'bbb'
  12. 'aaa' b.db_name 'bbb'
  13. 'aaa' from 'bbb'
  14. 'there will be the from clause'
  15. '333' from '444'
  16. 'ccc' abc_table a 'ddd'
  17. 'ccc' test_table b 'ddd'
  18. 'ccc' where 'ddd'
  19. '555' where '666'
  20. 'eee' a.job_name=b.job_name; 'fff'
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP