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 06-09-2008, 06:54 PM   #1
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
Senior Member
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

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

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

cat file.txt | while read line ; do
   if [ "$int" == "5" ]; then
      echo "found line nr 5"
      echo "${line##*|}"
Old 06-09-2008, 08:16 PM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
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.


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:37 AM.

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