LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 06-17-2008, 03:13 PM   #1
pdklinux79
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?

value=604775f0f7326e42e853359f64b2f5585d8f3551





thanks

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

Rep: Reputation: 99
Try this:

Code:
#!/bin/bash
value=`head -3 | tail -1 | awk '{print $2}'`
echo $value
HTH

Forrest
 
Old 06-17-2008, 03:25 PM   #3
pixellany
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)
OR
var=`command` (backtics, not single quotes)
 
Old 06-17-2008, 04:03 PM   #4
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

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

Last edited by pdklinux79; 06-17-2008 at 04:05 PM.
 
Old 06-18-2008, 08:21 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Yet another alternative

Code:
#!/bin/bash
value=`head -3 | tail -1 | cut -d' ' -f2`
echo $value
 
  


Reply


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


All times are GMT -5. The time now is 01:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration