Extracting a line in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a line in a text file
# 1  
Old 10-10-2008
Extracting a line in a text file

If my file looks like this….
10
20
30
and I want to take each line individually and put it in a variable so it can be read
later in it's on individual test statement, how can I do that? I guess what I'm asking is how can I extract each line individually.

Thanks

Last edited by terryporter51; 10-10-2008 at 06:31 PM..
# 2  
Old 10-10-2008
Hopefully this gives you an idea.

Code:
cat yourfile.txt | while read line ; do 
   if [ $line == 30 ] ;then 
     echo I hit 30
   fi
done

# 3  
Old 10-10-2008
you can extract each line by using simple while
Code:
while read line
do
echo "$line"
##do any operation on that line
done < filename

# 4  
Old 10-11-2008
We're both giving you essentially the same answer. When you mean "each line individually", do you mean you want each in its own variable? Bash3 supports arrays, so you could do it that way. If your file contains a fixed number of lines, say 3, you could do this:
Code:
cat yourfile.txt | {
  read line1
  read line2
  read line3

  # do stuff with line1 or line2 or line3
}

# 5  
Old 10-13-2008
I'm still having trouble stripping each line out so I can put it in it's on variable or text file.
i.e. I want 10 to be in a text file or set to a variable alone
20 set to be in a text file alone or in it's on variable and
30 set to be in a text file or set to it's on variable. I really want the position of the number because the number will change depending on the number of full tapes. Sorry guy's if this sound elementary all I want to do is grab which ever line I want 1-3 on call to my discretion.

Thx
# 6  
Old 10-13-2008
Something like that:
Code:
eval $(awk '{print "line"NR"="$0}' file)
echo $line1
....

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and copying text from one file to another

Helooo, So I have a .fasta file (a text file with sequence data) which looks like this, with just over 3 million lines of data. >TCONS_00000001 gene=XLOC_000001 AATTGTGGTGAAATGACTTCTGTTAACGGAGACATCGATGATTGTTGTTACTATTTGTTCTCAGGATTCA... (8 Replies)
Discussion started by: 4galaxy7
8 Replies

2. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

3. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

help extracting text from file

Hello I have a large file with lines beginning with 552, 553, 554, below is a small sample, I need to extract the data you can see below highlighted in bold from this file on the same location on every line and output it to a new file. Thank you in advance for any help 55201KL... (2 Replies)
Discussion started by: firefox2k2
2 Replies

5. UNIX for Dummies Questions & Answers

Extracting the last column of a text file

I would like to extract the last column of a text file but different rows of the text file have different numbers of columns. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

extracting part of a text file

Hi guys So I have a very large log file where each event is logged along with the time that it occurred. So for e.g. The contents of the file look like: ... 12:00:07 event 0 happened. 12:01:01 event 1 happened. 12:01:05 event 2 happened. 12:01:30 event 3 happened. 12:02:01 event 4... (10 Replies)
Discussion started by: alinaqvi90
10 Replies

7. Shell Programming and Scripting

Extracting specific characters from a text file

I'm extremely new to scripting and linux in general, so please bear with me. The class I'm taking gives virtually no instruction at all, and so I'm trying to learn everything off the web. Anyway, I'm trying to extract characters that follow after a specific pattern ( '<B><FONT FACE="Arial">' ) but... (3 Replies)
Discussion started by: livos23
3 Replies

8. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

9. Shell Programming and Scripting

Extracting specific text from a file

Dear All, I have to extract a a few lines from a log file and I know the starting String and end string(WHich is same ). Is there any simplere way using sed - awk. e.g. from the following file -------------------------------------- Some text Date: 21 Oct 2008 Text to be extracted... (8 Replies)
Discussion started by: rahulkav
8 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question