awk script modification - treat certain files differently


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script modification - treat certain files differently
# 1  
Old 08-09-2013
awk script modification - treat certain files differently

Code:
awk  'BEGIN{OFS=","} FNR == 1
			{if (NR > 1) {print fn,fnr,nl}
                        fn=FILENAME; fnr = 1; nl = 0}
                        {fnr = FNR}
         		/UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++}
                        <'{system ("gunzip -cd FILENAME")}'
         	END                    {print fn,fnr,nl}
        ' /tmp/appscraps/* > /tmp/test.txt

the above scans all files in a given directory. prints the file name, number of lines in each file and number of lines found containing 'ERROR'.

im now trying to make it so that the script executes a command if any of the file it reads in isn't a regular file. i.e., if the file is a gzip file, then run a particular command.

the bolded is my attempt to include the gunzip command in there. unfortunately, i cannot "gunzip" all the files in the directory beforehand. this is because not all files in the directory will be "gzip" type. some will be regular files.

so i need the script to treat any .gz file it finds a different way so it can read it, count and print the number of lines that's in it, and the number of lines it found matching the pattern supplied (just as it would if the file had been a regular file).

Last edited by SkySmart; 08-09-2013 at 11:32 AM..
# 2  
Old 08-09-2013
change:
Code:
 /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++}
 <'{system ("gunzip -cd FILENAME")}'

to:
Code:
/UNUSUAL/ && FILENAME ~ /\.gz$/ {nl++}
{
cmd="gunzip -cd " FILENAME
cmd; close(cmd)
}

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 08-09-2013
Quote:
Originally Posted by vgersh99
change:
Code:
 /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++}
 <'{system ("gunzip -cd FILENAME")}'

to:
Code:
/UNUSUAL/ && FILENAME ~ /\.gz$/ {nl++}
{
cmd="gunzip -cd " FILENAME
cmd; close(cmd)
}

i tried that, but it didn't seem to work. it printed the same file over and over, thousands of times:

Code:
awk  'BEGIN{OFS=","} FNR == 1
			{if (NR > 1) {print fn,fnr,nl}
                        fn=FILENAME; fnr = 1; nl = 0}
                        {fnr = FNR}
                        /UNUSUAL/ && FILENAME ~ /\.gz$/ {nl++}
                        {
                            cmd="gunzip -cd " FILENAME
                            cmd; close(cmd)
                         }
         	END                    {print fn,fnr,nl}
        ' /tmp/appscraps/* > /tmp/test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk treating variables differently in UNIX-Linux

Hi, awk seem to be acting differently in Unix and Linux when it comes to formatting. This is making it difficult to migrate scripts. for example: UNIX: echo "123" |awk '{printf ("%05s\n" ,$1)}' 00123 echo "123" |awk '{printf ("%05d\n" ,$1)}' 00123 echo "S12" |awk '{printf ("%05s\n"... (9 Replies)
Discussion started by: wanderingmind16
9 Replies

2. Shell Programming and Scripting

awk script modification

can someone help me identify what i'm doing wrong here: awk -F'|' 'BEGIN{c=0} /./ && /./ { if ($3 < 2) { print ; c++ } END { print c":OK" } else if (($3 >= 2) && ($3 < 4)) { print ; c++ } END { print c":WARNING" } else if ($3 >= 4) { print ; c++ } END { print c":CRITICAL" } }'... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Why awk perform differently when using variable?

Hi Gurus, I hit a hard block in my script. when using awk command with variable, I got different result. Please see below: my test file as below: $ cat demofile.txt filename-yyyy-abcd filename-xxx-week-pass filename-xxx-week-run for testing purpose, I put 3 awk command in one script.... (7 Replies)
Discussion started by: ken6503
7 Replies

4. Shell Programming and Scripting

awk script modification

I want the below script to omit every chunk of data that contains a specific hostname. here's the scenario. i have a configuration file that contains the configuration of several hosts. a sample of this configuration file is this: define host { address ... (12 Replies)
Discussion started by: SkySmart
12 Replies

5. UNIX for Dummies Questions & Answers

doubt to treat plaintext script

Hi everyone! first of all thank you all for the forum. My question is, is there a bash or java program, which addresses the existing text in the html that is visible in the web page for editing by another string, eg name1: flakjsdlñfjas Name of father: fdfjaksdfjskdfsd Well it... (1 Reply)
Discussion started by: xavilito
1 Replies

6. Windows & DOS: Issues & Discussions

Awk script in DOS and Linux behaves differently :(

Hi, I have an awk script which performs simple operations of variable assignments and finally printing the variables in custom form. BEGIN {FS=OFS="\n"} { v1=substr($0,1,15) v2=substr($0,16,200) v3=substr($0,216,20) print v1 "|" v2 "|" v3 } The input file being processed... (2 Replies)
Discussion started by: vidyak
2 Replies

7. UNIX for Dummies Questions & Answers

make script treat * as a regular character

I need a script that reads the out put of a command (softwareupdate --list) and will tally up the number of asterisks in the output and tell me how many there were. How do I go about getting my script to treat asterisks as a regular character and not a wildcard or some other operator? (8 Replies)
Discussion started by: glev2005
8 Replies

8. Shell Programming and Scripting

Paste the files contents differently

Hello , I want to segregate by file contents in a much simpler format ie input file Wednesday May 16 11:59:44 IST 2007 90376 44136 process1 pid1 90200 43208 process1 pid2 90200 43208 process1 pid3 90200 43208 process1 pid4 Wednesday May 16 12:00:45 IST 2007 90376 44136 process1 pid1... (1 Reply)
Discussion started by: er_aparna
1 Replies

9. UNIX for Advanced & Expert Users

Script behaving differently in Crontab..

I posted this in Shell scripting... maybe I'll try it in this forum.. ***************** I wrote a script to stop a process,truncate its log files and re-start the process... We are using Progress Software in Unix ( Sun Sparc) When ever I start this progress program , it should kick off a... (1 Reply)
Discussion started by: newtoxinu
1 Replies

10. Shell Programming and Scripting

awk - treat multiple delimiters as one

Is there anyway to get awk to treat multiple delimiters as one? Particularly spaces... (6 Replies)
Discussion started by: peter.herlihy
6 Replies
Login or Register to Ask a Question