LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2010, 10:02 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Using awk for displaying of row


Code:
Database
rina lives:fatin:20:20:20
rina:fatin:20:20:20
i am having a small problem in extracting out the information from the database. For example, if i were to input a book titled rina into my search , i would only want it to display the row which has the title rina, like this

Code:
rina:fatin:20:20:20
but when i use a coding like this :
Code:
grep -i "$Title" fruit| awk -F":" '{ print $0}'`
its displays both titles and their information

Code:
rina lives:fatin:20:20:20
rina:fatin:20:20:20
for some reason, it will read both rows as both contains the word "rina". does anybody know how i can modify my statement to solve this problem?
 
Old 01-24-2010, 10:15 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Your grep statement returns any line containing the value of "title"--in this case I assume it was "rina". So grep is performing as expected.

awk '{print $0}' seems to mean "print the whole line"

To get the line containing ONLY rina in the first field, I would use sed addressing---eg:

sed -n '/rina:/p' filename
 
Old 01-24-2010, 10:27 AM   #3
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
does sed recognize the -f":" also?
 
Old 01-24-2010, 10:30 AM   #4
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
hmmm, sed dosnt seem to read the FS command like awk right? so how do i go about solving the problem if all my rows are defined in that way?
 
Old 01-24-2010, 10:35 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Don't try to apply AWK options to SED---read the man pages or one of the various tutorials.

In my SED example, the address includes the ":", thereby accomplishing the same purpose as specifying the field separator in AWK.

Really good tutorials here:
http://www.grymoire.com/Unix/
 
Old 01-24-2010, 10:49 AM   #6
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
okay, just to check, i tried using awk for this problem. so my coding was like this .

Code:
Title=
echo "read this:"
read Title
awk '/'$Title'/' fruit

but it also displays both strings. why does awk have the same problem also?
 
Old 01-24-2010, 12:27 PM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Code:
awk -F":" ' $1 == "rina" ' fruit
 
Old 01-24-2010, 12:37 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by gregarion View Post
okay, just to check, i tried using awk for this problem. so my coding was like this .

Code:
Title=
echo "read this:"
read Title
awk '/'$Title'/' fruit

but it also displays both strings. why does awk have the same problem also?
I believe you are missing a key concept. AWK displays both line because they both contain the keyword. That is why I used the ":" in my SED example.

With any regular expression (which includes keywords in plain english), the match is made if the pattern is found---it does not matter if there is also something else there.
 
Old 01-24-2010, 03:40 PM   #9
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
The important thing is to work out exactly what you are after.
  • Do you want to search for Titles containing the input?
  • Do you want to search for Titles exactly matching the input?
  • Do you want to search anywhere in the file for the input?

To search for Titles containing the input:
Code:
#!/bin/bash

Title=
read -p "Enter title: " Title

awk -F":" ' $1 ~ TITLE' TITLE="Title" fruit
To search for Titles exactly matching the input:
Code:
#!/bin/bash

Title=
read -p "Enter title: " Title

awk -F":" ' $1 == TITLE' TITLE="Title" fruit
To search anywhere in the file for the input:
Code:
#!/bin/bash

Title=
read -p "Enter title: " Title

awk -F":" ' $0 ~ TITLE' TITLE="Title" fruit
I don't believe line 3 is really required, but put it in as it was in your original code.

Last edited by Disillusionist; 01-25-2010 at 12:01 AM. Reason: Added quotes to cater for spaces in search criteria
 
  


Reply



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
[SOLVED] Displaying information using awk gregarion Programming 4 01-14-2010 03:19 AM
[SOLVED] awk code help for displaying file contents ashok.g Programming 13 12-14-2009 12:55 AM
Row manipulation with awk SHIFTA Linux - Newbie 1 11-05-2009 10:37 PM
row operation usin awk RudraB Programming 3 03-02-2008 04:42 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 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
Open Source Consulting | Domain Registration