<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="lt">
	<id>https://wiki.eofnet.lt/w//index.php?action=history&amp;feed=atom&amp;title=Awk1line</id>
	<title>Awk1line - Versijų istorija</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.eofnet.lt/w//index.php?action=history&amp;feed=atom&amp;title=Awk1line"/>
	<link rel="alternate" type="text/html" href="https://wiki.eofnet.lt/w//index.php?title=Awk1line&amp;action=history"/>
	<updated>2026-04-17T08:10:39Z</updated>
	<subtitle>Šio puslapio versijų istorija projekte</subtitle>
	<generator>MediaWiki 1.35.1</generator>
	<entry>
		<id>https://wiki.eofnet.lt/w//index.php?title=Awk1line&amp;diff=3304&amp;oldid=prev</id>
		<title>\dev\null: straipsnis sukurtas</title>
		<link rel="alternate" type="text/html" href="https://wiki.eofnet.lt/w//index.php?title=Awk1line&amp;diff=3304&amp;oldid=prev"/>
		<updated>2014-05-04T13:09:12Z</updated>

		<summary type="html">&lt;p&gt;straipsnis sukurtas&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Naujas puslapis&lt;/b&gt;&lt;/p&gt;&lt;div&gt;'''HANDY ONE-LINE SCRIPTS FOR AWK'''                               30 April 2008&lt;br /&gt;
&lt;br /&gt;
Compiled by Eric Pement - eric [at] pement.org               version 0.27&lt;br /&gt;
&lt;br /&gt;
'''USAGE:'''&lt;br /&gt;
&lt;br /&gt;
 Unix: awk '/pattern/ {print &amp;quot;$1&amp;quot;}'    # standard Unix shells&lt;br /&gt;
 DOS/Win: awk '/pattern/ {print &amp;quot;$1&amp;quot;}'    # compiled with DJGPP, Cygwin&lt;br /&gt;
         awk &amp;quot;/pattern/ {print \&amp;quot;$1\&amp;quot;}&amp;quot;  # GnuWin32, UnxUtils, Mingw&lt;br /&gt;
&lt;br /&gt;
Note that the DJGPP compilation (for DOS or Windows-32) permits an awk&lt;br /&gt;
script to follow Unix quoting syntax '/like/ {&amp;quot;this&amp;quot;}'. HOWEVER, if the&lt;br /&gt;
command interpreter is CMD.EXE or COMMAND.COM, single quotes will not&lt;br /&gt;
protect the redirection arrows (&amp;lt;, &amp;gt;) nor do they protect pipes (|).&lt;br /&gt;
These are special symbols which require &amp;quot;double quotes&amp;quot; to protect them&lt;br /&gt;
from interpretation as operating system directives. If the command&lt;br /&gt;
interpreter is bash, ksh or another Unix shell, then single and double&lt;br /&gt;
quotes will follow the standard Unix usage.&lt;br /&gt;
&lt;br /&gt;
Users of MS-DOS or Microsoft Windows must remember that the percent&lt;br /&gt;
sign (%) is used to indicate environment variables, so this symbol must&lt;br /&gt;
be doubled (%%) to yield a single percent sign visible to awk.&lt;br /&gt;
&lt;br /&gt;
If a script will not need to be quoted in Unix, DOS, or CMD, then I&lt;br /&gt;
normally omit the quote marks. If an example is peculiar to GNU awk,&lt;br /&gt;
the command 'gawk' will be used. Please notify me if you find errors or&lt;br /&gt;
new commands to add to this list (total length under 65 characters). I&lt;br /&gt;
usually try to put the shortest script first. To conserve space, I&lt;br /&gt;
normally use '1' instead of '{print}' to print each line. Either one&lt;br /&gt;
will work.&lt;br /&gt;
&lt;br /&gt;
'''FILE SPACING:'''&lt;br /&gt;
&lt;br /&gt;
 # double space a file&lt;br /&gt;
 awk '1;{print &amp;quot;&amp;quot;}'&lt;br /&gt;
 awk 'BEGIN{ORS=&amp;quot;\n\n&amp;quot;};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 &amp;quot;\n&amp;quot;}'&lt;br /&gt;
&lt;br /&gt;
 # triple space a file&lt;br /&gt;
 awk '1;{print &amp;quot;\n&amp;quot;}'&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 &amp;quot;\t&amp;quot; $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 &amp;quot;\t&amp;quot; $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(&amp;quot;%5d : %s\n&amp;quot;, 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 &amp;quot; :&amp;quot; $0};1'&lt;br /&gt;
 awk '{print (NF? ++a &amp;quot; :&amp;quot; :&amp;quot;&amp;quot;) $0}'&lt;br /&gt;
&lt;br /&gt;
 # count lines (emulates &amp;quot;wc -l&amp;quot;)&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&amp;lt;=NF; i++) s=s+$i; print s}'&lt;br /&gt;
&lt;br /&gt;
 # add all fields in all lines and print the sum&lt;br /&gt;
 awk '{for (i=1; i&amp;lt;=NF; i++) s=s+$i}; END{print s}'&lt;br /&gt;
&lt;br /&gt;
 # print every line after replacing each field with its absolute value&lt;br /&gt;
 awk '{for (i=1; i&amp;lt;=NF; i++) if ($i &amp;lt; 0) $i = -$i; print }'&lt;br /&gt;
 awk '{for (i=1; i&amp;lt;=NF; i++) $i = ($i &amp;lt; 0) ? -$i : $i; print }'&lt;br /&gt;
&lt;br /&gt;
 # print the total number of fields (&amp;quot;words&amp;quot;) in all lines&lt;br /&gt;
 awk '{ total = total + NF }; END {print total}' file&lt;br /&gt;
&lt;br /&gt;
 # print the total number of lines that contain &amp;quot;Beth&amp;quot;&lt;br /&gt;
 awk '/Beth/{n++}; END {print n+0}' 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 '$1 &amp;gt; 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 &amp;quot;:&amp;quot; $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;
'''STRING CREATION:'''&lt;br /&gt;
&lt;br /&gt;
 # create a string of a specific length (e.g., generate 513 spaces)&lt;br /&gt;
 awk 'BEGIN{while (a++&amp;lt;513) s=s &amp;quot; &amp;quot;; print s}'&lt;br /&gt;
&lt;br /&gt;
 # insert a string of specific length at a certain character position&lt;br /&gt;
 # Example: insert 49 spaces after column #6 of each input line.&lt;br /&gt;
 gawk --re-interval 'BEGIN{while(a++&amp;lt;49)s=s &amp;quot; &amp;quot;};{sub(/^.{6}/,&amp;quot;&amp;amp;&amp;quot; s)};1'&lt;br /&gt;
&lt;br /&gt;
'''ARRAY CREATION:'''&lt;br /&gt;
&lt;br /&gt;
 # These next 2 entries are not one-line scripts, but the technique&lt;br /&gt;
 # is so handy that it merits inclusion here.&lt;br /&gt;
 &lt;br /&gt;
 # create an array named &amp;quot;month&amp;quot;, indexed by numbers, so that month[1]&lt;br /&gt;
 # is 'Jan', month[2] is 'Feb', month[3] is 'Mar' and so on.&lt;br /&gt;
 split(&amp;quot;Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec&amp;quot;, month, &amp;quot; &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 # create an array named &amp;quot;mdigit&amp;quot;, indexed by strings, so that&lt;br /&gt;
 # mdigit[&amp;quot;Jan&amp;quot;] is 1, mdigit[&amp;quot;Feb&amp;quot;] is 2, etc. Requires &amp;quot;month&amp;quot; array&lt;br /&gt;
 for (i=1; i&amp;lt;=12; i++) mdigit[month[i]] = i&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 '{sub(/\r$/,&amp;quot;&amp;quot;)};1'   # 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(/$/,&amp;quot;\r&amp;quot;)};1'&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; '1' 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 '{sub(/^[ \t]+/, &amp;quot;&amp;quot;)};1'&lt;br /&gt;
&lt;br /&gt;
 # delete trailing whitespace (spaces, tabs) from end of each line&lt;br /&gt;
 awk '{sub(/[ \t]+$/, &amp;quot;&amp;quot;)};1'&lt;br /&gt;
&lt;br /&gt;
 # delete BOTH leading and trailing whitespace from each line&lt;br /&gt;
 awk '{gsub(/^[ \t]+|[ \t]+$/,&amp;quot;&amp;quot;)};1'&lt;br /&gt;
 awk '{$1=$1};1'           # 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(/^/, &amp;quot;     &amp;quot;)};1'&lt;br /&gt;
&lt;br /&gt;
 # align all text flush right on a 79-column width&lt;br /&gt;
 awk '{printf &amp;quot;%79s\n&amp;quot;, $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 &amp;quot;%&amp;quot;(s+l)&amp;quot;s\n&amp;quot;,$0}' 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 '{sub(/foo/,&amp;quot;bar&amp;quot;)}; 1'           # replace only 1st instance&lt;br /&gt;
 gawk '{$0=gensub(/foo/,&amp;quot;bar&amp;quot;,4)}; 1'  # replace only 4th instance&lt;br /&gt;
 awk '{gsub(/foo/,&amp;quot;bar&amp;quot;)}; 1'          # replace 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 '/baz/{gsub(/foo/, &amp;quot;bar&amp;quot;)}; 1'&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 '!/baz/{gsub(/foo/, &amp;quot;bar&amp;quot;)}; 1'&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 '{gsub(/scarlet|ruby|puce/, &amp;quot;red&amp;quot;)}; 1'&lt;br /&gt;
&lt;br /&gt;
 # reverse order of lines (emulates &amp;quot;tac&amp;quot;)&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 (fails if&lt;br /&gt;
 # there are multiple lines ending with backslash...)&lt;br /&gt;
 awk '/\\$/ {sub(/\\$/,&amp;quot;&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 &amp;quot;:&amp;quot; '{print $1 | &amp;quot;sort&amp;quot; }' /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 = &amp;quot;&amp;quot;; 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(&amp;quot;%s &amp;quot;,$i);print &amp;quot;&amp;quot;}' file&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?&amp;quot;,&amp;quot;:&amp;quot;\n&amp;quot;' 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 &amp;quot;head&amp;quot;)&lt;br /&gt;
 awk 'NR &amp;lt; 11'&lt;br /&gt;
&lt;br /&gt;
 # print first line of file (emulates &amp;quot;head -1&amp;quot;)&lt;br /&gt;
 awk 'NR&amp;gt;1{exit};1'&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 '{y=x &amp;quot;\n&amp;quot; $0; x=$0};END{print y}'&lt;br /&gt;
&lt;br /&gt;
 # print the last line of a file (emulates &amp;quot;tail -1&amp;quot;)&lt;br /&gt;
 awk 'END{print}'&lt;br /&gt;
&lt;br /&gt;
 # print only lines which match regular expression (emulates &amp;quot;grep&amp;quot;)&lt;br /&gt;
 awk '/regex/'&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 '!/regex/'&lt;br /&gt;
&lt;br /&gt;
 # print any line where field #5 is equal to &amp;quot;abc123&amp;quot;&lt;br /&gt;
 awk '$5 == &amp;quot;abc123&amp;quot;'&lt;br /&gt;
&lt;br /&gt;
 # print only those lines where field #5 is NOT equal to &amp;quot;abc123&amp;quot;&lt;br /&gt;
 # This will also print lines which have less than 5 fields.&lt;br /&gt;
 awk '$5 != &amp;quot;abc123&amp;quot;'&lt;br /&gt;
 awk '!($5 == &amp;quot;abc123&amp;quot;)'&lt;br /&gt;
&lt;br /&gt;
 # matching a field against a regular expression&lt;br /&gt;
 awk '$7  ~ /^[a-f]/'    # print line if field #7 matches regex&lt;br /&gt;
 awk '$7 !~ /^[a-f]/'    # print line if field #7 does NOT match 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 (NR==1 ? &amp;quot;match on line 1&amp;quot; : 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 on the same line)&lt;br /&gt;
 awk '/AAA/ &amp;amp;&amp;amp; /BBB/ &amp;amp;&amp;amp; /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 &amp;lt; 64'&lt;br /&gt;
&lt;br /&gt;
 # print section of file from regular expression to end of file&lt;br /&gt;
 awk '/regex/,0'&lt;br /&gt;
 awk '/regex/,EOF'&lt;br /&gt;
&lt;br /&gt;
 # print section of file based on line numbers (lines 8-12, inclusive)&lt;br /&gt;
 awk 'NR==8,NR==12'&lt;br /&gt;
&lt;br /&gt;
 # print line number 52&lt;br /&gt;
 awk 'NR==52'&lt;br /&gt;
 awk 'NR==52 {print;exit}'          # more efficient on large files&lt;br /&gt;
&lt;br /&gt;
 # print section of file between two regular expressions (inclusive)&lt;br /&gt;
 awk '/Iowa/,/Montana/'             # case sensitive&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;quot;)&lt;br /&gt;
 awk NF&lt;br /&gt;
 awk '/./'&lt;br /&gt;
&lt;br /&gt;
 # remove duplicate, consecutive lines (emulates &amp;quot;uniq&amp;quot;)&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;
'''CREDITS AND THANKS:'''&lt;br /&gt;
&lt;br /&gt;
Special thanks to the late Peter S. Tillier (U.K.) for helping me with&lt;br /&gt;
the first release of this FAQ file, and to Daniel Jana, Yisu Dong, and&lt;br /&gt;
others for their suggestions and corrections.&lt;br /&gt;
&lt;br /&gt;
For additional syntax instructions, including the way to apply editing&lt;br /&gt;
commands from a disk file instead of the command line, consult:&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;sed &amp;amp; awk, 2nd Edition,&amp;quot; by Dale Dougherty and Arnold Robbins&lt;br /&gt;
  (O'Reilly, 1997)&lt;br /&gt;
  &amp;quot;UNIX Text Processing,&amp;quot; by Dale Dougherty and Tim O'Reilly (Hayden&lt;br /&gt;
  Books, 1987)&lt;br /&gt;
  &amp;quot;GAWK: Effective awk Programming,&amp;quot; 3d edition, by Arnold D. Robbins&lt;br /&gt;
  (O'Reilly, 2003) or at http://www.gnu.org/software/gawk/manual/&lt;br /&gt;
&lt;br /&gt;
To fully exploit the power of awk, one must understand &amp;quot;regular&lt;br /&gt;
expressions.&amp;quot; For detailed discussion of regular expressions, see&lt;br /&gt;
&amp;quot;Mastering Regular Expressions, 3d edition&amp;quot; by Jeffrey Friedl (O'Reilly,&lt;br /&gt;
2006).&lt;br /&gt;
&lt;br /&gt;
The info and manual (&amp;quot;man&amp;quot;) pages on Unix systems may be helpful (try&lt;br /&gt;
&amp;quot;man awk&amp;quot;, &amp;quot;man nawk&amp;quot;, &amp;quot;man gawk&amp;quot;, &amp;quot;man regexp&amp;quot;, or the section on&lt;br /&gt;
regular expressions in &amp;quot;man ed&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
USE OF '\t' IN awk SCRIPTS: For clarity in documentation, I have used&lt;br /&gt;
'\t' to indicate a tab character (0x09) in the scripts.  All versions of&lt;br /&gt;
awk should recognize this abbreviation.&lt;br /&gt;
&lt;br /&gt;
[[Category:Programavimas]]&lt;/div&gt;</summary>
		<author><name>\dev\null</name></author>
	</entry>
</feed>