<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://halfface.se/wiki/index.php?action=history&amp;feed=atom&amp;title=Awk</id>
	<title>Awk - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://halfface.se/wiki/index.php?action=history&amp;feed=atom&amp;title=Awk"/>
	<link rel="alternate" type="text/html" href="https://halfface.se/wiki/index.php?title=Awk&amp;action=history"/>
	<updated>2026-04-19T07:27:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://halfface.se/wiki/index.php?title=Awk&amp;diff=16249&amp;oldid=prev</id>
		<title>Ekaanbj: /* Print lines where 4 column is empty */</title>
		<link rel="alternate" type="text/html" href="https://halfface.se/wiki/index.php?title=Awk&amp;diff=16249&amp;oldid=prev"/>
		<updated>2025-09-03T08:47:57Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Print lines where 4 column is empty&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=basic=&lt;br /&gt;
The following simple commands are used most often.&lt;br /&gt;
 awk &amp;#039;pattern&amp;#039; file   # print lines matching pattern&lt;br /&gt;
 awk &amp;#039;{action}&amp;#039; file  # take action for every line&lt;br /&gt;
 awk &amp;#039;pattern {action}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
 $0, $1, $2,... 	Field variables&lt;br /&gt;
 FILENAME 	Name of input file&lt;br /&gt;
 FS 	Field separator, by default the space character. Can be modified.&lt;br /&gt;
 NF 	Number of fields in the current record&lt;br /&gt;
 NR 	Number of the current record (line number)&lt;br /&gt;
 OFMT 	Output format (default: &amp;quot;%.6g&amp;quot;)&lt;br /&gt;
 RS	Record Separator&lt;br /&gt;
 ORS	Output Record Separator. By default, the output and input records separators are a carriage return stored in the built-in variables ORS and RS, respectively.&lt;br /&gt;
=print every 5 line=&lt;br /&gt;
 awk &amp;#039;NR % 5 == 0&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=awk script=&lt;br /&gt;
 awk &amp;#039;BEGIN           {initializations}&lt;br /&gt;
      search_pattern1 {actions}&lt;br /&gt;
      search_pattern2 {actions}&lt;br /&gt;
      ...&lt;br /&gt;
      END             {final actions}&amp;#039;  file&lt;br /&gt;
&lt;br /&gt;
=useful=&lt;br /&gt;
Remove 2 first lines of file.&lt;br /&gt;
 awk &amp;#039;NR &amp;gt; 2&amp;#039; /tmp/tmp&lt;br /&gt;
&lt;br /&gt;
Get media information. Pick encoded date, replace : with -, print row 6 and 7 and add .mov. Find just once.&lt;br /&gt;
 mediainfo *.mov | awk &amp;#039;/Encoded date/ { gsub(/:/,&amp;quot;-&amp;quot;) ; print $6 &amp;quot;_&amp;quot; $7 &amp;quot;.mov&amp;quot; ;exit }&amp;#039;&lt;br /&gt;
=awk with replace=&lt;br /&gt;
Replace systemd with andreas and print column 1.&lt;br /&gt;
 awk -F: &amp;#039;{gsub (&amp;quot;systemd&amp;quot;,&amp;quot;andreas&amp;quot;)}&amp;#039;&amp;#039;{print $1}&amp;#039; /etc/passwd&lt;br /&gt;
&lt;br /&gt;
=Move last column to first=&lt;br /&gt;
 | awk &amp;#039;{ print $NF, $0 }&amp;#039; | sed &amp;#039;s/[^ ]*$//g&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=remove xml comments=&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 awk -v RS=&amp;#039;&amp;lt;!--|--&amp;gt;&amp;#039; &amp;#039;NR%2&amp;#039; file&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
=remove column 6 and 8.=&lt;br /&gt;
 awk &amp;#039;{$6=$8=&amp;quot;&amp;quot;; print $0}&amp;#039; file&lt;br /&gt;
&lt;br /&gt;
=print something between tags=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt; &lt;br /&gt;
awk -F&amp;#039;[&amp;lt;|&amp;gt;]&amp;#039; &amp;#039;/jndi/{print $3}&amp;#039;&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=print lines containing both cat, dog and bird.=&lt;br /&gt;
 awk &amp;#039;/cat/ &amp;amp;&amp;amp; /dog/ &amp;amp;&amp;amp; /bird/&amp;#039; files&lt;br /&gt;
&lt;br /&gt;
=Print rows where first column doesnt contains ABC=&lt;br /&gt;
 awk &amp;#039;$1 !~ /ABC/&amp;#039; file # print lines whose 1st field doesn&amp;#039;t&lt;br /&gt;
=Print lines where 8 column does not contain a date=&lt;br /&gt;
 awk -F&amp;#039;,&amp;#039; &amp;#039;$8 !~ /[0-9]{4,}/&amp;#039; list.csv&lt;br /&gt;
&lt;br /&gt;
=Print row 5 and 9.=&lt;br /&gt;
 awk &amp;#039;NR==5 || NR==9&amp;#039; &amp;quot;file&amp;quot;&lt;br /&gt;
Print row betwen 5 and 9.&lt;br /&gt;
 awk &amp;#039;NR&amp;gt;=5&amp;amp;&amp;amp;NR&amp;lt;=9&amp;#039; &amp;quot;file&amp;quot;&lt;br /&gt;
Print column 4 to the end of passwd. Field separator set to :&lt;br /&gt;
 cat /etc/passwd | awk &amp;#039; BEGIN { FS=&amp;quot;:&amp;quot; } {&lt;br /&gt;
 for (i=4; i&amp;lt;=NF; i++)&lt;br /&gt;
 printf(&amp;quot;%s &amp;quot;, $i)&lt;br /&gt;
 printf(&amp;quot;\n&amp;quot;) # CR at end of line&lt;br /&gt;
 } &amp;#039;&lt;br /&gt;
=Summarize size of files.=&lt;br /&gt;
 find . -type f -printf &amp;#039;%s\n&amp;#039; | awk &amp;#039;{ a+=$1 } END { print a }&amp;#039;&lt;br /&gt;
Summarize on 5 column. MB&lt;br /&gt;
 awk &amp;#039;{ SUM +=  $5} END { print SUM/1024/1024 }&amp;#039; &lt;br /&gt;
Summarize on 5 column. GB&lt;br /&gt;
 awk &amp;#039;{ SUM +=  $5} END { print SUM/1024/1024/1024 }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=Print lines containing T2 in second column.=&lt;br /&gt;
 awk &amp;#039;$2 ~ /T2/&amp;#039; file&lt;br /&gt;
Print second column where 1 columnt contains nodev&lt;br /&gt;
 awk &amp;#039;$1 == &amp;quot;nodev&amp;quot; { print $2 }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=Print lines where first column value is bigger than 10=&lt;br /&gt;
 awk &amp;#039;$1 &amp;gt; 10&amp;#039;&lt;br /&gt;
=lenght=&lt;br /&gt;
Print length of string.&lt;br /&gt;
 awk &amp;#039;{ print length(), $0 }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=Print fields matching regexp in 5 column delimiter being ,=&lt;br /&gt;
 awk -F&amp;#039;,&amp;#039; &amp;#039;$5 ~ /^10.[0-9]*.21.[0-9]*/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=Print lines where 4 column is empty=&lt;br /&gt;
 awk &amp;#039;BEGIN {FS=&amp;quot;,&amp;quot;} $4==&amp;quot;&amp;quot; {print}&amp;#039;&lt;br /&gt;
=Match more columns=&lt;br /&gt;
 awk &amp;#039;$5 == &amp;quot;10.100.22.105&amp;quot; &amp;amp;&amp;amp; $7 == &amp;quot;9094&amp;quot;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=awk one liners=&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FILE SPACING:&lt;br /&gt;
&lt;br /&gt;
# double space a file&lt;br /&gt;
awk ‘1;{print “”}’&lt;br /&gt;
awk ‘BEGIN{ORS=”\n\n”};1′&lt;br /&gt;
&lt;br /&gt;
# double space a file which already has blank lines in it. Output file&lt;br /&gt;
# should contain no more than one blank line between lines of text.&lt;br /&gt;
# NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are&lt;br /&gt;
# often treated as non-blank, and thus ‘NF’ alone will return TRUE.&lt;br /&gt;
awk ‘NF{print $0 “\n”}’&lt;br /&gt;
&lt;br /&gt;
# triple space a file&lt;br /&gt;
awk ‘1;{print “\n”}’&lt;br /&gt;
&lt;br /&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 ‘{print FNR “\t” $0}’ files*&lt;br /&gt;
&lt;br /&gt;
# precede each line by its line number FOR ALL FILES TOGETHER, with tab.&lt;br /&gt;
awk ‘{print NR “\t” $0}’ 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 ‘{printf(”%5d : %s\n”, NR,$0)}’&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 ‘NF{$0=++a ” :” $0};{print}’&lt;br /&gt;
awk ‘{print (NF? ++a ” :” :”&amp;quot;) $0}’&lt;br /&gt;
&lt;br /&gt;
# count lines (emulates “wc -l”)&lt;br /&gt;
awk ‘END{print NR}’&lt;br /&gt;
&lt;br /&gt;
# print the sums of the fields of every line&lt;br /&gt;
awk ‘{s=0; for (i=1; i max {max=$1; maxline=$0}; END{ print max, maxline}’&lt;br /&gt;
&lt;br /&gt;
# print the number of fields in each line, followed by the line&lt;br /&gt;
awk ‘{ print NF “:” $0 } ‘&lt;br /&gt;
&lt;br /&gt;
# print the last field of each line&lt;br /&gt;
awk ‘{ print $NF }’&lt;br /&gt;
&lt;br /&gt;
# print the last field of the last line&lt;br /&gt;
awk ‘{ field = $NF }; END{ print field }’&lt;br /&gt;
&lt;br /&gt;
# print every line with more than 4 fields&lt;br /&gt;
awk ‘NF &amp;gt; 4′&lt;br /&gt;
&lt;br /&gt;
# print every line where the value of the last field is &amp;gt; 4&lt;br /&gt;
awk ‘$NF &amp;gt; 4′&lt;br /&gt;
&lt;br /&gt;
TEXT CONVERSION AND SUBSTITUTION:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format&lt;br /&gt;
awk ‘{sub(/\r$/,”&amp;quot;);print}’ # 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 ‘{sub(/$/,”\r”);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=”w” ‘1′ infile &amp;gt;outfile&lt;br /&gt;
&lt;br /&gt;
# Use “tr” instead.&lt;br /&gt;
tr -d \r 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 ‘{sub(/^[ \t]+/, “”); print}’&lt;br /&gt;
&lt;br /&gt;
# delete trailing whitespace (spaces, tabs) from end of each line&lt;br /&gt;
awk ‘{sub(/[ \t]+$/, “”);print}’&lt;br /&gt;
&lt;br /&gt;
# delete BOTH leading and trailing whitespace from each line&lt;br /&gt;
awk ‘{gsub(/^[ \t]+|[ \t]+$/,”&amp;quot;);print}’&lt;br /&gt;
awk ‘{$1=$1;print}’ # 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 ‘{sub(/^/, ” “);print}’&lt;br /&gt;
&lt;br /&gt;
# align all text flush right on a 79-column width&lt;br /&gt;
awk ‘{printf “%79s\n”, $0}’ file*&lt;br /&gt;
&lt;br /&gt;
# center all text on a 79-character width&lt;br /&gt;
awk ‘{l=length();s=int((79-l)/2); printf “%”(s+l)”s\n”,$0}’ file*&lt;br /&gt;
&lt;br /&gt;
# substitute (find and replace) “foo” with “bar” on each line&lt;br /&gt;
awk ‘{sub(/foo/,”bar”);print}’ # replaces only 1st instance&lt;br /&gt;
gawk ‘{$0=gensub(/foo/,”bar”,4);print}’ # replaces only 4th instance&lt;br /&gt;
awk ‘{gsub(/foo/,”bar”);print}’ # replaces ALL instances in a line&lt;br /&gt;
&lt;br /&gt;
# substitute “foo” with “bar” ONLY for lines which contain “baz”&lt;br /&gt;
awk ‘/baz/{gsub(/foo/, “bar”)};{print}’&lt;br /&gt;
&lt;br /&gt;
# substitute “foo” with “bar” EXCEPT for lines which contain “baz”&lt;br /&gt;
awk ‘!/baz/{gsub(/foo/, “bar”)};{print}’&lt;br /&gt;
&lt;br /&gt;
# change “scarlet” or “ruby” or “puce” to “red”&lt;br /&gt;
awk ‘{gsub(/scarlet|ruby|puce/, “red”); print}’&lt;br /&gt;
&lt;br /&gt;
# reverse order of lines (emulates “tac”)&lt;br /&gt;
awk ‘{a[i++]=$0} END {for (j=i-1; j&amp;gt;=0;) print a[j–] }’ 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 ‘/\\$/ {sub(/\\$/,”&amp;quot;); getline t; print $0 t; next}; 1′ file*&lt;br /&gt;
&lt;br /&gt;
# print and sort the login names of all users&lt;br /&gt;
awk -F “:” ‘{ print $1 | “sort” }’ /etc/passwd&lt;br /&gt;
&lt;br /&gt;
# print the first 2 fields, in opposite order, of every line&lt;br /&gt;
awk ‘{print $2, $1}’ file&lt;br /&gt;
&lt;br /&gt;
# switch the first 2 fields of every line&lt;br /&gt;
awk ‘{temp = $1; $1 = $2; $2 = temp}’ file&lt;br /&gt;
&lt;br /&gt;
# print every line, deleting the second field of that line&lt;br /&gt;
awk ‘{ $2 = “”; print }’&lt;br /&gt;
&lt;br /&gt;
# print in reverse order the fields of every line&lt;br /&gt;
awk ‘{for (i=NF; i&amp;gt;0; i–) printf(”%s “,i);printf (”\n”)}’ file&lt;br /&gt;
&lt;br /&gt;
# remove duplicate, consecutive lines (emulates “uniq”)&lt;br /&gt;
awk ‘a !~ $0; {a=$0}’&lt;br /&gt;
&lt;br /&gt;
# remove duplicate, nonconsecutive lines&lt;br /&gt;
awk ‘! a[$0]++’ # most concise script&lt;br /&gt;
awk ‘!($0 in a) {a[$0];print}’ # 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 ‘ORS=%NR%5?”,”:”\n”‘ file&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 “head”)&lt;br /&gt;
awk ‘NR 1{exit};1′&lt;br /&gt;
&lt;br /&gt;
# print the last 2 lines of a file (emulates “tail -2″)&lt;br /&gt;
awk ‘{y=x “\n” $0; x=$0};END{print y}’&lt;br /&gt;
&lt;br /&gt;
# print the last line of a file (emulates “tail -1″)&lt;br /&gt;
awk ‘END{print}’&lt;br /&gt;
&lt;br /&gt;
# print only lines which match regular expression (emulates “grep”)&lt;br /&gt;
awk ‘/regex/’&lt;br /&gt;
&lt;br /&gt;
# print only lines which do NOT match regex (emulates “grep -v”)&lt;br /&gt;
awk ‘!/regex/’&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 ‘/regex/{print x};{x=$0}’&lt;br /&gt;
awk ‘/regex/{print (x==”&amp;quot; ? “match on line 1″ : x)};{x=$0}’&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 ‘/regex/{getline;print}’&lt;br /&gt;
&lt;br /&gt;
# grep for AAA and BBB and CCC (in any order)&lt;br /&gt;
awk ‘/AAA/; /BBB/; /CCC/’&lt;br /&gt;
&lt;br /&gt;
# grep for AAA and BBB and CCC (in that order)&lt;br /&gt;
awk ‘/AAA.*BBB.*CCC/’&lt;br /&gt;
&lt;br /&gt;
# print only lines of 65 characters or longer&lt;br /&gt;
awk ‘length &amp;gt; 64′&lt;br /&gt;
&lt;br /&gt;
# print only lines of less than 65 characters&lt;br /&gt;
awk ‘length&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;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=run command on mathed pattern=&lt;br /&gt;
 ls -la | awk &amp;#039;/^drwx/ { system(&amp;quot;echo directory with certain permissions&amp;quot; $NF) }&amp;#039;&lt;br /&gt;
=get average on a series of digits=&lt;br /&gt;
 awk &amp;#039;{ total += $1; count++ } END { print total/count }&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Category:Applications]]&lt;br /&gt;
[[Category:Unix]]&lt;br /&gt;
[[Category:Commands]]&lt;/div&gt;</summary>
		<author><name>Ekaanbj</name></author>
	</entry>
</feed>