awk使用摘记

1.用awk来解析一个文件

[WEB-INF/classes/hessian.properties]
#hessian server ip:port
hessian.server.ip.port=http://192.168.5.169:8080,http://localhost:8080

#hessian server appname
hessian.server.appname=videotracker-rpc-server

#hessian server interf name
hessian.server.interf.name=hessian/redisPrimaryKey

[WEB-INF/classes/exception.properties]
exception.dbpc.host=192.168.1.41
exception.dbpc.port=5800
exception.dbpc.projectName=VTWeb
exception.dbpc.module.name=VTWeb get unique primary key
exception.dbpc.heartbeatInterval=30000

exception.dbpc.heartbeatPrimarySleepInterval=60000

exception.dbpc.heartbeatSeniorSleepInterval=120000

[WEB-INF/classes/VTWeb.properties]
special_company_server_domain=116->http://192.168.1.72:8080/VTWeb

希望得到的结果是 WEB-INF/classes/hessian.properties,WEB-INF/classes/exception.properties,WEB-INF/classes/VTWeb.properties

changes_file=changes.properties
count=$(awk "/[.*]/" $changes_file | wc -l)
echo $count

#ab=$(awk '/[.*]/ {print $0}' ./changes.properties)
#echo $ab


for i in $(seq $count) ; do
        path=$(awk '/[.*]/{print "12"} {print $0}' $changes_file)

        echo $path
done

出现了结果

12 [WEB-INF/classes/hessian.properties] #hessian server ip:port hessian.server.ip.port=http://192.168.5.169:8080,http://localhost:8080 #hessian server appname hessian.server.appname=videotracker-rpc-server #hessian server interf name hessian.server.interf.name=hessian/redisPrimaryKey 12 [WEB-INF/classes/exception.properties] exception.dbpc.host=192.168.1.41 exception.dbpc.port=5800 exception.dbpc.projectName=VTWeb exception.dbpc.module.name=VTWeb get unique primary key exception.dbpc.heartbeatInterval=30000 exception.dbpc.heartbeatPrimarySleepInterval=60000 exception.dbpc.heartbeatSeniorSleepInterval=120000 12 [WEB-INF/classes/VTWeb.properties] special_company_server_domain=116->http://192.168.1.72:8080/VTWeb
12 [WEB-INF/classes/hessian.properties] #hessian server ip:port hessian.server.ip.port=http://192.168.5.169:8080,http://localhost:8080 #hessian server appname hessian.server.appname=videotracker-rpc-server #hessian server interf name hessian.server.interf.name=hessian/redisPrimaryKey 12 [WEB-INF/classes/exception.properties] exception.dbpc.host=192.168.1.41 exception.dbpc.port=5800 exception.dbpc.projectName=VTWeb exception.dbpc.module.name=VTWeb get unique primary key exception.dbpc.heartbeatInterval=30000 exception.dbpc.heartbeatPrimarySleepInterval=60000 exception.dbpc.heartbeatSeniorSleepInterval=120000 12 [WEB-INF/classes/VTWeb.properties] special_company_server_domain=116->http://192.168.1.72:8080/VTWeb
12 [WEB-INF/classes/hessian.properties] #hessian server ip:port hessian.server.ip.port=http://192.168.5.169:8080,http://localhost:8080 #hessian server appname hessian.server.appname=videotracker-rpc-server #hessian server interf name hessian.server.interf.name=hessian/redisPrimaryKey 12 [WEB-INF/classes/exception.properties] exception.dbpc.host=192.168.1.41 exception.dbpc.port=5800 exception.dbpc.projectName=VTWeb exception.dbpc.module.name=VTWeb get unique primary key exception.dbpc.heartbeatInterval=30000 exception.dbpc.heartbeatPrimarySleepInterval=60000 exception.dbpc.heartbeatSeniorSleepInterval=120000 12 [WEB-INF/classes/VTWeb.properties] special_company_server_domain=116->http://192.168.1.72:8080/VTWeb

但当我把 {print "12"} {print $0}的位置调换一下,

#!/bin/bash
#set -x

changes_file=changes.properties
count=$(awk "/[.*]/" $changes_file | wc -l)
echo $count

#ab=$(awk '/[.*]/ {print $0}' ./changes.properties)
#echo $ab


for i in $(seq $count) ; do
        path=$(awk '/[.*]/{print $0}{print "12"}' $changes_file)

        echo $path
done

就出现了不同的结果

12 [WEB-INF/classes/hessian.properties] 12 12 12 12 12 12 12 12 12 12 [WEB-INF/classes/exception.properties] 12 12 12 12 12 12 12 12 12 12 12 [WEB-INF/classes/VTWeb.properties] 12 12
12 [WEB-INF/classes/hessian.properties] 12 12 12 12 12 12 12 12 12 12 [WEB-INF/classes/exception.properties] 12 12 12 12 12 12 12 12 12 12 12 [WEB-INF/classes/VTWeb.properties] 12 12
12 [WEB-INF/classes/hessian.properties] 12 12 12 12 12 12 12 12 12 12 [WEB-INF/classes/exception.properties] 12 12 12 12 12 12 12 12 12 12 12 [WEB-INF/classes/VTWeb.properties] 12 12

一开始百思不得其果,不过后来查了一下,

 AWK is a line-oriented language.  The pattern comes first, and then the action.  Action statements are enclosed in { and }.  Either the pattern may be  miss‐
       ing,  or the action may be missing, but, of course, not both.  If the pattern is missing, the action is executed for every single record of input.  A missing
       action is equivalent to

              { print }

       which prints the entire record.

       Comments begin with the # character, and continue until the end of the line.  Blank lines may be used to separate statements.   Normally,  a  statement  ends
       with  a  newline, however, this is not the case for lines ending in a comma, {, ?, :, &&, or ||.  Lines ending in do or else also have their statements auto‐
       matically continued on the following line.  In other cases, a line can be continued by ending it with a “”, in which case the newline is ignored.

       Multiple statements may be put on one line by separating them with a “;”.  This applies to both the statements within the action  part  of  a  pattern-action
       pair (the usual case), and to the pattern-action statements themselves.

pattern /[.*]/ 和后面的action是一一对应的,第一种情况 awk '/[.*]/{print "12"} {print $0}‘,前面两个是对应的pattern和action, 而后面只有一个action,而pattern是默认也就是应用到每一行,所以12只打印了3次,文件的每一行都被打印;后一种情况 '/[.*]/{print $0}{print "12"}',所以满足pattern的只找到3次,而12被应用到了每一行。

2.pattern里如果要用变量,可以这样

line=$(awk '/^'$key'/ {print}' /tmp/$$)

这里注意单引号,双引号不要用错:

在 bash 中,常用的 quoting 有如下三種方法:
* hard quote:' ' (單引號),凡在 hard quote 中的所有 meta 均被關閉。
* soft quote: " " (雙引號),在 soft quoe 中大部份 meta 都會被關閉,但某些則保留(如 $ )。(註二)
* escape : (反斜線),只有緊接在 escape (跳脫字符)之後的單一 meta 才被關閉。

* literal:也就是普通純文字,對 shell 來說沒特殊功能。
* meta:對 shell 來說,具有特定功能的特殊保留字元。

3.awk用gsub函数去除行里首尾的空格和tab键

awk "/[.*]/{F++;next}{if(F==$i){gsub(/^( |	)*|( |	)*$/,"");print}}/[.*]/"  $changes_file | egrep -v '^#' > /tmp/$$
喜欢艺术的码农
原文地址:https://www.cnblogs.com/zjhgx/p/7795034.html