LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-05-2009, 11:43 AM   #1
coppuca
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
number of lines to the new file


Hi,
I have a huge file and I need to put only few lines of it in a new file.
For example:
Code:
awk '/keyword/ {print NR}' myfile
shows line #567, and I need to print lines 567-577 to a new file.

Please help me.
 
Old 01-05-2009, 11:56 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you want to extract the lines which match some keyword (or regular expression pattern), you can just use grep. awk can do all this too, but it's more complicated.

e.g.
Code:
$ ls
my_original_file
$ grep "bananas" original_file > new_file
grep's output is all lines which match the pattern "bananas", and this output is re-directed into a new file called, "new_file". No need to worry about the line numbers at all. You can do it of course, but it seems more complicated than you need.
 
Old 01-05-2009, 11:58 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
sed -n '567,577p' oldfile > newfile
 
Old 01-05-2009, 11:59 AM   #4
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Rep: Reputation: 78
Hi coppuca,

Well you can use grep instead of awk
Code:
grep -A10 'YourPattern' Original_File > Destination_File
The previous code will display the 10 next line matching YourPattern

-A = After the matching line
-B = Before the matching line

I Hope that will help.

Best regards,
Angel.

Last edited by angel115; 01-05-2009 at 12:01 PM.
 
Old 01-05-2009, 11:59 AM   #5
coppuca
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Original Poster
Rep: Reputation: 0
no. i want to extract the line that match the keyword + 10 lines below it.
 
Old 01-05-2009, 12:02 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
What Angel said.
 
Old 01-05-2009, 11:59 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Sorry my other answer did not fit the question exactly.

There is also:

sed -n '/pattern/,+10p' filename
 
Old 01-06-2009, 11:17 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 727

Rep: Reputation: 74
Hi.

If you have a really long file, you may want to consider some optimization. I don't have any really large files, but here are results on working with a file that is around 1 GB. I assume that for a match, you want only the first hit. I have adjusted the requirement form 10 to 2 to save posting space:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate obtaining a segment, piece, part of a file.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed grep cgrep
set -o nounset
echo

FILE=${1-/tmp/test-one-gb}

echo "  Lines in data file $FILE:"
time wc -l $FILE

echo
echo " Results, sed:"
time sed -n '434,435 p' $FILE

echo
echo " Results, sed with quit:"
time sed -n -e '434,435 p' -e '436 q' $FILE

echo
echo " Results, grep, max-count:"
time grep --max-count=1 -A 1 -n "nightmare" $FILE

echo
echo " Results, cgrep, -N matches:"
echo " http://www.bell-labs.com/project/wwexptools/cgrep/"
time cgrep -N 1 +1 -n -D "nightmare" $FILE

exit 0

Code:
$ ./s1

(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
GNU sed version 4.1.2
grep (GNU grep) 2.5.1
cgrep - (local: ~/executable/cgrep Sep 28 2007 )

  Lines in data file /tmp/test-one-gb:
14754910 /tmp/test-one-gb

real    0m19.423s
user    0m1.049s
sys     0m1.645s

 Results, sed:
nightmare to a dead sartainty.  Landlord, I whispered, that aint the
harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m5.926s
user    0m5.020s
sys     0m0.807s

 Results, sed with quit:
nightmare to a dead sartainty.  Landlord, I whispered, that aint the
harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m0.001s
user    0m0.002s
sys     0m0.000s

 Results, grep, max-count:
434:nightmare to a dead sartainty.  Landlord, I whispered, that aint the
435-harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m0.001s
user    0m0.000s
sys     0m0.001s

 Results, cgrep, -N matches:
 http://www.bell-labs.com/project/wwexptools/cgrep/
434:nightmare to a dead sartainty.  Landlord, I whispered, that aint the
435:harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m0.002s
user    0m0.001s
sys     0m0.002s
Note that without the "quit", sed will go through the entire file, whereas it's much faster with the "quit". If you want to do matching, then the GNU grep has a feature to stop at "n" hits. If you don't have GNU grep, then one can obtain cgrep, which has similar features (and much more), from the site noted ... cheers, makyo
 
  


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
Viewing specified number of lines sir_com Linux - Newbie 2 11-17-2008 01:27 AM
number of lines for runlevel 3 RAFAL Linux - General 3 07-24-2008 12:53 PM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 03:02 PM
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 01:36 AM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 05:01 PM


All times are GMT -5. The time now is 04:43 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