天天看点

linux shell 字符串处理

原文件

# cat tablelist.txt 

Schema |        Name         |   Type   |   Owner    

--------+---------------------+----------+------------

 public | dept           | table    | postgres

 public | job         | table    | postgres

 public | client        | table    | postgres

列表内容

#cat tablelist.txt|grep 'public'

打印列去除前后一个或若干个空格或tab制表符

 #cat tablelist.txt|grep 'public'|awk -F '|' '{print $2}'|awk '{gsub(/^[ \t]+|[ \t]+$/,"")};1'

 dept           

 job         

 client        

添加前后字符

# cat tablelist.txt|grep 'public'|awk -F '|' '{print $2}'|awk '{gsub(/^[ \t]+|[ \t]+$/,"")};1'|sed 's/^/grant all privileges on /g'|sed 's/$/ to dbauser;/g'

grant all privileges on dept to dbauser;

grant all privileges on job to dbauser;

grant all privileges on client to dbauser;

cat tablelist.txt|awk -F '|' '{print $2}'|grep 'ape*'|awk '{gsub(/^[ \t]+|[ \t]+$/,"")};1'|sed 's/^/grant all privileges on /g'|sed 's/$/ to jobinsight;/g'

显示所有文件》打印第二列》 打印带有ape的行     》去除字符前后一个或者若干个空格或者制表符》添加列表的前导》添加列表的后缀字符

本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/846504

继续阅读