Search This Blog

Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts

Wednesday, July 12, 2023

Remove blank/empty lines with awk

 How to remove empty or blank lines with awk.

Example 1:

command | awk '/^s*$/'


Example 2:

awk '/^s*$/' filename


Thursday, June 18, 2020

awk multiple field separators.

Using multiple field separators with awk and perl to convert "," to a newline.

Example:

echo "100(txt),528(smbw),529(smbt),530(smbn),10115(smbs)"|\
 perl -pe 's/,/\n/g'
100(txt)
528(smbw)
529(smbt)
530(smbn)
10115(smbs)

Using awk to just print the text between ().
echo "100(txt),528(smbw),529(smbt),530(smbn),10115(smbs)"|\
 perl -pe 's/,/\n/g'|\
 awk -F"[()]" '/smb/{print $2}'
smbw
smbt
smbn
smbs

Using awk to just print the text between [].

awk -F'[][]' and awk -F'[[]]' will not work.

Wednesday, August 17, 2016

Split the $PATH variable into seperate lines.

To split the $PATH variable into separate lines can be useful, specially if you have a very long $PATH. Also good for showing double entries.




echo $PATH | awk '{split($0,a,":")}; END { for (p in a) printf "%s\n",a[p];}'



Friday, May 08, 2015

Use variable in search pattern in awk

Passing a variable to awk and using it in the search pattern.



awk -v string="$STRING" -f  script.awk  [filename]

Script extract:

$0 ~ string {
      
       print $0;
}

Note:

        On Solaris use nawk.

Wednesday, August 06, 2014

awk and ifconfig

Print interface name and network config oneliner.


#ifconfig -a | awk '/^[a-z]/{intf=sprintf("%-10s", $1);getline; if($1=="inet") printf "%-6s %s\n", intf, $0;}'
          inet addr:172.19.2.26  Bcast:172.19.2.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

Tuesday, August 30, 2011

Awk: Using getline to read a file in BEGIN

Example reading in the password file into an array for later use.
BEGIN {
        FS=":";
        while ((getline  < "/etc/passwd")  >;0 )
        {
                Alias[$1]=$0;
        }
}



Wednesday, December 01, 2010

awk convert time string to time format

This will take a time string HHMMSS (121525) and format it as 12:15:25

A sample line from a.txt:

01201 095440 sd10 3393.2 1.1 13572.8 4.4 0.0 1.0 0.3 2 91

awk '{h=substr($2,1,2);\
      m=substr($2,3,2);\
      s=substr($2,5,2);\
      $2=sprintf("%s:%s:%s",h,m,s);\
      print $0;}' a.txt | head 
 

Will produce the following out put:

101201 09:54:40 sd10 3393.2 1.1 13572.8 4.4 0.0 1.0 0.3 2 91

Thursday, April 08, 2010

Using awk to remove path and print file date

$ls -lat /current/*xml | \
         nawk '{printf "%s ", $8; system("basename " $9)}'
20:24 file1.xml
21:44 file2.xml
22:34 file3.xml
$

Wednesday, May 16, 2007

awk

Using shell variables with awk.

1. awk "/$func/"'{print $0}' filename

2. awk -v pat=$func '/pat/{print $0}' filename

Using fields in search pattern

For a similar match:

1. awk '$1~/pattern/{print $0}' filename

For a precise match:

2.  awk -F":" -v user=$U '$1 == user {print $0}' filename