Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-12-2009, 11:56 AM   #1
LQ Newbie
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
how to skip 5 lines

how do you skip 5 lines from a text file and then search the text file for a specific word. after searching the word and display the each line corresponding to the search word.

so far i coded this much

echo -n "Enter a file name : "
read file

# make sure file exist
if [ ! -f $file ]
echo "$file not a file!"
exit 1

# put while loop to read a $file
while read line
#process each word
head 20 $file
for w in $line

Last edited by notoriouskingpin; 03-12-2009 at 11:58 AM.
Old 03-12-2009, 01:09 PM   #2
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
So you want to grep a file for a word, but don't want it to look in the first five lines? Well, there are a few ways though I'd reccommend python instead of bash. Python would be

#!/usr/bin/env python
#-*- encoding:utf-8 -*-

def worker(file, word):
        file = open( file ).readlines()[5:]
        print "File Does Not Exist"
        raise SystemExit

    for i in range( len( file) ):
        if word in file[i]:
            print file[i]

if __name__ == '__main__':
    from sys import argv
        worker( argv[1], argv[2] )
        print """ Syntax:
%s file word
""" % ( argv[0] )
If you're dead set with bash, you could get the number of lines (see man wc), minus 5 and then tail that into a variable. Then you could grep through that.
Old 03-12-2009, 02:09 PM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Is this homework??

I don't see anything about 5 lines in your code.

One thing to look at is SED addressing. You can specify a range of line numbers to search for a keyword.

Excellent SED tutorial here:


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
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 06:47 AM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 09:25 AM
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 01:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 03:03 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:57 PM.

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