Faster way to use this awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Faster way to use this awk command
# 1  
Old 05-23-2012
Faster way to use this awk command

Code:
awk "/May 23, 2012 /,0" /var/tmp/datafile

the above command pulls out information in the datafile. the information it pulls is from the date specified to the end of the file.

now, how can i make this faster if the datafile is huge? even if it wasn't huge, i feel there's a better/faster way to get what i want.
# 2  
Old 05-24-2012
Short command lines don't equal speed necessarily.

Your code does what all code has to do, read each line. Doing a regular expression search is extra overhead. The only speed up possible is to turn off regexp search after the first match. It has to read each line regardless of all else. See what you can do with that logic: make it skip over the regexp after the first find and just print.
# 3  
Old 05-24-2012
Quote:
Originally Posted by jim mcnamara
Short command lines don't equal speed necessarily.

Your code does what all code has to do, read each line. Doing a regular expression search is extra overhead. The only speed up possible is to turn off regexp search after the first match. It has to read each line regardless of all else. See what you can do with that logic: make it skip over the regexp after the first find and just print.


i need to grep for certain strings between the point specified to the end of the file. and i need to know the amount of lines containing those strings.

thats why im concerned about speed.
# 4  
Old 05-24-2012
What's your system? What's your shell?

If you're on Linux I'd be tempted to use ( grep -m 1 "myregex" ; cat ) < inputfile > outputfile, using GNU grep just to get to the right place in the file and cat-ing the rest, which should be about as fast as anything can get.

If that's not fast enough, the bottleneck may not be your program.
These 2 Users Gave Thanks to Corona688 For This Post:
# 5  
Old 05-24-2012
shell is bash, os is linux and sunos.

your command appears to do the trick. thank you so much. i think awk is one of the top 5 best language out there. just sad it couldn't do what u just did with this grep command.
# 6  
Old 05-24-2012
awk 'index($0,"May 23, 2012 "),0' would do a fixed string search without using regex.

if you need to search for more terms *after* that, GNU is rather fast I hear, and -F might speed it up (fixed string search). or try using awk for it too. use time and see how different varients weigh in.

Code:
awk 'index($0,"May 23, 2012 "),0{if (index($0,"ERROR") {c++;print}} END {print "Total errors after date: " c}' file

Code:
awk '!start && index($0,"May 23, 2012 ") {start=1} !start {next}
index($0,"ERROR"){c++;print} END {print "Total errors after date: " c}' file

internally awk probably does ranges about the same as that though..
This User Gave Thanks to neutronscott For This Post:
# 7  
Old 05-24-2012
Quote:
Originally Posted by SkySmart
shell is bash, os is linux and sunos.

your command appears to do the trick.
Not under SunOS, it won't, unless you install GNU grep. -m is a GNU extension.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make awk command faster for large amount of data?

I have nginx web server logs with all requests that were made and I'm filtering them by date and time. Each line has the following structure: 127.0.0.1 - xyz.com GET 123.ts HTTP/1.1 (200) 0.000 s 3182 CoreMedia/1.0.0.15F79 (iPhone; U; CPU OS 11_4 like Mac OS X; pt_br) These text files are... (21 Replies)
Discussion started by: brenoasrm
21 Replies

2. Shell Programming and Scripting

How to make awk command faster?

I have the below command which is referring a large file and it is taking 3 hours to run. Can something be done to make this command faster. awk -F ',' '{OFS=","}{ if ($13 == "9999") print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12 }' ${NLAP_TEMP}/hist1.out|sort -T ${NLAP_TEMP} |uniq>... (13 Replies)
Discussion started by: Peu Mukherjee
13 Replies

3. Shell Programming and Scripting

awk changes to make it faster

I have script like below, who is picking number from one file and and searching in another file, and printing output. Bu is is very slow to be run on huge file.can we modify it with awk #! /bin/ksh while read line1 do echo "$line1" a=`echo $line1` if then echo "$num" cat file1|nawk... (6 Replies)
Discussion started by: mirwasim
6 Replies

4. Shell Programming and Scripting

Making a faster alternative to a slow awk command

Hi, I have a large number of input files with two columns of numbers. For example: 83 1453 99 3255 99 8482 99 7372 83 175 I only wish to retain lines where the numbers fullfil two requirements. E.g: =83 1000<=<=2000 To do this I use the following... (10 Replies)
Discussion started by: s052866
10 Replies

5. UNIX for Dummies Questions & Answers

A faster equivalent for this sed command

Hello guys, I'm cleaning out big XML files (we're talking about 1GB at least), most of them contain words written in a non-latin alphabet. The command I'm using is so slow it's not even funny: cat $1 | sed -e :a -e 's/&lt;*&gt;//g;/&lt;/N;//ba;s/</ /g;s/>/... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

Multi thread awk command for faster performance

Hi, I have a script below for extracting xml from a file. for i in *.txt do echo $i awk '/<.*/ , /.*<\/.*>/' "$i" | tr -d '\n' echo -ne '\n' done . I read about using multi threading to speed up the script. I do not know much about it but read it on this forum. Is it a... (21 Replies)
Discussion started by: chetan.c
21 Replies

7. UNIX for Dummies Questions & Answers

Which command will be faster? y?

i)wc -c/etc/passwd|awk'{print $1}' ii)ls -al/etc/passwd|awk'{print $5}' (4 Replies)
Discussion started by: karthi_g
4 Replies

8. Shell Programming and Scripting

command faster in crontab..

Hi all you enlightened unix people, I've been trying to execute a perl script that contains the following line within backticks: `grep -f patternfile.txt otherfile.txt`;It takes normally 2 minutes to execute this command from the bash shell by hand. I noticed that when i run this command... (2 Replies)
Discussion started by: silverlocket
2 Replies

9. Shell Programming and Scripting

awk help to make my work faster

hii everyone , i have a file in which i have line numbers.. file name is file1.txt aa bb cc "12" qw xx yy zz "23" we bb qw we "123249" jh here 12,23,123249. is the line number now according to this line numbers we have to print lines from other file named... (11 Replies)
Discussion started by: kumar_amit
11 Replies

10. Shell Programming and Scripting

Which is faster AWK or CUT

If I just wanted to get andred08 from the following ldap dn would I be best to use AWK or CUT? uid=andred08,ou=People,o=example,dc=com It doesn't make a difference if it's just one ldap search I am getting it from but when there's a couple of hundred people in the group that retruns all... (10 Replies)
Discussion started by: dopple
10 Replies
Login or Register to Ask a Question