LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-13-2014, 02:44 AM   #1
Vipul_sharma11
LQ Newbie
 
Registered: Oct 2014
Posts: 2
Blog Entries: 1

Rep: Reputation: Disabled
How to read data from a text file?


Hi All,

I have a text file with Name and Address in it.

<Name>|<Address>

Amit|32, Rajpura Road, Dehradun
Sumit|123, Shahbad, Ambala

and so on....

I want to fetch the address when I enter the name.

Can anyone help me please?
 
Old 10-13-2014, 02:46 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
just try the grep command, see the man page or look for tutorials and/or examples
 
Old 10-13-2014, 02:56 AM   #3
715509387
LQ Newbie
 
Registered: Oct 2014
Posts: 1

Rep: Reputation: Disabled
try it
Code:
[root@svn252 ~]# cat URFILE 
Amit|32, Rajpura Road, Dehradun
Sumit|123, Shahbad, Ambala
[root@svn252 ~]# awk -F'|' '$1=="Sumit"{print $2}' URFILE 
123, Shahbad, Ambala
[root@svn252 ~]# awk -F'|' '$1=="Amit"{print $2}' URFILE 
32, Rajpura Road, Dehradun
[root@svn252 ~]#

Last edited by 715509387; 10-13-2014 at 03:02 AM.
 
Old 10-13-2014, 02:57 AM   #4
Vipul_sharma11
LQ Newbie
 
Registered: Oct 2014
Posts: 2
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Thanks

hi,

thanks for the suggestion.

i want to read input from user and then take address from file.

can you please tell me how to read the file line by line?
 
Old 10-13-2014, 03:15 AM   #5
nisagnel
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2
Smile

Hi Vipul,

You can do it using bash script like below.
Assume that you have file "details.txt" which has name and address.

#!/bin/bash

## read name from the user
read NAME

## now grep the name from the details.txt
cat details.txt | grep $NAME


..hope this will help you.
 
Old 10-13-2014, 03:58 AM   #6
rupal.chawla
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Rep: Reputation: Disabled
You can try using 'grep' command

Eg. your details are in file 'text.txt'

type in command:
cat text.txt| grep "name"

Output:
Name Address



Happy Learning

---------- Post added 10-13-14 at 02:58 AM ----------

You can try using 'grep' command

Eg. your details are in file 'text.txt'

type in command:
cat text.txt| grep "name"

Output:
Name Address



Happy Learning
 
Old 10-13-2014, 04:50 AM   #7
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
Since you didnt mentioned what you tried, i will take it as after a small guidance you would try it yourself by looking into some examples:

So i would prefer something like :
Code:
#declare filename as variable $filename
#read a variable say $inputname
#use grep to fetch users all details, instead of using cat. Avoiding a useless use of commands
grep $inputname $filename
#you can use awk, cut and further more commandsdepending upon your requirement, see man pages and examples as 'pan64' told.
#if there is any iteration needed you can use while loop to do the due
while read $variable; do something_with_variable;done< $filename
let us know if you ty and fail with what you tried, we can help suggesting better option.

Last edited by SAbhi; 10-13-2014 at 04:53 AM.
 
  


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
how to read data from text file and output into a table? j123 Linux - Newbie 26 04-15-2010 01:58 AM
Shell script for read user data with emptyLines in a text file and filter them srimal Linux - Newbie 7 11-01-2009 05:37 AM
Shell script to read lines in a text file and filter user data srimal Linux - Newbie 5 10-21-2009 08:41 AM
How to Read data from a Text file in this case ( C++ Programming) smasher Programming 8 12-29-2008 08:55 PM
read the data from the text file which is located in remote machin to oracle databse marthesh Linux - Newbie 1 07-07-2008 08:05 PM


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