We help IT Professionals succeed at work.

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

291 Views
Last Modified: 2017-04-12
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?
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Is the opposite command of tail (head) an option?
You could use wc to count the number of lines, then seek move to ......
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
MURUGESAN NApplication Development and Automation Management
CERTIFIED EXPERT

Commented:
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
MURUGESAN NApplication Development and Automation Management
CERTIFIED EXPERT

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions