LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-03-2011, 12:42 AM   #1
ViciousBox
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
Awk : Trying to count lines with user input


Hello,
I am trying to write a script that will use awk to take an user input field and check and count the lines that contain the entry:

ie: ./test.sh search_text file

Code:

num=$(awk '/$1/ {++x} END {print x+0}' $2)
if [ $num -eq 0 ]; then
echo $num" organisms named "$1" found! Try changing syntax or organism does not exist."
else
awk '/$1/ {print $0}' $2 > $1.txt
echo "The number of organisms found is" $num"! A new file named "$1".txt has been created!"
fi

If I change $1 to what I am actually looking for it works
Any ideas?

Thank so much!
 
Old 10-03-2011, 01:00 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
How about just:
Code:
grep -c search_text file
 
1 members found this post helpful.
Old 10-03-2011, 01:01 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
Any ideas?
Plenty

1. Why use bash at all? Just write the whole script in awk. You simply change your interpreter:
Code:
#!/bin/bash

now becomes

#!/usr/bin/awk -f
Obviously needs to point to where ever awk is installed on your machine

2. Both bash and awk have a $1 variable and they are not related

3. Placing a field inside the regex will never work :- /$1/ ... remember that $ has a special meaning and no escaping it
will not help.

4. 'print x+0' .... why?? what was wrong with 'print x'? 'x' is not a string
 
1 members found this post helpful.
Old 10-03-2011, 01:10 AM   #4
ViciousBox
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the suggestions. I am still very new at this!

I like the grep command. But we are just learning about bash so our script has to use awk.
I was fooling around with my code:
by adding

organism="$1"

and changing the line
num=$(awk '/$1/ {++x} END {print x+0}' $2)
to num=$(awk '/'$organism'/ {++x} END {print x+0}' $2)
it works

Thanks for suggestions and helping me clean up my code!
The reason for the x+0 is that if x=0 then it leaves x blank- its strange (even if i attach num=0 at the top of my code)

Last edited by ViciousBox; 10-03-2011 at 01:21 AM. Reason: Found a bug in code
 
Old 10-03-2011, 02:55 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Again you need to remember that any variable in bash is not related to any variable in awk (unless you tell it somehow).

Even though you might set num=0, once the awk script runs, if the value of x has not been incremented it will be blank
and this is then passed back to num hence the original value of 0 is now gone.

The other reason for suggestion to do all in awk is to not then require you to run the same awk query twice.

May I suggest you have a look at the '-v' option for awk. Your current solution to alter the quoting works fine in this case but once you start to need more variables translated into the awk script this will become unmanageable.

Lastly, you may also wish to look at the BEGIN clause for setting variables.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read multiple lines from user input in c archanac07 Linux - Software 1 02-26-2010 06:15 AM
AWK count kj6loh Programming 1 09-07-2009 10:50 PM
script to count lines kalimat Programming 16 12-15-2008 05:09 AM
user input howto using awk cmontr Programming 11 09-29-2007 08:47 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM


All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration