14 comment

  1. Hi great article. One suggestion is that the file names in the awk scripts can be substituted for the standard input.

    e.g. to get the number of lines in a file you could use

    wc -l filename | awk '{ print $1 }'

    Andre

  2. Exactl, even I wanted to know the same. If anyone could help me with this. :)

    Ed Dagos September 16, 2011

    Hi All,
    Is it possible to find a pattern in the file and then print the previous 3 or 4 lines using awk/sed/grep ??
    Regards,

    1. Search for “foo” in data.txt and display 3 lines of leading context before matching line:

      grep -B3 "foo" data.txt

      The -A options display $NUM lines of trailing context after matching lines:

      grep -A3 "foo" data.txt
  3. Is there any way to use awk (or any other aspect of unix shell scripting) to “declare” fields from position a to position b etc in the outline of the input, and reference those fields, IF there are no “automatic separators like a spacee, semicolon, etc. I have input with no separators and would love to use something “like” the usual while read LINE ; do processinputintooutput done < filename, but I'm not sure it can be done in shell scripting without separators, though it sounds like awk would work if I DID have separators.

  4. i have a file a.dat with following input

    123456789

    the first 5 digits represents ABC
    the next 4 digits represents XYZ

    how do i use awk to do the following

    If XYZ = 6789 then print ABCXYZ in a new file
    else
    skip that line

Leave a Comment