LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 06-09-2008, 06:54 PM   #1
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
how do i read a particular line from the text file..


THere is a text file. i want to read only every 5th line and then awk the value i need in that line, 3rd word... how can i do it in bash shell script? please help me as soon as possible.
 
Old 06-09-2008, 08:10 PM   #2
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
cat yourfile.txt | grep -En ^ | grep -E ^[0-9]*[05]: | perl -pe 's/^[0-9]+://;s/^\s*\S*\s*\S*\s*$//;s/\S+\s+\S+\s+(\S+).*/$1/'

Last edited by w1k0; 06-09-2008 at 08:19 PM.
 
Old 06-09-2008, 08:12 PM   #3
Uxinn
Member
 
Registered: May 2008
Location: Iceland
Distribution: Ubuntu Hardy
Posts: 47

Rep: Reputation: 16
are these every 5th lines different in any way ? is it possible to filter by using grep ? that would make it easy

Code:
grep 'some.filter' file.txt|awk ....

Code:
#!/bin/bash
int=0
cat file.txt | while read line ; do
   if [ "$int" == "5" ]; then
      echo "found line nr 5"
      echo "${line##*|}"
      int=0
   fi
   int=$(($int+1));
done
 
Old 06-09-2008, 08:16 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
You could use
awk 'NR%5==0 { print $3 }' file

The mod expression will select every 5th line. The print function prints out the 3rd record.
 
  


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
read a line from text file and extracting the details needed pdklinux79 Linux - Newbie 6 06-06-2008 11:41 PM
read line by line form text file in java. spank Programming 1 10-18-2006 03:46 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 03:10 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 06:28 AM
[C & Linux] Read line from text-file Y_Haarman Programming 5 11-08-2002 05:44 PM


All times are GMT -5. The time now is 03:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration