Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i read only third line from a file into a variable in shell script

Tags:

shell

I am executing a shell script in which I run a sybase query which gives the O/P in a file .

The Output is :-

            tablename

            ---------------------------
            result of query executed my case will be an integer returned


            (Number of rows effected ).

What i want to do is read result of query executed which in my case will be an integer returned into a variable in a shell script . It will be in 3rd line in the file in which i have redirected the Output of my Sybase Query .

How can i do that ?

like image 389
Invictus Avatar asked Apr 02 '12 08:04

Invictus


People also ask

How do you display the first 3 lines of a file in Unix?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file. You can change this by typing head -number filename, where number is the number of lines you want to see.


2 Answers

Did you mean this?

x=`sed -n '3p' inputfile`;
echo $x;

EDIT

Do this.

thirdline=`sed -n '3p' /home/nmsad/abc.txt`;
echo $thirdline;
like image 75
cppcoder Avatar answered Nov 15 '22 08:11

cppcoder


Try using sed like this:

variable=$(your_script | sed -n '3 p')
like image 27
Mithrandir Avatar answered Nov 15 '22 09:11

Mithrandir