Curiosidades De Hackers
EXPRESIONES REGULARESLINUX

EXPRESIONES REGULARES(AWK)


Getting Started


Have a try

$ awk -F: '{print $1, $NF}' /etc/passwd

`-F:` Colon as a separator
`{…}` Awk program
`print` Prints the current record
`$1` First field
`$NF` Last field
`/etc/passwd` Input data file

Awk program

BEGIN          {<initializations>} 
   <pattern 1> {<program actions>} 
   <pattern 2> {<program actions>} 
   ...
END            {<final actions>}

Example

awk '
    BEGIN { print "\n>>>Start" }
    !/(login|shutdown)/ { print NR, $0 }
    END { print "(<<<END\n" }
' /etc/passwd

Variables

          $1      $2/$(NF-1)    $3/$NF
           ▼          ▼           ▼ 
        ┌──────┬──────────────────┬───────┐
$0/NR ▶ │  ID  │  WEBSITE         │  URI  │
        ├──────┼──────────────────┼───────┤
$0/NR ▶ │  1   │  cheatsheets.zip │  awk  │
        ├──────┼──────────────────┼───────┤
$0/NR ▶ │  2   │  google.com      │  25   │
        └──────┴──────────────────┴───────┘

Awk program examples

awk 'BEGIN{print "hello world"}'        # Prints "hello world"
awk -F: '{print $1}' /etc/passwd         # -F: Specify field separator

# /pattern/ Execute actions only for matched pattern
awk -F: '/root/ {print $1}' /etc/passwd                     

# BEGIN block is executed once at the start
awk -F: 'BEGIN { print "uid"} { print $1 }' /etc/passwd     

# END block is executed once at the end
awk -F: '{print $1} END { print "-done-"}' /etc/passwd

Conditions

awk -F: '$3>30 {print $1}' /etc/passwd

Generate 1000 spaces

awk 'BEGIN{
    while (a++ < 1000)
        s=s " ";
    print s
}'

Arrays

awk 'BEGIN {
   fruits["mango"] = "yellow";
   fruits["orange"] = "orange"
   for(fruit in fruits) {
     print "The color of " fruit " is " fruits[fruit]
   }
}'

Functions

# => 5
awk 'BEGIN{print length("hello")}'
# => HELLO
awk 'BEGIN{print toupper("hello")}'
# => hel
awk 'BEGIN{print substr("hello", 1, 3)}'

Awk Variables

# Build-in variables
| -              | -                                                   |
|----------------|-----------------------------------------------------|
| `$0`           | Whole line                                          |
| `$1, $2...$NF` | First, second… last field                           |
| `NR`           | `N`umber of `R`ecords                               |
| `NF`           | `N`umber of `F`ields                                |
| `OFS`          | `O`utput `F`ield `S`eparator  <br> _(default " ")_  |
| `FS`           | input `F`ield `S`eparator <br> _(default " ")_      |
| `ORS`          | `O`utput `R`ecord `S`eparator <br> _(default "\n")_ |
| `RS`           | input `R`ecord `S`eparator <br> _(default "\n")_    |
| `FILENAME`     | Name of the file                                    |

# Expressions
| -                   | -                                  |
|---------------------|------------------------------------|
| `$1 == "root"`      | First field equals root            |
| `{print $(NF-1)}`   | Second last field                  |
| `NR!=1{print $0}`   | From 2nd record                    |
| `NR > 3`            | From 4th record                    |
| `NR == 1`           | First record                       |
| `END{print NR}`     | Total records                      |
| `BEGIN{print OFMT}` | Output format                      |
| `{print NR, $0}`    | Line number                        |
| `{print NR "	" $0}` | Line number (tab)                  |
| `{$1 = NR; print}`  | Replace 1st field with line number |
| `$NF > 4`           | Last field > 4                     |
| `NR % 2 == 0`       | Even records                       |
| `NR==10, NR==20`    | Records 10 to 20                   |
| `BEGIN{print ARGC}` | Total arguments                    |
| `ORS=NR%5?",":"\n"` | Concatenate records                |

# Examples
Print sum and average
awk -F: '{sum += $3}
END { print sum, sum/NR }
' /etc/passwd

Copy code
Printing parameters
awk 'BEGIN {
for (i = 1; i < ARGC; i++)
print ARGV[i] }' a b c

csharp
Copy code
Output field separator as a comma
awk 'BEGIN { FS=":";OFS=","}
{print $1,$2,$3,$4}' /etc/passwd

css
Copy code
Position of match
awk 'BEGIN {
if (match("One Two Three", "Tw"))
print RSTART }'

lua
Copy code
Length of match
awk 'BEGIN {
if (match("One Two Three", "re"))
print RLENGTH }'

bash
Copy code