grep command to retrieve one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep command to retrieve one file
# 1  
Old 05-01-2009
grep command to retrieve one file

The Sed/Grep command is really confusing me. I know I'm missing something that should be really easy to fix. My program displays multiple names after I ask it to display only one, How do I get it to do only one??

it looks like this:

Please enter a name to display?
>> John

(A list then appears)
1. John Doe
2. John Smith
3. John James
Please enter the record that you would like to display on separate page?
>> 3

And then John James should appear on a separate page like this
3.
John James
1234 Main Street
City, State, Zipcode
Telephone Number

However, my program displays every John
_________
my code looks like this:

echo "Please enter a name to display \c"
read name

#Displays all of the names with their record number
grep -in $name phonebook

#Enter the record number to display on separate page
echo "Enter the record number of the name you want to display \c"
read number

record=`grep -i $number phonebook` (I'm Pretty sure this is the error)

clear

grep -in "$name" phonebook | sed -n -e 's/^/\
/' -e 's/:/\
/gp'
# 2  
Old 05-01-2009
Can you show us what the format of the phonebook file is?
# 3  
Old 05-01-2009
there's nothing in the phone book file except the names of the people.

1. John Doe:1234 Main Street: City:State
2. John Smith: Address:City:State
3. John Something: Address: City: State
# 4  
Old 05-01-2009
phonebook.txt
Code:
1. John Doe:1234 Main Street: City:State:ZIP
2. John Smith: Address:City:State:ZIP
3. John Something: Address: City: State:ZIP

nawk -v idx=1 -f toe.awk phonebook
OR
nawk -v idx=3 -f toe.awk phonebook

toe.awk:
Code:
BEGIN {
  FS=":"
  OFS=ORS
  SEP=","
  idxI="^" idx "."

}

$0 ~ idxI {
    name=substr($1, index($1, " ")+1)
    print idx, name, $2, $3 SEP $4 SEP $5
}

# 5  
Old 05-01-2009
I'm not familiar with awk at all ! Smilie

is there a simplified way such as a grep/sed select statement that can work??
# 6  
Old 05-01-2009
Code:
echo -e "Please enter a name to display \c"
read name

#Displays all of the names with their record number
grep -in $name phonebook.txt

#Enter the record number to display on separate page
echo -e "Enter the record number of the name you want to display \c"
read number

record=`grep "^$number" phonebook.txt`

clear

echo $record | sed -n -e 's/^/\
/' -e 's/:/\
/gp'

HTH,
tyler_durden
# 7  
Old 05-02-2009
this will print the first matching pattern of a file ..

sed -n '/John/{p;q;}' test1.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

3. Homework & Coursework Questions

awk command to retrieve record 23 and 89 from UNIX file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file?... (6 Replies)
Discussion started by: rakeshp
6 Replies

4. UNIX for Beginners Questions & Answers

awk command to retrieve record 23 and 89 from UNIX file

Hi Everyone, I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file? Please let me know what is the awk command for this? Regards Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

5. Shell Programming and Scripting

Retrieve logs generated in last 10 mins from a log file using 'grep' command

HI All, I have a log file where the logs will be in the format as given below: 2011-05-25 02:32:51 INFO PROCESS STARTING 2011-05-25 02:32:52 INFO PROCESS STARTED . . . I want to retrieve only the logs which are less than 5 mins older than current time using grep... (3 Replies)
Discussion started by: rvhg16
3 Replies

6. Shell Programming and Scripting

How to retrieve command line args one by on.

Hi, I have to store all the command line arguments into an array. I have the following code. ********************** #! /bin/sh set -A arr_no_updates i=1 while do arr_no_updates=$($i) echo ${arr_no_updates} i=$(($i+1)) done**************** (1 Reply)
Discussion started by: little_wonder
1 Replies

7. UNIX for Dummies Questions & Answers

retrieve lines using sed, grep or awk

Hi, I'm looking for a command to retrieve a block of lines using sed or grep, probably awk if that can do the job. In below example, By searching for words "Third line2" i'm expecting to retrieve the full block starting with 'BEGIN' and ending with 'END' of the search. Example: ... (3 Replies)
Discussion started by: learning_linux
3 Replies

8. Shell Programming and Scripting

how to retrieve only the Use% value from df command

when I do a df -k for a particular mount i get the result like this Filesystem 1K-blocks Used Available Use% Mounted on /dev/ 4128448 3527496 391240 91% / I need to extract the value 91 from this and use it in my script in an if condition. How will i do it Please advice. (8 Replies)
Discussion started by: codeman007
8 Replies

9. UNIX for Dummies Questions & Answers

retrieve text after grep

I am trying to search for a pattern in a file containing xml - When I match the search I want to retrieve all the text within the xml brackets.. Whats the best way to read in data between xml tags in a shell script? ie.. xml returned which I have in a file now is something like below:... (2 Replies)
Discussion started by: frustrated1
2 Replies

10. UNIX for Dummies Questions & Answers

How to retrieve the typed command

For examples, I have typed 4 commands in the command prompt: ls -la rm -rf /home/user1 du -k /home find . -name "abc.out" -print And now I want to retrieve the command which begin with letter "r" (i.e. rm -rf /home/user1), what can I do? (5 Replies)
Discussion started by: laum
5 Replies
Login or Register to Ask a Question