2013年8月20日星期二

AWK text-processing tools (Linux)

 

AWK text-processing tools (Linux)

 
  

PS: the beginning of practice, gave a million-level data processing tasks previously learned something SHELL sed / awk sort of treatment, but there is no specific application, just a few lines of a few 10 10 small data column operation, so I want to study under the new and applied in practical work. Fortunately, you can use excel to edit, save on the card you die!

 
 

What is AWK

 

awk what? Unlike most other UNIX commands is different from the name of point of view, we can not know the function of awk, because awk are three names of abbreviations, they are: Aho, (Peter) Weinberg and (Brain) Kernighan. It is these three men created awk --- an excellent pattern scanning and processing tools.

 

AWK use

 

awk language most basic function is in a file or a string based on the specified rules browse and extract information.

 
   There are three ways you can call awk:  
 
      
  1. command line
  2.   
  3. all the awk command to insert a file, and use chmod to make the file as an executive awk-f scripts.awk
  4.   
  5. all the awk command to insert a separate file, and then call #! / bin / awk-f
  6.  
 

specific use

 

1. save awk output

 

nwefile from myfile file to file, where $ 0 means all content

 
  
 awk '{print $0}'  myfile>newfile
 
 

2. use tee, the output of the document at the same time output to the screen (with the pipe '|')

 
  
 awk '{print $0}' myfile |tee newfile
 
 

3.awk general grammatical form

 
  
awk [-Field-separator] 'commands' input-file(s) 

awk -f awk-script-file input-file
 
 The general form of the script

4.awk

 
  
#awk-script-file.awk 
BEGIN {FS
=","}
{
print $
1
}
END {print
"FINISH"}
 
 

which, FS-F with the command line is the same, used to set the delimiter when dealing with SCV files need to be, "" as a delimiter

 

AWK built-in function

 

awk makes it a good programming language one of the reasons is that it absorbs some of the best programming language language many advantages. One of these advantages is the use of built-in functions, awk define and support a series of built-in functions, because these functions are used, making the awk provides a more complete and powerful features.

 

built-in string functions:

 
  
gsub(r,s)           在整个$0中用s替代r 


awk 'gsub(/name/,"xingming") {print $0}' temp


gsub(r,s,t) 在整个t中用s替代r


index(s,t) 返回s中字符串t的第一位置,失败返回0


awk 'BEGIN {print index("Sunny","ny")}' temp 返回4


length(s) 返回s的长度


match(s,r) 测试s是否包含匹配r的字符串,失败返回0 也可以使用~/xxx/


awk '$1=="J.Lulu" {print match($1,"u")}' temp 返回4


split(s,a,fs) 在fs上将s分成序列a


awk 'BEGIN {print split("12#345#6789",myarray,"#")"'


返回3,同时myarray[
1]="12", myarray[2]="345", myarray[3]="6789"


sprint(
fmt,exp) 返回经fmt格式化后的exp


sub(r,s) 从$0中最左边最长的子串中用s代替r(只更换第一遇到的匹配字符串)


substr(s,p) 返回字符串s中从p开始的后缀部分


substr(s,p,n) 返回字符串s中从p开始长度为n的后缀部分
 
 

2.awk Customize statement

 

1. supported if the judge

 
  
 if(表达式) 

{语句1}

else if(表达式)

{语句2}

else

{语句3}
 
 

2. support for loop

 
  
for(变量 in 数组) 

{语句}
 
 
  
for(变量;条件;表达式) 

{语句}
 
 

3. supports while loop

 
  
while(表达式) 

{语句}
 
 
  
do 

{语句}
while(条件)
 

没有评论:

发表评论