Extract a specific line from a stream


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a specific line from a stream
# 1  
Old 01-08-2011
Error Extract a specific line from a stream

Hello,

I'm trying to code a bash script and I was wondering how to extract a specific line from a stream.

E.g.
My file "file" contains three lines and i'd like to find a function f which returns after execution a specific line like the second line, which would be :

Code:
f(file, 2) = Second line of the file (String form)



I was trying to make so with grep, less, cat, more, ... but I can't figure oout any way to make it happens.

Ok to be more precise, I wanna print the name of the n-file in the "ls -1" stream. For example if I type "ls -1", here's the output:

Code:
file1
file2
file3



My script prompts the user to specify the name of the file n he wants to read.
If the user type 2, the new output would be "file2" (string form).
The final line instruction would be something like :

Code:
f( $(ls -1) , n) = the n-line of the output of "ls -1"


Ok so much lines for a little trouble.
I hope someone knows a solution for it, and since i'm french and i'm not sure my problem is well-posted feel free to ask other questions.

Thanks Smilie
# 2  
Old 01-08-2011
Try this bash code:
Code:
f()
{
  local i
  while IFS= read -r line
  do 
    if [[ $((i+=1)) -eq $2 ]]; then  
      printf "%s\n" "$line"
      break
    fi
  done < "$1"
}

Call it like this:

Code:
f  file 2

or
Code:
f <(ls -1) n # = the n-line of the output of "ls -1"


Last edited by Scrutinizer; 01-08-2011 at 01:46 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-08-2011
Nope sorry that doesn't seem to work or perhaps i'm doing it the wrong way.
does it not already exist a unix command ?

ok i've found the line function but this function seems to return the first line of any stream.
Besides maybe i'm using it badly.

anyway thanks for your help, but when I type "f <(ls -1) 2" nothing happening, is that working on your system?
# 4  
Old 01-08-2011
Hi, I thought you were looking for a bash function. You need to run the function code first in a bash shell and then call it, or put it in a bash script.
Sure there are commands that can do this:
Code:
sed -n 2p file

for example, or more efficiently:
Code:
sed '2q;d' file


Last edited by Scrutinizer; 01-08-2011 at 01:44 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-08-2011
Ok it works perfectly, what a pleasure to end up with what we looking for.

Thanks a lot having helped me ! Smilie

Regards, Oddant

Last edited by Oddant; 01-08-2011 at 02:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

2. Shell Programming and Scripting

Extract & Manipulate continous data stream-- tcpdump

Hello; I have this rather tricky problem to solve --(to me, anyways) .. I am processing the following one liner with tcpdump.. tcpdump -i T3501 -A ether host 00:1e:49:29:fc:c9 or ether host 00:1b:2b:86:ec:1b or ether host 00:21:1c:98:a4:08 and net 149.83.6.0/24 | grep --line-buffered -B... (5 Replies)
Discussion started by: delphys
5 Replies

3. UNIX for Dummies Questions & Answers

Extract specific contents from each line

Hi all, Happy new year! Here I have a problem with extract specific information from each line in unix: My file is the dbSNP flat file, take two SNPs for examples: REFSNP-DOCSUM-SET (FULL-DUMP) CREATED ON: 2012-06-08 10:50 rs782 | human | 9606 | snp | genotype=NO |... (6 Replies)
Discussion started by: luoruicd
6 Replies

4. Shell Programming and Scripting

sed or awk, cut, to extract specific data from line

Hi guys, I have been trying to do this, but... no luck so maybe you can help me. I have a line like this: Total Handled, Received, on queue Input Mgs: 140 / 14 => 0 I need to, get the number after the / until the =, to get only 14 . Any help is greatly appreciated. Thanks, (4 Replies)
Discussion started by: ocramas
4 Replies

5. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

Extract character between specific line numbers

Hi guys, I have txt file and I would need to extract all the contents between specific line numbers. Line 1: apple Line 2: orange Line 3: mango Line 4: grapes Line 5: pine apple I need to extract the content between line 2 and 4, including the contents of Line 2 and 4 so the ouput... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

8. Shell Programming and Scripting

extract specific line if the search pattern is found

Hi, I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses. My Source file <Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT ... (3 Replies)
Discussion started by: Sekar1
3 Replies

9. Shell Programming and Scripting

[Video stream] network stream recording with mplayer

Hi I used this command: mplayer http://host/axis-cgi/mjpg/video.cgi -user root -passwd root \ -cache 1024 -fps 25.0 -nosound -vc ffh264 \ -demuxer 3 -dumpstream -dumpfile output.avi It's ok but... Video Playing is very fast! Why? Is it a synch problem? What parameter I have to use for... (1 Reply)
Discussion started by: takeo.kikuta
1 Replies

10. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies
Login or Register to Ask a Question