LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-08-2008, 07:11 AM   #1
teguh.purnama
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
Thumbs up How to take value from file (bash script)


The content of the file is (file name is varlist):

Quote:
list1: 235.214 -6532.211 0.00214
list2(addition): -6524.214 23.125 0.00234
how to take those value using bash script. I tried to use

Quote:
$var1=`cat varlist | grep "list1" | expr substr \([0-9]......)\`
it result only error.
 
Old 09-08-2008, 07:22 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
  • Do not prefix a variable name with $ when assigning to it, only when taking the value.
  • Unnecessary use of cat, you can pass the input file name to grep directly as an argument.
  • You can use cut with -d option to split lines by some character, which might be cleaner than using expr.
 
Old 09-08-2008, 07:23 AM   #3
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
var1 = `awk '/list1/{print $2}' varlist`

Dave

More clearly var1 = $(awk '/list1/{print $2}' varlist)

Last edited by david1941; 09-08-2008 at 07:24 AM.
 
Old 09-08-2008, 07:32 AM   #4
teguh.purnama
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Original Poster
Rep: Reputation: 0
awk script seems easier for me.
thanks for the answer.
 
  


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
File size using BASH script cb951303 Programming 16 11-07-2012 07:45 AM
file or directory? bash script efus Programming 3 04-26-2007 06:11 PM
how to creata file through bash script? varala_kanth Linux - Software 2 04-29-2004 02:52 PM
Writing to a file - Bash script Skute Programming 2 03-15-2004 04:41 AM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:24 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
Open Source Consulting | Domain Registration