grep a specific line from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a specific line from a file
# 1  
Old 11-24-2009
grep a specific line from a file

Is there a way to grep only one line from a file listing name of files?
If I use head -1 it's ok(the first line only)
If I use head -2 it's not good because I have already checked the first line.

I'd like something like this:
Code:
 
while test $i -le $max 
do
  grep "$3" `head -$i temp.txt`  > temp2.txt
  cat temp2.txt
  i=`expr $i + 1`
done

but the head command doesn't give only the i line as I would like.
Thank you in advance.
# 2  
Old 11-24-2009
Have you tried the -m flag?


Quote:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines
are output, grep ensures that the standard input is positioned to just after the last matching line before exiting,
regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep
stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep
does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting
NUM non-matching lines.
# 3  
Old 11-24-2009
awk 'NR=='$i temp.txt
# 4  
Old 11-24-2009
Thank you all,but I don't understand 'NR=='
Quote:
awk 'NR=='$i temp.txt
Should I put there $3(the argument I want to compare every line)?

Code:
awk '$3=='$i temp.txt
awk '$3'$i temp.txt

Both doesn't work.
# 5  
Old 11-24-2009
You would just be using awk rather than head.

grep "$3" `awk 'NR=='$i temp.txt ` > temp2.txt

Though you could have used head anyhow:
`head -$i temp.txt|tail -1`


If you are simply looping over a file, why not:

i=0
for fname in `cat temp.txt`
do
if test $i -gt $max; then
break
fi
grep "$3" $fname > temp2.txt
cat temp2.txt
i=`expr $i + 1`
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string in a file and paste next line in a specific way

Hello, I know there are many questions and replies regarding grep command. What I would like to do is a bit different. File A: hello world welcome to my page this is my test site how are you I am fine, thank you where have you been I was in hospital really hope you are fine now Thanks,... (10 Replies)
Discussion started by: baris35
10 Replies

2. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. 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

4. Shell Programming and Scripting

How to grep an empty line in a specific column of a file?

Suppose i have the following data : cat file.txt 12431,123334,55353,546646,14342234,4646,35234 123123,3535,123434,132535,1234134,13535,123534 123213,545465,23434,45646,2342345,4656,31243 2355425,2134324,53425,342,35235,23434,234535 3423424,234234,65465,,2344,35436,234524,234... (7 Replies)
Discussion started by: Ravi Tej
7 Replies

5. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

6. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

7. UNIX Desktop Questions & Answers

grep lines with two specific characters somewhere in the line

I'm having trouble with extracting certain lines from a file based on whether they have all the required fields. Original file: snt:594:Sam N This bpt:2342:Bob P That lr:123 wrp:23:Whoever Person cor:794 Desired output: snt:594:Sam N This bpt:2342:Bob P That wrp:23:Whoever Person ... (3 Replies)
Discussion started by: Chthonic
3 Replies

8. Shell Programming and Scripting

grep specific line

if i have a file that contains common directory -bash-3.2$ cat test /opt /opt/mdm /opt/test -bash-3.2$ how can i grep "/opt" so that the result should only show "/opt" (6 Replies)
Discussion started by: ryandegreat25
6 Replies

9. Shell Programming and Scripting

grep on specific line of the file

Hi, how can we search for a word (with case ignore )on specific line numbers ex: Awk /^regEX/ with condition on first line or second line Awk/^ regex1/ on line 1 Awk /^regEX2/ on line 3 thanks in advance (4 Replies)
Discussion started by: rider29
4 Replies

10. Shell Programming and Scripting

grep a word from a specific line

for example: searches only for single word for single word this is line three match=$(grep -n -e "single" data.txt) this command will stored "..... single ...... single" into search. how can i grep the single word just from line 2 only?? (3 Replies)
Discussion started by: blurboy
3 Replies
Login or Register to Ask a Question