Download your favorite Linux distribution at LQ ISO.
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-17-2008, 02:13 PM   #1
LQ Newbie
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
how to select a particular line from a file and assign it to a variable?

R 468bf89188632e67cd268d0b1a24833c10056b34 [13] 840 (13920) kind=7
R c7465b12162ae2c0a4601ba7895f154f38df1a69 17669
R 604775f0f7326e42e853359f64b2f5585d8f3551 24069
R df03a22135c735b59753cee0c8f4a8e4cac9f032 28528
R 5639c31a86ed9735a0c654510d1038620a85ab92 43007
R 519e896e36dff8dfd32a4bcfc2afac0d360ed1e4 67924

the above is how the file looks like. From the file i want 3rd row, 2nd column to be assigned to a variable.. can u let me know how to do it?



Last edited by pdklinux79; 06-17-2008 at 02:15 PM.
Old 06-17-2008, 02:21 PM   #2
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Try this:

value=`head -3 | tail -1 | awk '{print $2}'`
echo $value

Old 06-17-2008, 02:25 PM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What have you tried?

You can get data by line # using SED, and then cut to give you the second field. AWK is also good for data that is in fields.

Example with SED:
sed -n '3p' filename|cut -d " " -f 2

Assign the whole thing to a variable name using:
var=`command` (backtics, not single quotes)
Old 06-17-2008, 03:03 PM   #4
LQ Newbie
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
thanks .. both r working...

Last edited by pdklinux79; 06-17-2008 at 03:05 PM.
Old 06-18-2008, 07:21 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
Yet another alternative

value=`head -3 | tail -1 | cut -d' ' -f2`
echo $value


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
assign converted string to variable bmaheni Linux - Newbie 2 12-28-2007 08:41 AM
How to assign a string to a structure variable in C? daYz Programming 7 06-13-2007 02:38 PM
how to read certain line of file, and output to variable? jimmyjiang Red Hat 2 05-01-2007 08:46 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
set a line in a file as a variable tpreitano Linux - General 5 08-24-2005 11:53 AM

All times are GMT -5. The time now is 04:18 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration