LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-06-2008, 08:56 PM   #1
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
read a line from text file and extracting the details needed


Hi ,
I need to read line from a txt file.
then if the line has ID, then get the value of ID and save it in a variable/array
else if the line has State , then get the value of state and store it in a variable/array

how do i go about it?

Im able to read the lines. but when i grep each line to find if there id word ID or State, im failing and im unable to assign the value to a variable..

Please anyone help me...
im a newbie...
 
Old 06-06-2008, 09:03 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Code:
grep ID filename.ext | while read line
do
  field_1=$(echo "$line" | awk '{print $1}')
  field_2=$(echo "$line" | awk '{print $2}')
  field_3=$(echo "$line" | awk '{print $3}')
  field_4=$(echo "$line" | awk '{print $4}')
# <---do whatever--->
done

Last edited by i92guboj; 06-06-2008 at 09:05 PM.
 
Old 06-06-2008, 09:19 PM   #3
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
The result for field_1:
ID
:
0

now how do i use ID=0 for running a text? i need to extract the value for ID from field_1.. how do i do it?

Thank you
 
Old 06-06-2008, 09:23 PM   #4
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
cant i just store field_1 = 0 , field_2=1 instead of field_1 = ID : 1 ??how do i accomplish that?
 
Old 06-06-2008, 09:51 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Can you paste some sample input and the desired results?



Cheers,
Tink
 
Old 06-06-2008, 11:35 PM   #6
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
Quote:
Originally Posted by pdklinux79 View Post
cant i just store field_1 = 0 , field_2=1 instead of field_1 = ID : 1 ??how do i accomplish that?

If ID only appears once in the file, you can use this

[QUOTE]
VALUEOFID=$(grep "ID=" datafile | read ID FIELD2 FIELD3 | awk -F= '{print $2}')[/QOUTE]

If it appears multple times, just feed the grep into
a while read ID field2 field3 line and

grep "ID=" datafile | while read ID FIELD2 FIELD3
do
VALUEOFID=$(echo "$ID" | awk -F= '{print $2}')


### What your going to do with ID

done
 
Old 06-06-2008, 11:41 PM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
As Tinkster already suggested, please, post:

1.- a few lines, with sample contents of the file you are going to parse
2.- a very detailed example of a line, the value of fields that you expect, the value of ID (which I don't know what's supposed to be) and the expected results. Be as clear as you can, please.
 
  


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 line by line form text file in java. spank Programming 1 10-18-2006 03:46 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 03:10 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 06:28 AM
[C & Linux] Read line from text-file Y_Haarman Programming 5 11-08-2002 05:44 PM


All times are GMT -5. The time now is 03:25 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration