LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how do i read a particular line from the text file.. (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-read-a-particular-line-from-the-text-file-648134/)

pdklinux79 06-09-2008 05:54 PM

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.

w1k0 06-09-2008 07:10 PM

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/'

Uxinn 06-09-2008 07:12 PM

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


jschiwal 06-09-2008 07:16 PM

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.


All times are GMT -5. The time now is 05:29 PM.