How to grep particular line from a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep particular line from a file?
# 1  
Old 05-09-2014
How to grep particular line from a file?

Hi,

I am having file like
Code:
$ cat file

sumanth
anil
harish

from the file i want to grep one line at a time depending on the line number.And can it work through grep -n...

TIA

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Even if its this small... Thanks

Last edited by vbe; 05-09-2014 at 11:58 AM..
# 2  
Old 05-09-2014
To do a grep one line at a time would be to read line by line, e.g using a loop

P:S. please dont post in AIX something not OS specifc, threat will be moved...
# 3  
Old 05-09-2014
@Sumanth:could you explain it bit more what exactly u want or expected output
# 4  
Old 05-10-2014
Hi,

The scenario is... I am having a text file it is having 100's of lines..
from that one i have to grep a line according to the user input ... Here I am taking line number as a parameter....say if user gives 3 as a line number, then it should grep only contents in 3rd line

TIA..
# 5  
Old 05-10-2014
Please give us a sample input file, sample line number, and explain how you know what you're looking for on that line, and then show us what output you want if you find it and what output you want if you don't find it.

Do you want to use a fixed string, a basic regular expression, or an extended regular expression to search for a matching line?
# 6  
Old 05-12-2014
I'm having file like,

Code:
$cat file

Code:
0wdu12345
oghi6745211
utyu672921
tyug1562722

if user give 3(Line number) as input then it should grep the contents in the 3rd line here it is utyu672921.

TIA

Last edited by Don Cragun; 05-12-2014 at 02:27 PM.. Reason: Add CODE tags again.
# 7  
Old 05-12-2014
try
Code:
awk 'NR==3{ print; }' filename

or
Code:
sed -n '3,3p'  filename

or
Code:
awk 'NR==3' filename

This User Gave Thanks to Makarand Dodmis For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

Grep in file and print in the line

hi # cat test.txt Test Date: 20131008 1515 -------------------------------------------------------------------------------------------------------------- Saxx = Proc_m0_s13 : 1640 Saxx = Proc_m0_s15 : 1791 Saxx = Proc_m0_s17 ... (2 Replies)
Discussion started by: justbow
2 Replies

3. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

4. UNIX for Dummies Questions & Answers

grep a line from file?

hello, i am trying to grep and the show lines from file that starts with * example of file: * bobo: dsfdf,sdfsd,sdfsdf ------------------------------- gogo: sdf,sdfsdf,sdfsdf,sdfsdf --------------------------------- * bobo2: sdfsdf,sdfsdf,sdfsdf... (5 Replies)
Discussion started by: zigizag
5 Replies

5. UNIX for Dummies Questions & Answers

grep command on a line from a file

Dear forum, Please excuse my ignorance. I have looked through various forum posts and tried various solutions but have not been able to solve my problem. File1.txt consist of a list of 100 names (one per line). File2.txt contains details for 1000 people (one per line), with the first field... (7 Replies)
Discussion started by: beginner0302
7 Replies

6. Shell Programming and Scripting

grep a line from a text file

Hi all, I need to grep a line from a log file which ensures me that the application server script is executed successfully. Some body please help me on this. I also need to write a while loop in which i need to use the status of the above grep output. Could some one please tell me how to use... (12 Replies)
Discussion started by: firestar
12 Replies

7. UNIX for Dummies Questions & Answers

grep a file from a given line only

Hi all, I'm looking for the line number where the string "total" appears. I know that this is after a given line number which I know. So I would like to know the line number of the first "total" after the line X. Is it possible to use grep a file from a given line only? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

8. Shell Programming and Scripting

how to grep a file and cut a corresponding value from the line

i have to grep a file which has contents like /Source/value/valuefile/readme.txt DefaultVersion:=1.7 I have to grep this file with "/Source/value/valuefile/readme.txt" and cut the corresponding value "1.7" from the same line i tried grep and cut but not working. (1 Reply)
Discussion started by: codeman007
1 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

Read file then grep the line

Dear all, I am reading a file that has 1 column. While reading I must find the line references from the another file. The following shell doesn't works. Please help #!/bin/bash while read filename; do grep ${filename} fs_full.dat >> unprocfull.dat; done < unproc.dat But when... (2 Replies)
Discussion started by: mr_bold
2 Replies
Login or Register to Ask a Question