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 06-26-2009, 02:19 PM   #1
ZimMonkey
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Rep: Reputation: 0
Limiting awk to desired response


I'm new to programming, and was really happy when I found out how to use an outside variable in an awk command. Now I can't figure out how to limit the responses to what I want Here's what I have:

Code:
#!/bin/bash

echo "Enter Number"
read Numb

awk '$1 ~ /'$Numb'/ {print} fromfile
The fromfile has anywhere from 1 - 100 numbered lines in $1. If Numb = 1, then it prints records for 1, and 10 - 19 as well.

How do you limit it to just the number selected?

Thanks

Zim
 
Old 06-26-2009, 02:31 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
You just need to modify your expression, a la:
Code:
$ foo=1
$ awk '$1 ~ /\<'${foo}'\>/ { print }' fromfile 
1
You might search the 'net for some regular expressions tutorials (esp. in the context of grep and awk).
 
Old 06-26-2009, 02:51 PM   #3
ZimMonkey
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for your reply. I tried it only to get

foo: command not found. Actually in my case it was Numb: command not found

I'll check my spelling.
 
Old 06-26-2009, 03:10 PM   #4
ZimMonkey
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Anomie,

Thanks for your help. I figured out what I was doing wrong. I included the () in front of, and behind foo. I took your advice and did a Google search on regular expression tutorial awk. I know what I'll be doing this weekend.

Thanks for your help

Zim
 
Old 06-28-2009, 08:19 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
This is good for awk: http://www.grymoire.com/Unix/Awk.html
 
Old 06-28-2009, 09:27 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ZimMonkey View Post
I'm new to programming, and was really happy when I found out how to use an outside variable in an awk command. Now I can't figure out how to limit the responses to what I want Here's what I have:

Code:
#!/bin/bash

echo "Enter Number"
read Numb

awk '$1 ~ /'$Numb'/ {print} fromfile
The fromfile has anywhere from 1 - 100 numbered lines in $1. If Numb = 1, then it prints records for 1, and 10 - 19 as well.

How do you limit it to just the number selected?

Thanks

Zim
you can do everything in awk.
Code:
awk 'BEGIN{
    printf "Enter a number: "
    getline number <"-"    
}
$1 == number{
  print
}' file
see my sig for learning awk.
 
  


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
LXer: A Response to HIMSS "Call to Action" on the American Recovery and Response Act LXer Syndicated Linux News 0 02-13-2009 01:40 AM
Program : Not getting the desired output thelink123 Linux - Newbie 4 11-17-2008 01:59 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
how to set the right charset (or desired) amimusa Debian 1 04-05-2007 03:07 AM
SSH and desired interface stakhous Linux - Networking 1 02-05-2005 01:10 AM


All times are GMT -5. The time now is 05:26 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