sed/awk/tail: how to read 3'de last line

how to put a variable to 3'te las t line from file?
i have a file which last 3 line is
...
...
12   <-- 3'de last line
...    
sql>      <-- last line
how to put a variable to read number 12 from file?
Jasmin shahrzadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Is the opposite command of tail (head) an option?
You could use wc to count the number of lines, then seek move to ......
0
tfewsterCommented:
VAR=`tail -3 yourfile |head -1`
2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Right way which I used to assign one command output to a variable:
        InputFile="./Jasmin_shahrzad.txt"
        if test -f "$InputFile"
        then
                LAST_Third_LINE=''`/usr/bin/tail -3 "$InputFile" |/usr/bin/head -1`''
                echo $LAST_Third_LINE
        else
                if test -d "$InputFile"
                then
                        echo "Input file $InputFile is a directory"
                        echo "Provide valid input file"
                else
                        echo "$InputFile No such file"
                fi
        fi

Open in new window

Reason for using full path of any executable command:
unset -f tail
tail ()
{
         echo Use full path of command /usr/bin/tail $@
         echo instead of using tail $@
         echo Returning \$? to 12
         return 12
}

Open in new window

$ tail -f Dinesh_Murugesan.txt
           tail -f Dinesh_Murugesan.txt
           Use full path of command /usr/bin/tail -f Dinesh_Murugesan.txt
           instead of using tail -f Dinesh_Murugesan.txt
           Returning $? to 12
$ echo $?
12
$ unset -f tail
$ tail -f Dinesh_Murugesan.txt
       tail: cannot open 'Dinesh_Murugesan.txt' for reading: No such file or directory
       tail: no files remaining
0
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Same using /bin/awk
Following codes assumes handled all exceptions:
/bin/awk '{ EachLine[NR]=$0;}END { print EachLine[NR-2]}' Jasmin_shahrzad.txt

Open in new window


using /bin/sed
NUMOFLINES=''`/usr/bin/wc -l  Jasmin_shahrzad.txt | /bin/sed "s/ .*//;"`''
REQUIRED_LINE_NUM=''`/usr/bin/expr $NUMOFLINES - 3 + 1`''
/bin/sed "$REQUIRED_LINE_NUM"'!d' Jasmin_shahrzad.txt

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AWK

From novice to tech pro — start learning today.