LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-12-2009, 10:56 AM   #1
notoriouskingpin
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

#!/bin/sh
echo -n "Enter a file name : "
read file
jones=0


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

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

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

Rep: Reputation: 70
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

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

def worker(file, word):
    try:
        file = open( file ).readlines()[5:]
    except:
        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
    try:
        worker( argv[1], argv[2] )
    except:
        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, 01:09 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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:
http://www.grymoire.com/Unix/Sed.html
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:17 PM.

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
Open Source Consulting | Domain Registration