LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-11-2008, 12:44 PM   #1
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Rep: Reputation: 15
grepping different lines.....


i want to grep 2nd line and 35th line of 15 files simultaneously.. can anybody help me.
 
Old 04-11-2008, 01:38 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I'm not sure you can do it with grep (I'm sure someone will point out if I'm wrong )
To get the Nth line of a file I've always done (where N is the line number you want):

Code:
head -N file | tail -1
So, you could put that into a foreach loop (note, this is tcsh):

Code:
foreach FILE (list of files)
foreach? head -2 $FILE | tail -1
foreach? head -35 $FILE | tail -1
foreach? end
HTH

Forrest

p.s. if you can get the "list of files" from a command (like ls) replace it with `command` (those are "back ticks") so it would be "foreach FILE (`ls`)"

Last edited by forrestt; 04-11-2008 at 02:29 PM.
 
Old 04-11-2008, 02:05 PM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
In bash
Code:
for file in list_of_files; do 
awk 'NR==2; NR==35' $file | grep whatever 
done
 
Old 04-11-2008, 02:29 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
After seeing pwc101's code block I realize I may have misread your question. If you want to determine if the 2nd and 35th line contain some string then you will want to use grep:

Code:
grep -n somestring filelist* | grep -e :2: -e :35:
HTH

Forrest

p.s. The reason I use head and tail instead of awk is that together they are still about a third the size.

Last edited by forrestt; 04-11-2008 at 02:30 PM.
 
Old 04-12-2008, 07:40 AM   #5
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Original Poster
Rep: Reputation: 15
There is a problem in the files. At first i didnot read it properly. As I told earlier the lines i have to grep or not 2nd and 35th. There is an inconsistency in the required lines. But there is particular line in the every file.

The required lines are 2 lines above the particular line and 11 lines below the particular line.

like...

sjkdjkd

Henry Mac 123456 line1 to be grepped
------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxx ----- This is unique(particular)line
jljfs
jlksjdf
jlkjsdf
jkldsf
kldsf
jkdsa
jkdfs
jsldfjlsj
jslkdjfkl
jlskdfjlk
Basic IB $123456.00 line2 to be grepped


There are total 15 files to be processed.
lines should come as

line1
line2
line1
line2

please help me....
 
Old 04-12-2008, 09:35 AM   #6
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Code:
#!/bin/bash
>output_file_name
for fic in <list of files>; do
grep --before-context=2 "xxxxxxxx" $fic|head -1 >>output_file_name
grep --after-context=11 "xxxxxxxx" $fic|tail -1 >>output_file_name
done
<list of files> can be something like : $(ls *.extension)
or directly : file1 file2 ... file15
"xxxxxxxx" should be a string which identifies the "unique(particular)line" you mention.
For documentation read 'man grep', 'man bash', ...
Bye.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grepping "'s and .'s and the such secretlydead Linux - Software 6 11-30-2007 03:49 AM
Advanced Grepping keysorsoze Linux - General 10 04-20-2007 06:00 AM
grepping all manfiles pgrodt Linux - Software 8 12-01-2006 10:40 AM
question about grepping something Grafbak Programming 6 03-01-2005 01:28 PM
Not grepping emails right mikeyt_333 Linux - General 11 04-13-2003 02:48 PM


All times are GMT -5. The time now is 07:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration