<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.define-technology.com/mediawiki-1.35.0/index.php?action=history&amp;feed=atom&amp;title=Linux%3A_awk_Tips_and_Tricks</id>
	<title>Linux: awk Tips and Tricks - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.define-technology.com/mediawiki-1.35.0/index.php?action=history&amp;feed=atom&amp;title=Linux%3A_awk_Tips_and_Tricks"/>
	<link rel="alternate" type="text/html" href="http://wiki.define-technology.com/mediawiki-1.35.0/index.php?title=Linux:_awk_Tips_and_Tricks&amp;action=history"/>
	<updated>2026-05-04T20:11:15Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>http://wiki.define-technology.com/mediawiki-1.35.0/index.php?title=Linux:_awk_Tips_and_Tricks&amp;diff=1936&amp;oldid=prev</id>
		<title>David: Created page with &quot;NUMBERING AND CALCULATIONS:    # precede each line by its line number FOR THAT FILE (left alignment).   # Using a tab (\t) instead of space will preserve margins.   awk &#039;{prin...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.define-technology.com/mediawiki-1.35.0/index.php?title=Linux:_awk_Tips_and_Tricks&amp;diff=1936&amp;oldid=prev"/>
		<updated>2013-02-11T17:15:22Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;NUMBERING AND CALCULATIONS:    # precede each line by its line number FOR THAT FILE (left alignment).   # Using a tab (\t) instead of space will preserve margins.   awk &amp;#039;{prin...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;NUMBERING AND CALCULATIONS:&lt;br /&gt;
&lt;br /&gt;
  # precede each line by its line number FOR THAT FILE (left alignment).&lt;br /&gt;
  # Using a tab (\t) instead of space will preserve margins.&lt;br /&gt;
  awk &amp;#039;{print FNR &amp;quot;\t&amp;quot; $0}&amp;#039; files*&lt;br /&gt;
&lt;br /&gt;
  # precede each line by its line number FOR ALL FILES TOGETHER, with tab.&lt;br /&gt;
  awk &amp;#039;{print NR &amp;quot;\t&amp;quot; $0}&amp;#039; files*&lt;br /&gt;
&lt;br /&gt;
  # number each line of a file (number on left, right-aligned)&lt;br /&gt;
  # Double the percent signs if typing from the DOS command prompt.&lt;br /&gt;
  awk &amp;#039;{printf(&amp;quot;%5d : %s\n&amp;quot;, NR,$0)}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
  # number each line of file, but only print numbers if line is not blank&lt;br /&gt;
  # Remember caveats about Unix treatment of \r (mentioned above)&lt;br /&gt;
  awk &amp;#039;NF{$0=++a &amp;quot; :&amp;quot; $0};{print}&amp;#039;&lt;br /&gt;
  awk &amp;#039;{print (NF? ++a &amp;quot; :&amp;quot; :&amp;quot;&amp;quot;) $0}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
  # count lines (emulates &amp;quot;wc -l&amp;quot;)&lt;br /&gt;
  awk &amp;#039;END{print NR}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
  # print the sums of the fields of every line&lt;br /&gt;
  awk &amp;#039;{s=0; for (i=1; i&amp;lt;=NF; i++) s=s+$i; print s}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
  # add all fields in all lines and print the sum&lt;br /&gt;
  awk &amp;#039;{for (i=1; i&amp;lt;=NF; i++) s=s+$i}; END{print s}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
  # print every line after replacing each field with its absolute value&lt;br /&gt;
  awk &amp;#039;{for (i=1; i&amp;lt;=NF; i++) if ($i &amp;lt; 0) $i = -$i; print }&amp;#039;&lt;br /&gt;
 awk &amp;#039;{for (i=1; i&amp;lt;=NF; i++) $i = ($i &amp;lt; 0) ? -$i : $i; print }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the total number of fields (&amp;quot;words&amp;quot;) in all lines&lt;br /&gt;
 awk &amp;#039;{ total = total + NF }; END {print total}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
 # print the total number of lines that contain &amp;quot;Beth&amp;quot;&lt;br /&gt;
 awk &amp;#039;/Beth/{n++}; END {print n+0}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
 # print the largest first field and the line that contains it&lt;br /&gt;
 # Intended for finding the longest string in field #1&lt;br /&gt;
 awk &amp;#039;$1 &amp;gt; max {max=$1; maxline=$0}; END{ print max, maxline}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the number of fields in each line, followed by the line&lt;br /&gt;
 awk &amp;#039;{ print NF &amp;quot;:&amp;quot; $0 } &amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the last field of each line&lt;br /&gt;
 awk &amp;#039;{ print $NF }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the last field of the last line&lt;br /&gt;
 awk &amp;#039;{ field = $NF }; END{ print field }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print every line with more than 4 fields&lt;br /&gt;
 awk &amp;#039;NF &amp;gt; 4&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print every line where the value of the last field is &amp;gt; 4&lt;br /&gt;
 awk &amp;#039;$NF &amp;gt; 4&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
TEXT CONVERSION AND SUBSTITUTION:&lt;br /&gt;
&lt;br /&gt;
 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format&lt;br /&gt;
 awk &amp;#039;{sub(/\r$/,&amp;quot;&amp;quot;);print}&amp;#039;   # assumes EACH line ends with Ctrl-M&lt;br /&gt;
&lt;br /&gt;
 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format&lt;br /&gt;
 awk &amp;#039;{sub(/$/,&amp;quot;\r&amp;quot;);print}&lt;br /&gt;
&lt;br /&gt;
 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format&lt;br /&gt;
 awk 1&lt;br /&gt;
&lt;br /&gt;
 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format&lt;br /&gt;
 # Cannot be done with DOS versions of awk, other than gawk:&lt;br /&gt;
 gawk -v BINMODE=&amp;quot;w&amp;quot; &amp;#039;1&amp;#039; infile &amp;gt;outfile&lt;br /&gt;
&lt;br /&gt;
 # Use &amp;quot;tr&amp;quot; instead.&lt;br /&gt;
 tr -d \r &amp;lt;infile &amp;gt;outfile            # GNU tr version 1.22 or higher&lt;br /&gt;
&lt;br /&gt;
 # delete leading whitespace (spaces, tabs) from front of each line&lt;br /&gt;
 # aligns all text flush left&lt;br /&gt;
 awk &amp;#039;{sub(/^[ \t]+/, &amp;quot;&amp;quot;); print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete trailing whitespace (spaces, tabs) from end of each line&lt;br /&gt;
 awk &amp;#039;{sub(/[ \t]+$/, &amp;quot;&amp;quot;);print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete BOTH leading and trailing whitespace from each line&lt;br /&gt;
 awk &amp;#039;{gsub(/^[ \t]+|[ \t]+$/,&amp;quot;&amp;quot;);print}&amp;#039;&lt;br /&gt;
 awk &amp;#039;{$1=$1;print}&amp;#039;           # also removes extra space between fields&lt;br /&gt;
&lt;br /&gt;
 # insert 5 blank spaces at beginning of each line (make page offset)&lt;br /&gt;
 awk &amp;#039;{sub(/^/, &amp;quot;     &amp;quot;);print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # align all text flush right on a 79-column width&lt;br /&gt;
 awk &amp;#039;{printf &amp;quot;%79s\n&amp;quot;, $0}&amp;#039; file*&lt;br /&gt;
&lt;br /&gt;
 # center all text on a 79-character width&lt;br /&gt;
 awk &amp;#039;{l=length();s=int((79-l)/2); printf &amp;quot;%&amp;quot;(s+l)&amp;quot;s\n&amp;quot;,$0}&amp;#039; file*&lt;br /&gt;
&lt;br /&gt;
 # substitute (find and replace) &amp;quot;foo&amp;quot; with &amp;quot;bar&amp;quot; on each line&lt;br /&gt;
 awk &amp;#039;{sub(/foo/,&amp;quot;bar&amp;quot;);print}&amp;#039;           # replaces only 1st instance&lt;br /&gt;
 gawk &amp;#039;{$0=gensub(/foo/,&amp;quot;bar&amp;quot;,4);print}&amp;#039;  # replaces only 4th instance&lt;br /&gt;
 awk &amp;#039;{gsub(/foo/,&amp;quot;bar&amp;quot;);print}&amp;#039;          # replaces ALL instances in a line&lt;br /&gt;
&lt;br /&gt;
 # substitute &amp;quot;foo&amp;quot; with &amp;quot;bar&amp;quot; ONLY for lines which contain &amp;quot;baz&amp;quot;&lt;br /&gt;
 awk &amp;#039;/baz/{gsub(/foo/, &amp;quot;bar&amp;quot;)};{print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # substitute &amp;quot;foo&amp;quot; with &amp;quot;bar&amp;quot; EXCEPT for lines which contain &amp;quot;baz&amp;quot;&lt;br /&gt;
 awk &amp;#039;!/baz/{gsub(/foo/, &amp;quot;bar&amp;quot;)};{print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # change &amp;quot;scarlet&amp;quot; or &amp;quot;ruby&amp;quot; or &amp;quot;puce&amp;quot; to &amp;quot;red&amp;quot;&lt;br /&gt;
 awk &amp;#039;{gsub(/scarlet|ruby|puce/, &amp;quot;red&amp;quot;); print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # reverse order of lines (emulates &amp;quot;tac&amp;quot;)&lt;br /&gt;
 awk &amp;#039;{a[i++]=$0} END {for (j=i-1; j&amp;gt;=0;) print a[j--] }&amp;#039; file*&lt;br /&gt;
&lt;br /&gt;
 # if a line ends with a backslash, append the next line to it&lt;br /&gt;
 # (fails if there are multiple lines ending with backslash...)&lt;br /&gt;
 awk &amp;#039;/\\$/ {sub(/\\$/,&amp;quot;&amp;quot;); getline t; print $0 t; next}; 1&amp;#039; file*&lt;br /&gt;
&lt;br /&gt;
 # print and sort the login names of all users&lt;br /&gt;
 awk -F &amp;quot;:&amp;quot; &amp;#039;{ print $1 | &amp;quot;sort&amp;quot; }&amp;#039; /etc/passwd&lt;br /&gt;
&lt;br /&gt;
 # print the first 2 fields, in opposite order, of every line&lt;br /&gt;
 awk &amp;#039;{print $2, $1}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
 # switch the first 2 fields of every line&lt;br /&gt;
 awk &amp;#039;{temp = $1; $1 = $2; $2 = temp}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
 # print every line, deleting the second field of that line&lt;br /&gt;
 awk &amp;#039;{ $2 = &amp;quot;&amp;quot;; print }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print in reverse order the fields of every line&lt;br /&gt;
 awk &amp;#039;{for (i=NF; i&amp;gt;0; i--) printf(&amp;quot;%s &amp;quot;,i);printf (&amp;quot;\n&amp;quot;)}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
 # remove duplicate, consecutive lines (emulates &amp;quot;uniq&amp;quot;)&lt;br /&gt;
 awk &amp;#039;a !~ $0; {a=$0}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # remove duplicate, nonconsecutive lines&lt;br /&gt;
 awk &amp;#039;! a[$0]++&amp;#039;                     # most concise script&lt;br /&gt;
 awk &amp;#039;!($0 in a) {a[$0];print}&amp;#039;      # most efficient script&lt;br /&gt;
&lt;br /&gt;
 # concatenate every 5 lines of input, using a comma separator&lt;br /&gt;
 # between fields&lt;br /&gt;
 awk &amp;#039;ORS=%NR%5?&amp;quot;,&amp;quot;:&amp;quot;\n&amp;quot;&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTIVE PRINTING OF CERTAIN LINES:&lt;br /&gt;
&lt;br /&gt;
 # print first 10 lines of file (emulates behavior of &amp;quot;head&amp;quot;)&lt;br /&gt;
 awk &amp;#039;NR &amp;lt; 11&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print first line of file (emulates &amp;quot;head -1&amp;quot;)&lt;br /&gt;
 awk &amp;#039;NR&amp;gt;1{exit};1&amp;#039;&lt;br /&gt;
&lt;br /&gt;
  # print the last 2 lines of a file (emulates &amp;quot;tail -2&amp;quot;)&lt;br /&gt;
 awk &amp;#039;{y=x &amp;quot;\n&amp;quot; $0; x=$0};END{print y}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the last line of a file (emulates &amp;quot;tail -1&amp;quot;)&lt;br /&gt;
 awk &amp;#039;END{print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print only lines which match regular expression (emulates &amp;quot;grep&amp;quot;)&lt;br /&gt;
 awk &amp;#039;/regex/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print only lines which do NOT match regex (emulates &amp;quot;grep -v&amp;quot;)&lt;br /&gt;
 awk &amp;#039;!/regex/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the line immediately before a regex, but not the line&lt;br /&gt;
 # containing the regex&lt;br /&gt;
 awk &amp;#039;/regex/{print x};{x=$0}&amp;#039;&lt;br /&gt;
 awk &amp;#039;/regex/{print (x==&amp;quot;&amp;quot; ? &amp;quot;match on line 1&amp;quot; : x)};{x=$0}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the line immediately after a regex, but not the line&lt;br /&gt;
 # containing the regex&lt;br /&gt;
 awk &amp;#039;/regex/{getline;print}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # grep for AAA and BBB and CCC (in any order)&lt;br /&gt;
 awk &amp;#039;/AAA/; /BBB/; /CCC/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # grep for AAA and BBB and CCC (in that order)&lt;br /&gt;
 awk &amp;#039;/AAA.*BBB.*CCC/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print only lines of 65 characters or longer&lt;br /&gt;
 awk &amp;#039;length &amp;gt; 64&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print only lines of less than 65 characters&lt;br /&gt;
 awk &amp;#039;length &amp;lt; 64&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print section of file from regular expression to end of file&lt;br /&gt;
 awk &amp;#039;/regex/,0&amp;#039;&lt;br /&gt;
 awk &amp;#039;/regex/,EOF&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print section of file based on line numbers (lines 8-12, inclusive)&lt;br /&gt;
 awk &amp;#039;NR==8,NR==12&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print line number 52&lt;br /&gt;
 awk &amp;#039;NR==52&amp;#039;&lt;br /&gt;
 awk &amp;#039;NR==52 {print;exit}&amp;#039;          # more efficient on large files&lt;br /&gt;
&lt;br /&gt;
 # print section of file between two regular expressions (inclusive)&lt;br /&gt;
 awk &amp;#039;/Iowa/,/Montana/&amp;#039;             # case sensitive&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECTIVE DELETION OF CERTAIN LINES:&lt;br /&gt;
&lt;br /&gt;
 # delete ALL blank lines from a file (same as &amp;quot;grep &amp;#039;.&amp;#039; &amp;quot;)&lt;br /&gt;
 awk NF&lt;br /&gt;
 awk &amp;#039;/./&amp;#039;&lt;/div&gt;</summary>
		<author><name>David</name></author>
	</entry>
</feed>