LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-14-2008, 08:12 AM   #1
Jayfrin
LQ Newbie
 
Registered: Apr 2008
Posts: 19

Rep: Reputation: 0
Finding things on lines of a txt file


Ok this may not make sense but I have a program which uses a while loop to search through a text file and I have a counter that records the line which it is on so what I need is that when it finds the first piece of desired data it then looks to the like right under that (counter number) and puts that in a variable.

Code:
data#1
data#2<this is the one i want to get when 
                 it gets the value of #1
data #3
data #4
Note : these lines arn't nessecarly line one & two the ycould be anywhere in the text file.
 
Old 05-14-2008, 09:08 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If you grep for text you can -A n next lines with it and how you get the numerical value into a variable from the counter line depends on if its fixed field or not if its not then you could test each element in the line while read line do for item in line do test -z ${item//[0-9]/} && echo $item done done prolly is an easier way btw notice how punctuation makes stuff easier to read at least i think so.

Last edited by unSpawn; 05-14-2008 at 09:09 AM.
 
Old 05-15-2008, 07:56 AM   #3
Jayfrin
LQ Newbie
 
Registered: Apr 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I'm primitive and confused
 
Old 05-15-2008, 08:13 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
As unSpawn said, there is a command line option to the grep program which will help you do this.

You didn't state what programming language you are using, and you didn't post what code you have so far, so it's very hard to give you more precise help.

If you are writing a shell script, you don't need to read through the file with your own loop - it is more normal (and usually more efficient) to use grep or one of the other ubiquitous text processing programs (like sed, awk, tr, cut, paste, fold, head, tail etc). What if there is more than one line in the input file which has the search pattern in it? Do you want to do something for all the following lines, or just the first one? Perhaps this will help:
Code:
$ cat data
data#1 First line
data#2 Second line
data#3 Third line
data#4 Fourth line
$ grep -A 1 -m 1 "data#1" data |tail -1
data#2 Second line
You can assign the output of one program to a variable like this:
Code:
myvariablename=$(some command here)
 
  


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
Finding duplicate lines in a file MikeyCarter Linux - Software 3 10-05-2008 05:28 PM
Borland C++ Saving things to txt files Jayfrin Programming 4 04-15-2008 01:31 PM
Remove 38 lines after finding match 0.o Programming 12 03-28-2008 02:15 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Finding lines in file1,but not in file 2 subu_s Programming 2 12-14-2004 09:56 AM

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

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