Visit Our UNIX and Linux User Community

Top Forums Shell Programming and Scripting Print certain lines based on condition Post 302844025 by Don Cragun on Friday 16th of August 2013 06:37:35 AM
Old 08-16-2013
Hi ckwan,
Here is a version of the script with comments added. Let me know if something is still not clear.
awk '
# Define a function named printlist()
function printlist(     i) { # It has a local variable (i).
        for(i = 1; i <= c; i++) # For every line in the array l[], 
                printf("%s\n", l[i]) # print that line.
        print "" # Then print an empty line as a separator.
# If the 1st character on a line is '[',
/^[[]/ {if(c > 2) printlist() # if we have more than two lines saved, print the saved lines.
        c = 0 # Clear the number of saved lines no matter how many lines were saved before.
# If the input line is not empty,
/./ {   l[++c] = $0 # increment the number of saved lines (c) and save the current line.
# When we hit end-of-file,
END {   if(c > 2) printlist()  # if we have more than two lines saved, print the saved lines.
}' listing # Our input file is named "listing"

For any Linux or UNIX system utility, you should be able to find out how it works by issuing the command:
man 1 utility_name

If that doesn't work for you, try looking at the "Man Pages" section of this forum. The POSIX section gives you the (hopefully) portable subset of options for a utility that should work on most systems. For awk, that would be the POSIX awk utility description.

I avoided the simpler solution (suggested by Scrutinizer and Jotne) using RS= because although setting the input record separator to an empty string works on some implementations of awk; it is not portable. The standards only specify the behavior of an awk program if RS is defined to be a single character other than the NULL character.
Test Your Knowledge in Computers #999
Difficulty: Easy
The trusted computing base (TCB) of a computer system is the set of all hardware, firmware, and/or software components that are critical to its security.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies

2. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

3. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies

4. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

5. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

6. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

7. Shell Programming and Scripting

Print every 5 lines with special condition

Hi Friends, I have an input file like this chr1 100 200 chr1 200 300 chr1 300 400 chr1 400 500 chr1 500 600 chr1 600 700 chr1 700 800 chr1 800 900 chr1 900 920 chr1 940 960 I would like to get the first line's second column and the fifth line's 3rd column as one single line. This... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

8. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

9. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

10. Shell Programming and Scripting

awk to reformat lines based on condition

The awk below uses the tab-delimeted fileand reformats each line based on one of three conditions (rules). The 3 rules are for deletion (lines in blue), snv (line in red), and insertion (lines in green). I have included all possible combinations of lines from my actual data, which is very large.... (0 Replies)
Discussion started by: cmccabe
0 Replies
awk(1)							      General Commands Manual							    awk(1)

       awk - pattern scanning and processing language

       awk [-Fc] [-f prog] [-] [file...]

       The  command scans each input file for lines that match any of a set of patterns specified in prog.  With each pattern in prog there can be
       an associated action that will be performed when a line of a file matches the pattern.  The set of patterns may appear literally  as  prog,
       or in a file specified as -f prog.

       Files  are  read  in  order;  if there are no files, the standard input is read.  The file name `-' means the standard input.  Each line is
       matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern.

       An input line is made up of fields separated by white space.  (This default can be changed by using FS, as described  below.)   The  fields
       are denoted $1, $2, ... ; $0 refers to the entire line.

       A pattern-action statement has the form

	    pattern { action }

       A missing { action } means print the line; a missing pattern always matches.

       An action is a sequence of statements.  A statement can be one of the following:

	    if ( conditional ) statement [ else statement ]
	    while ( conditional ) statement
	    for ( expression ; conditional ; expression ) statement
	    { [ statement ] ... }
	    variable = expression
	    print [ expression-list ] [ >expression ]
	    printf format [ , expression-list ] [ >expression ]
	    next # skip remaining patterns on this input line
	    exit # skip the rest of the input

       Statements  are terminated by semicolons, new lines or right braces.  An empty expression-list stands for the whole line.  Expressions take
       on string or numeric values as appropriate, and are built using the operators +, -, *, /, %,  and concatenation	(indicated  by	a  blank).
       The  C operators ++, --, +=, -=, *=, /=, and %= are also available in expressions.  Variables may be scalars, array elements (denoted x[i])
       or fields.  Variables are initialized to the null string.  Array subscripts may be any string, not necessarily numeric; this allows  for  a
       form of associative memory.  String constants are quoted "...".

       The  print  statement prints its arguments on the standard output (or on a file if >file is present), separated by the current output field
       separator, and terminated by the output record separator.  The statement formats its expression list according to the format.  For  further
       information, see

       The  built-in  function	length	returns the length of its argument taken as a string, or of the whole line if no argument.  There are also
       built-in functions exp, log, sqrt, and int.  The last truncates its argument to an integer.  substr(s, m, n) returns the  n-character  sub-
       string  of  s that begins at position m.  The function sprintf(fmt, expr, expr, ...)  formats the expressions according to the format given
       by fmt and returns the resulting string.

       Patterns are arbitrary Boolean combinations (!, ||, &&, and parentheses)  of  regular  expressions  and	relational  expressions.   Regular
       expressions  must be surrounded by slashes and are as in egrep.	Isolated regular expressions in a pattern apply to the entire line.  Regu-
       lar expressions may also occur in relational expressions.

       A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines between	an  occurrence	of
       the first pattern and the next occurrence of the second.

       A relational expression is one of the following:

	    expression matchop regular-expression
	    expression relop expression

       where a relop is any of the six relational operators in C, and a matchop is either ~ (for contains) or !~ (for does not contain).  A condi-
       tional is an arithmetic expression, a relational expression, or a Boolean combination of these.

       The special patterns BEGIN and END may be used to capture control before the first input line is read and after the last.   BEGIN  must	be
       the first pattern, END the last.

       A single character c may be used to separate the fields by starting the program with

	    BEGIN { FS = "c" }

       or by using the -Fc option.

       Other  variable	names  with special meanings include NF, the number of fields in the current record; NR, the ordinal number of the current
       record; FILENAME, the name of the current input file; OFS, the output field separator (default blank); ORS,  the  output  record  separator
       (default new line); and OFMT, the output format for numbers (default "%.6g").

       -	 Used for standard input file.

       -Fc	 Sets interfield separator to named character.

       -fprog	 Uses prog file for patterns and actions.

       Print lines longer than 72 characters:
	    length > 72

       Print first two fields in opposite order:
	    { print $2, $1 }

       Add up first column, print sum and average:
		 { s += $1 }
	    END  { print "sum is", s, " average is", s/NR }

       Print fields in reverse order:
	    { for (i = NF; i > 0; --i) print $i }

       Print all lines between start/stop pairs:
	    /start/, /stop/

       Print all lines whose first field is different from previous one:
	    $1 != prev { print; prev = $1 }

       There  are  no explicit conversions between numbers and strings.  To force an expression to be treated as a number add 0 to it; to force it
       to be treated as a string concatenate "" to it.

See Also
       lex(1), sed(1)
       "Awk - A Pattern Scanning and Processing Language" ULTRIX Supplementary Documents Vol. II: Programmer


Featured Tech Videos

All times are GMT -4. The time now is 09:30 AM.
Unix & Linux Forums Content Copyright 1993-2020. All Rights Reserved.
Privacy Policy