Sponsored Content
Top Forums Shell Programming and Scripting awk not working for calculating no of lines with criteria Post 302832575 by pamu on Monday 15th of July 2013 08:14:01 AM
Old 07-15-2013
Quote:
Originally Posted by siramitsharma
I have tar.gz file and i want to count the lines which are matching the criteria as well as which are not matching the criteria. Following is the code

Output Requirement:
Match the input from zcat with 26th filed having 02 value, in case it matches then print the output in a file & increase the match counter by 1 & in case it doesnt matches then increase the not match counter. At last i have 2 files one having the records in a.txt file & another file having match counter & not match counter values.

But this is not working, please help

Code:
zcat filename.tar.gz | awk -v mon="07" '
BEGIN {
 if (( (substr($0,26,2)=="02") && substr($0,84,2) == month  ))
  print $0 >> "a.txt"
  ++matchcounter
 else 
 ++notmatch 
 ;}
END { print matchcounter","notmatch >> "countfile"}
'

Try without BEGIN
Code:
zcat filename.tar.gz | awk -v mon="07" '
    {
 if (( (substr($0,26,2)=="02") && substr($0,84,2) == month  ))
  print $0 >> "a.txt"
  ++matchcounter
 else 
 ++notmatch}
    END { print matchcounter","notmatch >> "countfile"}'

 

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

selection criteria in Access query not working

Attached are views of the components of a dummy Access database. The database represents an example of the problem that has reared its ugly head. The query example is a simple "Selection" query, which, after getting it to work, will become an "Append" query. The selected data will be appended... (1 Reply)
Discussion started by: hipockets
1 Replies

2. Shell Programming and Scripting

Delete new lines based on search criteria

Hi all! A bit of background: I am trying to create a script that formats SQL statements. I have gotten so far as to add new lines based on certain match criteria like commas, keywords etc. In the process, I end up adding newlines where I don't want. For example: substr(colName, 1, 10)... (3 Replies)
Discussion started by: jayarkay
3 Replies

3. Shell Programming and Scripting

Replacing lines which match certain criteria

Hi, I have code which is like this <TABLE name="UsageDetail_24> <ROW> <Date24><!]></Date24> <Time24><!]></Time24> <Destination24><!]></Destination24> <Rate24><!]></Rate24> <Duration24><!]></Duration24> <Cost24><!]></Cost24> <Allowance24><!]></Allowance24> </ROW> <ROW>... (3 Replies)
Discussion started by: legolad
3 Replies

4. Shell Programming and Scripting

Calculating 12th working day

I have a business requirement in my project where I need to calculate the 12th working day of every month. Can any please tell me the solution to my problem. Thanks in advance (7 Replies)
Discussion started by: ami_smart
7 Replies

5. Shell Programming and Scripting

[Solved] awk calculating between lines

Hey guys, maybe you can help me with this... I want to read input.dat line by line, while doing a simple calculation between the second column value of the current line and the second column value of the next line (like a difference). input is something like this: 0 3.945757 1 ... (1 Reply)
Discussion started by: origamisven
1 Replies

6. Shell Programming and Scripting

Print lines that match certain criteria

Hi all I have a text file with the following format: id col1 col2 col3 col4 col5 col6 col7 ... row1 0 0 0 0 0 0 0 row2 0 0 0 0 0 0 0 row3 0 0 0 0 0 0.2 0 row4 0 0 0 0 0 0 0 row5 0 0 0 0 0 0 0 row6 0 0 0 0.1 0 0 0 row7 0 0 0 0 0 0 0 row8 0 0 0 0 0 0 0 row9 0 0 0 0 0 0 0 ... The file... (2 Replies)
Discussion started by: gautig
2 Replies

7. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {beginning of file} New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line. New: Hello losttouch. You seem to be struggling... (4 Replies)
Discussion started by: losttouch
4 Replies

8. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

9. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

10. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 Replies
tre-agrep(1)						      General Commands Manual						      tre-agrep(1)

NAME
tre-agrep - print lines approximately matching a pattern SYNOPSIS
tre-agrep [OPTION]... PATTERN [FILE]... DESCRIPTION
Searches for approximate matches of PATTERN in each FILE or standard input. Example: `tre-agrep -2 optimize foo.txt' outputs all lines in file `foo.txt' that match "optimize" within two errors. E.g. lines which contain "optimise", "optmise", and "opitmize" all match. OPTIONS
Regexp selection and interpretation: -e PATTERN, --regexp=PATTERN Use PATTERN as a regular expression; useful to protect patterns beginning with -. -i, --ignore-case Ignore case distinctions (as defined by the current locale) in PATTERN and input files. -k, --literal Treat PATTERN as a literal string, that is, a fixed string with no special characters. -w, --word-regexp Force PATTERN to match only whole words. A "whole word" is a substring which either starts at the beginning or the record or is preceded by a non-word constituent character. Similarly, the substring must either end at the end of the record or be followed by a non-word constituent character. Word-constituent characters are alphanumerics (as defined by the current locale) and the under- score character. Note that the non-word constituent characters must surround the match; they cannot be counted as errors. Approximate matching settings: -D NUM, --delete-cost=NUM Set cost of missing characters to NUM. -I NUM, --insert-cost=NUM Set cost of extra characters to NUM. -S NUM, --substitute-cost=NUM Set cost of incorrect characters to NUM. Note that a deletion (a missing character) and an insertion (an extra character) together constitute a substituted character, but the cost will be the that of a deletion and an insertion added together. Thus, if the const of a substitution is set to be larger than the sum of the costs of deletion and insertion, direct substitutions will never be done. -E NUM, --max-errors=NUM Select records that have at most NUM errors. -# Select records that have at most # errors (# is a digit between 0 and 9). Miscellaneous: -d PATTERN, --delimiter=PATTERN Set the record delimiter regular expression to PATTERN. The text between two delimiters, before the first delimiter, and after the last delimiter is considered to be a record. The default record delimiter is the regexp " ", so by default a record is a line. PATTERN can be any regular expression that does not match the empty string. For example, using -d "^From " defines mail messages as records in a Mailbox format file. -v, --invert-match Select non-matching records instead of matching records. -V, --version Print version information and exit. -y, --nothing Does nothing. This options exists only for compatibility with the non-free agrep program. --help Display a brief help message and exit. Output control: -B, --best-match Only output the best matching records, that is, the records with the lowest cost. This is currently implemented by making two passes over the input files and cannot be used when reading from standard input. --color, --colour Highlight the matching strings in the output with a color marker. The color string is taken from the GREP_COLOR environment vari- able. The default color is red. -c, --count Only print a count of matching records per each input file, suppressing normal output. -h, --no-filename Suppress the prefixing filename on output when multiple files are searched. -H, --with-filename Prefix each output record with the name of the input file where the record was read from. -l, --files-with-matches Only print the name of each input file which contains at least one match, suppressing normal output. The scanning for each file will stop on the first match. -n, --record-number Prefix each output record with its sequence number in the input file. The number of the first record is 1. -q, --quiet, --silent Do not write anything to standard output. Exit immediately with zero exit status if a match is found. -s, --show-cost Print match cost with output. --show-position Prefix each output record with the start and end offset of the first match within the record. The offset of the first character of the record is 0. The end position is given as the offset of the first character after the match. -M, --delimiter-after By default, the record delimiter is the newline character and is output after the matching record. If -d is used, the record delim- iter will be output before the matching record. This option causes the delimiter to be output after the matching record. With no FILE, or when FILE is -, reads standard input. If less than two FILEs are given -h is assumed, otherwise -H is the default. DIAGNOSTICS
Exit status is 0 if a match is found, 1 for no match, and 2 if there were errors. If -E or -# is not specified, only exact matches are selected. PATTERN is a POSIX extended regular expression (ERE) with the TRE extensions. REPORTING BUGS
Report bugs to the TRE mailing list <tre-general@lists.laurikari.net>. COPYRIGHT
Copyright (C) 2002-2004 Ville Laurikari. This is free software, and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute this software under certain conditions; see the source for the full license text. TRE agrep 0.8.0 November 21, 2004 tre-agrep(1)
All times are GMT -4. The time now is 04:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy