Awk: Difference between revisions

From Leo's Notes
This page was last edited on 24 February 2017, at 19:14.
m Text replacement - "<code>" to "{{code|"
m Text replacement - "</code>" to "}}"
Line 41: Line 41:




== Convert {{code|ps</code> Elapsed Time to Seconds ==
== Convert {{code|ps}} Elapsed Time to Seconds ==
To convert the elapsed time (in [http://pubs.opengroup.org/onlinepubs/009696799/utilities/ps.html POSIX locale] formatted as {{code|[[dd-]hh:]mm:ss</code>) to seconds using awk:
To convert the elapsed time (in [http://pubs.opengroup.org/onlinepubs/009696799/utilities/ps.html POSIX locale] formatted as {{code|[[dd-]hh:]mm:ss}}) to seconds using awk:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 55: Line 55:
</syntaxhighlight>
</syntaxhighlight>


For example, a process running for {{code|66-00:12:58</code> has been running for 5703178 seconds. The Awk command will match how many columns were found and then do the proper calculation.
For example, a process running for {{code|66-00:12:58}} has been running for 5703178 seconds. The Awk command will match how many columns were found and then do the proper calculation.


== Get a Text Section ==
== Get a Text Section ==
To grab a specific section from a .spec file (where sections begin with {{code|%</code>):
To grab a specific section from a .spec file (where sections begin with {{code|%}}):


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">

Revision as of 19:14, 24 February 2017

Counting Lines Until Match

To print the number of lines before a match is found:

awk 'BEGIN { n = 0 } /PATTERN/ { print n; next } { n++ }'

Eg: Suppose I have a file with the contents:

leo
spoon
cake
fork

To find the number of lines up until 'cake', do:

# cat list.txt | awk 'BEGIN { n = 0 } /cake/ { print n; next } { n++ }'

Convert Number as Bytes to Human Readable Value

I wanted to sort size in reverse order, but in order to do that properly, the value from du needs to be in kilobytes. I also didn't want to run this through du again just to get the human readable value. So, I did this:

du -s ./*/ ./*/*/ ./*/*/*/ \ 
   | sort -rn \ 
   | awk 'BEGIN { \ 
      split("KB MB GB TB PB", type) \ 
   } \ 
   { \ 
      y = 0; \                                                                                                  
      x = $1; \ 
      for (i = 4; y < 1 ; i--) \ 
         y = x / (2 ** (10 * i)); \ 
      print y type[i+2]" "$2 \ 
   }'

If you want to count using bytes instead of kilobytes, just add K to the split function and replace i=4 with i=5.


Convert ps Elapsed Time to Seconds

To convert the elapsed time (in POSIX locale formatted as {{code|[[dd-]hh:]mm:ss}}) to seconds using awk:

Elapsed=`ps -p $Pid -o etime=`
Elapsed=`echo $Elapsed | tr - : | tr : ' '` 
Seconds=`echo $Elapsed | awk ' 
	NF == 2 { print ($1 * 60) + $2 } 
	NF == 3 { print ((($1 * 60) + $2) * 60) + $3 } 
	NF == 4 { print ((((($1 * 24) + $2) * 60) + $3) * 60) + $4 } 
	{}'` 
echo $Seconds

For example, a process running for 66-00:12:58 has been running for 5703178 seconds. The Awk command will match how many columns were found and then do the proper calculation.

Get a Text Section

To grab a specific section from a .spec file (where sections begin with %):

if [ $# -ne 2 ] ; then
	echo "Usage: $0 file.spec section"
	exit
fi

Section="$2"

cat $1 | awk -v Section="$Section" '
BEGIN {
	InSection=false
} 
/^%.*/ { 
	if ($0 ~ Section) {
		InSection=1
	} else {
		InSection=0
	}

	next
}
{
	if (InSection) {
		print $0
	}
}