how do I filter for a specific line in a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how do I filter for a specific line in a file?
# 1  
Old 06-19-2008
how do I filter for a specific line in a file?

It seems like it should be very simple to have a command like grep return a specific line from a file but I can't find anything in the man pages to make grep search by line. I've looked in cat as well. Anyone know how to return a single line from a file?
# 2  
Old 06-19-2008
I'd also like to add that I want to be able to find the nth line in a file where n is a shell variable.
# 3  
Old 06-19-2008
more +n file|head -1
# 4  
Old 06-25-2008
Strange, my head command on Mac OS doesn't seem to take std input on this. Each command works fine alone. Here's what I used:

>cat b
1
2
3
4
5

>more +3 b | head -1
1

Odd? Seems like it should give
3

>more +3 b
3
4
5

This works good. Now running head -1 should just give the first line. Bug or me?
# 5  
Old 06-25-2008
more does the same as cat when followed by a pipe, the +n does nothing in this case.
# 6  
Old 06-26-2008
Will this work for your project?

$ cat myfile
1
2
3
4
5
$ sed -n '3p' myfile
3

Last edited by eBay; 06-26-2008 at 07:38 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Filter lines based on values at specific positions

hi. I have a Fixed Length text file as input where the character positions 4-5(two character positions starting from 4th position) indicates the LOB indicator. The file structure is something like below: 10126Apple DrinkOmaha 10231Milkshake New Jersey 103 Billabong Illinois ... (6 Replies)
Discussion started by: kumarjt
6 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

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

5. Shell Programming and Scripting

Need help in a script to filter specific pattern

Hello , Below is the command srvctl config database -d cmdbut -s boms10.world Below is the output. Now in the command instead of cmdbut it will be a variable like $database which would be called in the script as below: srvctl config database -d $database -s $service Now,as shown in... (6 Replies)
Discussion started by: Vishal_dba
6 Replies

6. Shell Programming and Scripting

how to filter file for specific date

Hi My OS is solaris 64 bit 10, I have many files in a logs directory where we recive 40-50 logs every day. i have last 20 days file present in this directory. I want to move each day file to a particulaar directory whose name is appended with the date of file. eg Code: 1.txt file... (1 Reply)
Discussion started by: guddu_12
1 Replies

7. Shell Programming and Scripting

Filter files by exact line anywhere in file

I have two files. L1 and L2. (each file, has one string per line without any spaces) I would like to get those lines in L1 that are not present anywhere in L2. No substring match, just exact line by line match but it can be anywhere in the file. (line 5 in L1 may be present in line 22 of L2,... (2 Replies)
Discussion started by: nick2011
2 Replies

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

9. Shell Programming and Scripting

[ask]filter with line number in file

dear all about my last question in fileA.txt (all list line i want to find in fileB.txt) is content of line number fileA.txt 23 34 35 55 59 and fileB.txt is content like 24:asd|ekkk|001|001 123:bca|egsd|210|002 1231:cas|egds|322|231 ... in fileB.txt they have line number like... (5 Replies)
Discussion started by: zvtral
5 Replies

10. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies
Login or Register to Ask a Question