Author Topic: basic of awk by golfreeze  (Read 6100 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
basic of awk by golfreeze
« on: กันยายน 04, 2022, 10:24:46 AM »
##show second column contain word "account"
awk '$2 ~ account {print} ' filenamel

##show first column start with "e" in file check.sh
awk '$1 ~ /^e/ {print } ' check.sh

##show first column start with not contain "e" in file check.sh
awk '$1 !~ /^e/ {print } ' check.sh

##search and grep "account" from file
awk '/account/ {print } ' check.sh
awk '/^account/ {print }' check.sh

##if > < >= <= && || finding $5 more than 2000 and less than 2500 then print
awk '$5 > 2000 && $5 <2500 {print $0}' check.sh

##if > < >= <= && || finding $5 more than 2000 or $5 not contain "sal" in line then print
awk '$5 > 2000 || $5 !~ /sal/ {print $0}' check.sh

##if need change first line with specific "Start processing" and grep "account"
awk 'BEGIN {print "Start processing"} {print $0}' check.sh
awk 'BEGIN {print "Start processing"} /account/ {print $0}' check.sh

###if need add BEGIN and END of awk and grep "account"
awk 'BEGIN {print "Start processing"} /account/ {print $0} END {print "End processed"}' check.sh

##if need count NR=Number record , NF=Number of field
awk '{print NR,$0,NF} END {print "End processed"}' check.sh

##if need print LAST Field $NF
awk '{print NR,$0,$NF,FILENAME} ' check.sh

##if need only line4 until line8 in file to show
awk 'NR==4,NR==8  {print $0}' check.sh
awk 'NR==4,NR==8 {print $0}' check.sh | awk '$3 ~ /username/'

##if substitute space with some character use OFS=output field substitute => if see space " " then replace with "@@"
awk 'NR==4,NR==8 {print $0}' check.sh | awk 'BEGIN{OFS="@@"} {print $2,$3}'
awk 'BEGIN {print "Start processing"} {print $0}' text3 | awk 'BEGIN{OFS="@"} {print $1,$2".com"}'

##if substitute space with some character use FS=field substitute => if see ":" then replace with space " "
awk 'BEGIN{FS=":"} {print $2,$3}' text4

##if substitute space with some character use FS=field substitute => if see ":" then replace with "@@"
awk 'BEGIN{FS=":";OFS="@@"} {print $2,$3}' text4

##if substitute last character in file use ORS=":" with :
awk 'BEGIN {print "Start processing"} {print $0}' text3 | awk 'BEGIN{OFS="@";ORS=":"} {print $1,$2".com"}'
output=> Start@processing.com:store@manager.com:Woodridge,Australia@Jon.com:Lethbridge,Canada@Mike.com:Bridge,
Canada@Mike.com:Sisaket,Thailand@Liamthong.com:Pathumthani,Thailand@White.com:Saimai,Bangkok@Packetlove.com:

###if need awk run if condition or run condition on script file
awk '{if ($3 > 2000) print $0 }' text3
output=>
store                manager       total_sales
Woodridge,Australia  Jon Stephens     33726.77
Lethbridge,Canada    Mike Hillyer     33679.79
Bridge,Canada   Mike Hillyer          43679.89
Saimai,Bangkok          Packetlove      8229.4

awk '{if ($3 > 2000) print $0; else print "Low" }' text3
output=>
store                manager       total_sales
Woodridge,Australia  Jon Stephens     33726.77
Lethbridge,Canada    Mike Hillyer     33679.79
Bridge,Canada   Mike Hillyer          43679.89
Low
Low
Saimai,Bangkok          Packetlove      8229.4

###if need awk run if condition or run condition on script file
awk -f script text4

###toupper=change small character to BIG capital leeter  on first column , length=count length of character on $3
awk '{print toupper($1),length($3)} {print $0}' text3
« Last Edit: กันยายน 04, 2022, 10:27:56 AM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic of awk by golfreeze
« Reply #1 on: กันยายน 04, 2022, 10:25:30 AM »
=== text3 ====
store                manager       total_sales
Woodridge,Australia  Jon Stephens     33726.77
Lethbridge,Canada    Mike Hillyer     33679.79
Bridge,Canada   Mike Hillyer          43679.89
Sisaket,Thailand        thong       562.22
Pathumthani,Thailand    White           992.42
Saimai,Bangkok          Packetlove      8229.4

=== text4 ===
store:manager:total_sales
Woodridge,Australia:Stephens:33726.77
Lethbridge,Canada:Hillyer:33679.79
Bridge,Canada:Mike:43679.89
Sisaket,Thailand:thong:562.22
Pathumthani,Thailand:White:992.42
Saimai,Bangkok:Packetlove:8229.4
« Last Edit: กันยายน 04, 2022, 10:27:34 AM by golfreeze »