LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2010, 01:52 AM   #1
Goni
LQ Newbie
 
Registered: Sep 2005
Posts: 26

Rep: Reputation: 15
Parse multiple variable from text file with bash


Hello everyone
I am trying to think of a logic where my file contains some data I had to read and do some processing. Issue is that file contains data multiple times. For example:

:::::::::::
var1=value1
var2=value2
:::::::::::
var1=value3
var2=value4
:::::::::::

I have to read first paragraph of variables and do some processing and then move on until the end of file. Variable names are same in whole file but for each paragraph the value is different. I can't think of a logic to attain this task. How can I do it? It should be a simple bash script, but I am not able to work out. Need help.

Thank you
 
Old 07-13-2010, 02:05 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
I'd "grep" for one variable name piped with "head" and "tail" with the "-n" argument, then you get the variable line from the paragraph you want. Then awk to parse the name in a couple of name / value
 
Old 07-13-2010, 02:25 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In bash an useful statement to parse a file line-by-line is the while read loop. You can read two variables per line, using the equal sign as Input Field Separator. Inside the loop you can do whatever you want with the obtained variables, e.g.
Code:
#!/bin/bash
while IFS="=" read name value
do
  echo name is $name
  echo value is $value
done < file
Anyway, you may want to exclude some lines from the input (for example those ones with colons in your example). In that case you can feed the loop using process substitution. Just think at a command to retrieve only the lines you want to read, e.g.
Code:
done < <(grep -v ':' file)
Hope this helps.
 
  


Reply



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
variable from text file bash drumbum Programming 1 07-30-2009 09:50 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
Getting a variable from a text-file (bash) PatrickBecks Programming 4 02-14-2008 08:52 PM
bash script help to parse out text slack guy Linux - Newbie 3 12-30-2004 08:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 PM.

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