sh AWK - 标准用途
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh AWK - 标准用途相关的知识,希望对你有一定的参考价值。
# Simple awk usage - Print all lines
$ awk '{ print }' /etc/passwd
$ awk '{ print $0 }' /etc/passwd
# Print blank lines = document lines
$ awk '{ print "" }' /etc/passwd
# Print hiya = document lines
$ awk '{ print "hiya" }' /etc/passwd
# Print first field - Use declared delimiter
$ awk -F":" '{ print $1 }' /etc/passwd
# Print first and third field - Use declared delimiter
$ awk -F":" '{ print $1 $3 }' /etc/passwd
#######################################################
# Way to call an awk script from the command line
$ awk -f myscript.awk myfile.in
########################################################
# Way to call an awk script and pass parameter to it
# Inline script
awk -v var1=$column '{ x+=$var1 } END { print x }'
########################################################
# Way to call an awk script and pass parameters to it
# Normal script
awk -v var1=$column -v var2=$column2 -f myscript.awk myfilein > myfile.ot
########################################################
# Way to call an awk script that will deal with greek letters
gawk -b -v var1=$aa -f myscript.awk filein > fileot
########################################################
# BASIC STRUCTURE OF AWK SCRIPT
# BEGIN block is executed before file line iteration
# Used traditionally to initialize variables, set record separators (IFS/OFS)
BEGIN {
FS=":"
}
# MAIN BLOCK 1
# If the block has no declared pattern then it applies to every line
{
print $1
}
# MAIN BLOCK 2
# If the block has specified conditions then it applies
# only to lines that meet those conditions.
NR>2 {
print $0
}
# END BLOCK
# Executed after reading the last line of input file
# BASIC TYPES
# If block with pattern check in condition
# equivalent to :
# awk '$5 ~ /root/ { print $3 }'
{
if ( $5 ~ /root/ ) {
print $3
}
}
# If block with string eq check in condition
# equivalent to :
# awk '$5 == "string" { print $3 }'
{
if ( $5 == "string" ) {
print $3
}
}
# If block with numerical eq check in condition
# equivalent to :
# awk '$5 == 7 { print $3 }'
{
if ( $5==7 ) {
print $3
}
}
# If block with pattern check and regex in condition
# equivalent to :
# awk '$5 ~ /[A|B][AB][0-9]*/ { print $3 }'
{
if ( $5 ~ /[A|B][AB][0-9]*/ ) {
print $3
}
}
############################################
# NESTED CONDITIONALS
{
if ( $1 == "foo" ) {
if ( $2 == "foo" ) {
print "uno"
} else {
print "one"
}
} else if ($1 == "bar" ) {
print "two"
} else {
print "three"
}
}
##########################################
# NEGATION
# USING THE !
# Equivalent to:
# awk ' $0 !~ /matchme/ { print $1 $3 $4 }
#
! /matchme/ { print $1 $3 $4 }
to this:
#
# also equivalent to:
{
if ( $0 !~ /matchme/ ) {
print $1 $3 $4
}
}
############################################
# LOGICAL OPERATORS
# Using &&(AND) - ||(OR)
awk '( $1 == "foo" ) && ( $2 == "bar" ) { print }'
# eqyivalent to:
BEGIN {
}
($1 == "foo" ) && ( $2 == "bar" ) {
print $0
}
awk '/foo/ { print }'
# Basic structure :
# One liner : awk ' <condition> { <actions on each line that meets condition}'
# Script:
#
# BEGIN {
# }
#
# <condition> { <actions on each line that meets it
# }
#
# END {
# }
#
# Regular expression as a condition
/[0-9]+\.[0-9]*/ { print }
# String comparison as condition
$1 == "fred" { print $3 }
# Pattern matching as condition
$5 ~ /root/ { print $3 }
# Numerical comparison as condition
$5 == 4 { print $3 }
# Using internal variables as condition
NR>2 { print $0 }
NF<4 { print $1 $2 }
# Negation of conditions (NOT)
# Multiple conditions on tha same block will need to be bound
# by AND/OR/NOT.
# Search the 6th field for given pattern - print entire line
awk ' $6 ~ /[OoNn][CcOo][TtvV]/ { print $0 }' sedtest
# Search the 5th field for values greater > 100 and print entire line
awk '{ if ($5>100) } { print $0 }' sedtest
# Same as above, but with condition set as pattern
awk ' $5>100 { print $0 }' sedtest
awk '{if($5>100) print $0 }' sedtest
# Search in 6th field for the string "Nov" and print entire line
awk ' $6="Nov" { print } ' sedtest
awk ' $6="Nov" { print $0 } ' sedtest
awk '{ if($6="Nov") print $0 } ' sedtest
# Search in 6th field for the string "Nov" and print substring
awk '{ if($6="Oct") print substr($4,0,2) }' sedtest
awk '$6="Oct" { print substr($4,0,2) }' sedtest
awk '$6="Oct" { print substr($4,0,4) }' sedtest
# Search in 9th field for pattern and print entire line
awk ' $9 ~ /arraytest/ { print $0 }' sedtest
# Search in all lines with even line number and print them
awk ' NR%2 { print $0 } ' sedtest
# Search for lines that begin with -rwx and print them
awk ' /^ -rwx/ { print }' sedtest
awk ' /^ "-rwx"/ { print }' sedtest
awk ' /^ \-rwx/ { print }' sedtest
# Will print the lines that match continuously
awk '{ while($6="Nov") print } ' sedtest
# Print specified fields and substrings
awk ' {print $2, $3, $5, substr($8,0,2)}' sedtest
# Print all lines after the fourth - NR in condition
awk ' NR>4 {print $0}' sedtest
# Print all lines that do not contain pattern in the first field
awk ' $1 !~ /total/ { print }' sedtest
# Print 8th field lines that fifth field is greater than 2000
awk '$5>2000 { print $8 }' sedtest
awk '{ if ($5>2000) print $8 }' sedtest
# Various ways of pattern matching and negative pattern matching using !
awk ' $9 ~ /columncut[1-3]/ { print }' sedtest
awk ' $9 !~ /columncut[1-3]/ { print }' sedtest
awk '{ if($9 ~ /columncut*/) print $0}' sedtest
awk '{ if($9 ~ /columncut./) print $0}' sedtest
awk '{ if($9 ~ /columncut/) print $0}' sedtest
awk '{ if($9 ~ /columncut..bsh/) print $0}' sedtest
# Various ways of using NR to specify condition
awk '{ if(NR%4!=0) print }' sedtest
awk '{ if(NR%4>1) print }' sedtest
awk '{ if(NR>1) print }' sedtest
awk '{ if(NR>5) print }' sedtest
# Look for match in the beginning of line
awk '/^-/ { print } ' sedtest
awk '/^-rwx/ { print } ' sedtest
# Look for substring of entire line - offset 1 and print entire line
awk '{ if ( substr($0, 1, 1) == "1" ) print $0 }' all.srtmrg.pan
# This snippet details various ways the substring command can be used
# substring can be placed in BEGIN, END and main code block.
# substring can be used as a "line pattern" or into the code block.
# Using awk substring in pattern with equality condition
awk 'substr($0, 1, 5) == 11131 { print substr($0, 1, 5) }' TEST.OUT
# Using awk substring in pattern with regex condition ( We need to enclose it in / /)
awk 'substr($0, 1, 5) ~ /11131/ { print substr($0, 1, 10) }' TEST.OUT
# Using awk substring in pattern with pattern containing escape characters.
awk 'substr($0, 1, 5) ~ /16\/12\/2018/ { print $0 }' BUT000.TXT > TEST2.OUT
# Using awk substring with delimiter, substring in pattern and pattern containing escape characters
$ awk -F"," '$11 == 16\/12\/2018 { print $0 }' BUT000.TXT > TEST2.OUT
# Using awk substring in code block with eq. condition
awk 'substr($0, 1, 5) == 11131 { if(substr($0, 1, 5)==11131){ a = "panagos"; print a} else { print substr($0, 1, 10)} }' TEST.OUT
# Using awk substring in code block with regex condition
awk 'substr($0, 1, 5) == 11131 { if(substr($0, 1, 5)~/11131/){ a = "panagos"; print a} else { print substr($0, 1, 10)} }' TEST.OUT
# Using awk substring in print
awk 'substr($0, 1, 5) ~ /11\131/ { print substr($0, 1, 10) }' TEST.OUT
# Multiple uses on all levels.
awk 'substr($0, 1, 5) ~ /16\/12\/2018/ { if(substr($0, 1, 2)==3){ varmode = "new"; print $0, substr($0, 1, 20); }}
# gawk -F "," 'substr($9, 0, 1)==3 && length($9)==12 { print $9 }' MISINDYPODIEYREYNISI.TXT > out1.txt^C
gawk -F "," 'substr($10, 0, 3)=3 && length($10)==12 { gsub(/,/,".", $10)l print}'
以上是关于sh AWK - 标准用途的主要内容,如果未能解决你的问题,请参考以下文章