Programming :: Parse Multiple Variable From Text File With Bash?
Jul 13, 2010
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
[code].....
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.
For example, I have a text file with data which lists numerical values from two separate individuals
Code: Person A 100 200 300 400 500 600 700 800 900 1000 1100 1200
Person B 1200 1100 1000 900 800 700 600 500 400 300 200 100
How would I go about reading the values for each Person, then being able to perform mathematical equations for each Person (finding the sum for example)?
I want to write a bash script to parse a text file with the following lines and set variables for each line so that I can use them in the rest of the script.
Timestamp=123456789 Company=ABC Company Server=Server Recipient=Joe Smith Email=joe@abc.com
simplest way to read each line one at a time for everything before the =, set that to a variable name with the value equal to everything after the =
i am on processing text tasks And i found that if you assign a text to a variable is chomp'ed automatically the newline
Code:
variable=$(cat file.txt)
The problem is i can only access the items/lines using:
Code:
for line in $variable do echo $line # Other commands done
how do i convert this to an indexed array. More importantly, how do i get access to individual $line[0], ..., $line[n] Another thing, if the file.txt, has lines with spaces it is a mess using the for...in..., but echoing prints line by line...o_0
The output of following code is not like it's intended ...
Code:
This is the output:
Code:
Test prepending ...apple is a nice word, hour is a nice word, But of course what I want to do in the first set of commands is to prepend the word "an" to the words "apple" and "hour" in the for-loop.
I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.
For example:
Code:
Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).
This is what I have right now. Well, I thought I knew sed, and apparently I don't... I tried writing this for someone else, and this has given me trouble, so since the user pretty much figured it out on his own, here it goes. Say VARR=1, so VARX and VARY contain the above text, appended by 1. What I am trying to do is replace the text "defaults.ctl.card 0" by VARX and "defaults.pcm.card 0" by VARY. The contents of FILE1 is the file being used to search for both text fields, and FILE2 is the output file. I tried using single quotes, double quotes, and a mixture of both, and no go whatsoever. So my question... What is the proper way of searching for text within a file and replacing with a variable?
I would like to extract Room number, Lastname,Firstname,invoice (205880080),arrival date, departure date, and total(229.46). Can you at least give me a hint on how to proceed? I have tried a lot but I am stumped from the beginning.
***History*** Room: 124 B Payment: Bell/TRAVELSCAPE.COM Lastname*FIT*,Firstname 4A, 0K, 0B Guest Bell *205880080 FT Bell *205880080 July 31, 2010
At my wit's end I can't find anything that I understand well enough to use. This is for a Unix class, we are working with shell scripting. File1 has 5 in it and File2 has 100 in it.The teacher wants us to read the values then do the math. This is what I have so far:#!/bin/bashvar1='cat File1'var2='cat File2'var3=`echo "scale=4; $var1 / $var2" | bc`echo The final result is: $var3
Code: #include <stdio.h> struct test { int i; int j;
[Code]....
why i am getting this error. I know the error is occuring because i have assigned values to obj.i and obj.j outside main(). But i want to know why do that result in an error. From my part i have created an object 'obj' of stucture 'test' and assigned values to its variables.
I need to Read a path of a file witch is written in Text file i used this
Code:
FILENAME=$1 while read line do echo $line done < $FILENAME
it worked and showed me the Line witch was written in my file but now my problem is how am gonna use that line as a path i mean for example if am gonna execute a linux command on that file like dpkg -i /path/to/the/file how am gonna export it from The $Line variable and use it after the command.
To run a parallel chemistry program, I need to build the host list. The cluster already has SGE(grid engine) installed and it generates a host list file with the following contents:
The important bits are hostname(ex compute-1-1) and number of cpus to use(ex 2). And for this program, it wants them in this form, a shell variable: HOSTLIST=hostname:cpus=X hostname:cpus=X .... I've tried this script, but it doesn't work
I have a text file which stores the list of files & dir, I want to get only file's extensions from this file & want to store it in another file.eg, below is the file's contents & from it I want to get the extensions sh, pl & h & want to store it in another file. Also I don't want directory list.
A scripts/services_restarter.sh A scripts/svn post_commit scripts A scripts/tmp/
write such script (bash script). I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable.
For example. File: PHP Code: PHP Code: Start 15022011 Eng 12-3-42 SN1232324422 11 test-string-first SN322211 securities HH keyword-string:123456321-net mark (11-22)
I have a script which checks on my jobs that run on some cluster.The script, "script.sh", takes as an input the job-id for the job to be checked. Sometimes I have 100s of jobs and I want to check them all (for successful completion.) I could put these job-ids into a text file, "job-id.txt", each id in its own line.For each job-id, the script would ask me few questions (with a yes or no answers) to see if I want to do some other checks for each job-id.I want to know how may I direct my job-ids from this text file into the script one job-id at a time.
Is there a way to process individual characters one-by-one from a text file in Bash, or is that hoping for a little too much from this lovable old clunker?
I'm trying to figure out how I can get a request count per CIDR/24 from an apache log in combined format - e.g.: Code: 24.147.44.122 - frank [10/Oct/2011:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "[URL] "Mozilla/4.08 [en] (Win98; I ;Nav)"
I'm stuck using BASH for this and I generally write everything in Python, or even Perl if necessary so BASH isn't the most comfortable for me. I've got enough to extract the IPs and get a count I just need a slick way to come up with a sum on a per CIDR basis.
Now in my bash script, I want to get the output /home/user instead of $HOME once read. So far, I have managed to get the $HOME variable but I can't get it to echo the variable. All I get is the output $HOME.
I have a log file that contains information like this:
---------------------------- r11141 | prasath-palani | 2010-12-23 16:21:24 +0530 (Thu, 23 Dec 2010) | 1 line Changed paths: M /projects/ M /projects/
[code]....
what i need is, i need to copy the data given between the "---" to seperate files, for, e.g. the first set of data between the "---" should be in one file and another set of data in another file.
bash 3.1.17(2) I'm trying do write a shell script which must operate on each line of an ASCII text file. So, all the code must be inside a loop, and inside the loop, the first thing should be to read the next line from the file. I have the bash read command. But it reads from stdin. Any way to make read from a file?
I have written quite a few separate bash & scripts and php scripts that up to now I have run from cron jobs. However I have to estimate how long each takes to run, before running the next and so it probably takes much longer than necessary to run them all. They have to run in order.
Now there are so many I am thinking it would be better to have a master bash script that would run one after the other, but I am not sure how to get the master script to wait before starting to run the next script. Is this possible and is there a command that will make the script wait between bash and php scripts , for them to finish, before running the next?