Programming :: BASH: Read + Define + Modify A Numeric Value From/to A File?
Mar 25, 2010
I want my bash file to read from "input.dat" the two values emin emax. My input file looks like that:
#cat input.dat
!Energies
emin 10.00 !minimum energy
emax 30.00 !maximum energy
Now this seems to be not so hard with the command awk
#!/bin/bash
awk '{FS=" "}/emin/{print $2}' input.dat
awk '{FS=" "}/emax/{print $2}' input.dat
[code]....
So far so good. Now, I want to define two variables (e.g. e1,e2) in the bash file, so that their values would correspond to 00.00 and 30.00, as read from the input file. This one I have not found yet, thus asking for your advice. At the end, writing echo $e1 $e2, I should get 10.00 30.00 This is even harder to me: I want to replace the values emin,emax in a new file "modify.dat" which looks like that:
...
c---- energy interval
emin = 1.00
emax = 2.00
...
with the values e1 and e2 I have in my bash file. In other words, I want to call "modify.dat", find these two lines and replace the numeric values with the e1 and e2. At the end, my file should be like:
...
c---- energy interval
emin = 10.00
emax = 30.00
...
View 3 Replies
ADVERTISEMENT
Jul 7, 2011
Bash 3.1.7
Code:
Code:
Code:
I think read A1 A2 makes A1, A2 string variables. Then, when A2 gets the value 01, '01' should be a string. But for some reason bash takes it as numeric. I know there are no types in bash.
View 11 Replies
View Related
Aug 8, 2010
I have a script that reads part of a line, delimited between the first and second intended part by a colon. Then it "chops" the part after the colon, which are words offset by commas (counting them beforehand so as to catch every word in the string's second part), like this:
Code:
"COLORS.JPG:red,orange,yellow,green,"
(Returning)
red
[code]....
single script that parses/breaks both parts of a line like this "COLORS.JPG:red,orange,yellow,green;blue,indigo,violet," so that the two parts, separated into single words (or two and three words, sometimes with spaces) can be used as single-line annotations and written to JPEG files using Exiv2. So far, I haven't been able to come up with a script that does this without one part of the total string(usually that part after the colon) becoming the first word in the second array. In other words, I look for this:
KEYWORDS:
[ ]red
[ ]orange
[ ]yellow
[code]....
Or vice-versa (ie, the second array winds up as a single-line "member" of the first). I think it's because I'm using a single while read loop to read the text file in which the filenames and substrings happen to be. If there's some way of reading a file once and going back to the beginning to read it again in another while loop, I haven't found it.
View 14 Replies
View Related
Mar 14, 2011
I am struggling with Bash scripting at the moment (I can't seem how anyone can write scripts with this language!!!) I have a need at home to have a cron job execute daily to lookup my downloads.txt file, read each url (per line) and download content from that url. Then that entry needs to be removed (well I keep all urls in memory and clear the file afterwards). If an error occurred during the download process, then the url is written to a downloads.err file. I got all the above working except for properly reading the url from the text file without including newline characters. I am using the following to read:
while read url; do
--Do whatever here--
done < downloads.txt
How can I get it not to let the url variable have newline characters?
View 11 Replies
View Related
Mar 11, 2011
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)?
View 13 Replies
View Related
Feb 27, 2011
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
View 9 Replies
View Related
Apr 27, 2010
I can define all variables in tcl programming in a file for instance var.cfgand source the same file in my tcl script such assource var.cfgIs this possible in perl too?
View 3 Replies
View Related
Jan 24, 2010
simple bash code:
Code:
#!/bin/bash
trap "echo 'you got me'" SIGINT SIGTERM # to trap ctrl+c
echo "Press ctrl+c during 5 sec loop"
for ((i=0;i<5;i++)); do
[Code]...
How come code behaves normally and stops when ctrl+c signal is caught and resumes, but after I use at least one timeout read in the code it looks like, if signal is caught again it doesn't pause the execution but skips the loop. If you remove -t (timeout) option from the read, both loops look the same!
View 10 Replies
View Related
Aug 21, 2009
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).
View 6 Replies
View Related
Jan 21, 2011
I have a output file look like this:
{"test1" : "test2", "test3" : "test4"},
How can I read word by word in each line?This is not working code:
a=0
while read word
do a=$(($a+1));
[code]...
View 14 Replies
View Related
Jul 7, 2011
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?
View 6 Replies
View Related
Sep 16, 2010
I had a script once that had in it a while true; do and read lines or data from a file similarly to a while read; do. I have a script I'm working on now where I'm trying to "prove the negative" that certain substrings culled from one file do not appear in another file. I'll try to explain what I mean with an example. An item in one list might be
Code:
[URL]
But the other list may only have
Code:
harvey1996nicks:Six stores in north London
harvey1997nicks:Three stores in Bristol
harvey1999nicks:Eight stores in the lower Midlands
The problem is, other items in the first list do match items in the second. The script I'm drawing this from has already pulled those out and written them to one (or several) lists that I've been able to use with other programs (Exiv2 specifically). The furthest I've bothered to go in the direction of "proving the negative" is a slow "if/then/fi" loop that looks like this:
Code:
linecheck=$[linecheck+1]
if [[ "$linecheck" == "$axe" ]] && [[ "$matchX" == "" ]]; then echo -e $file>>nomatches.txt; fi
linecheck is counting the lines of the second file -- the one with the designations or "labels" (the "so many stores in ..." from my example) -- as they are checked against the file names in the first (the "harvey199*nicks.jpg" etc of my example. axe is the number of lines in that first list (culled from a cat |wc -l at the beginning of the script). matchX is the variable the "prove the positive" script used for matches, one list to the other, which then got written line by line to a text file.
View 1 Replies
View Related
Dec 7, 2009
Fedora 12 gcc 4.4.1 I am doing some programming, and my program gave me a stack dump. However, there is no core file for me to examine.
So I did:
Code:
ulimit -c unlimited
and got this error message:
Code:
bash: ulimit: core file size: cannot modify limit: Operation not permitted I also tried setting ulimit to 50000 and still got the same error. The results of ulimit -a:
Code:
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
[Code]...
View 3 Replies
View Related
Dec 21, 2010
My linux distro is CentOS 5.3. Today I edited /etc/sysconfig/readonly-root and set "READONLY" to yes, now my /etc/sysconfig/readonly-root file is like this:
# Set to 'yes' to mount the system filesystems read-only.
READONLY=yes
# Set to 'yes' to mount various temporary state as either tmpfs
[code]...
View 3 Replies
View Related
Nov 4, 2010
i have just modify tcp.c file in/usr/src/linux/net/ ipv4 location.Now should i compile the complete kernel?if not then how to compile that net/ipv4 package or etc.
View 1 Replies
View Related
Mar 20, 2010
I have around 600 empty text files that I need to add the name of this file as part of the data, I mean files from "file1.txt to "file599.txt, all of them empty, and I need to get the name inside the file, so, when I open the file show the name as part of the data "file1".
View 11 Replies
View Related
Jun 3, 2010
I have been looking for a script example of reading and writing to the parallel port's data, status, and control registers using bash. I see it done in pascal, tcl, etc. but nothing in bash.
View 5 Replies
View Related
Sep 24, 2010
Code:
test=(1 2 3 4 5)
for car in ${test[@]}
do
echo "Element : $car"
[code]....
if variable $car equals 1, new element is added "6" to an array. But i don't know why when i am printing all elements of this array (echo "Element : $car") this element ("6") is not mentioned, but if i make a command which check an amount of contained elements by array it will be 6 elements.
View 14 Replies
View Related
Mar 28, 2011
I have a script that will ask the operator to specify whether the script should fetch the bits via FTP or SCP:
Code:
# Specify transfer method
echo "[setup]: Please specify file transfer method [ftp] or [scp]:"
[code]....
View 2 Replies
View Related
May 7, 2010
As I'm starting to learn bash scripting I'm trying to automatize some tasks I usually perform. I have a notification mail I need to send several times a day. It has this structure:
Quote:
Dear user,
blah blah blah blah
You need to contact the following people:
[code]...
To replace "user", I found this:
Code:
read -p "Please enter username: " username
echo "Dear $username,"
Which probe to be very useful with other simple notifications like this. But I don't know how to manage the email addresses as they are usually more than one and could vary from 1 to 10. They should appear one above the other. I found this: "Here is a little work around. The only thing the user needs to do is hit enter without anything else on a line and it will close out"
Code:
#!/usr/bin/ksh
word=a
until [[ $word = "" ]];do
[code]....
I tried to use it and modify for my needs but I failed, I don't realize yet how can I use it. If possible, I would like to use the until loop like the above example just for learning purposes but any other form will be accepted as well.
View 10 Replies
View Related
Nov 4, 2010
I don't think this is a "perl one-liner" of find and replace. I'm trying to auto-fill some information in a listing of files. The simplest example is that in the files the following exists:
I would want the script to find this and populate it with something like -- Date : 20101004-1758
I have a few more similar fields to autofill, and I'd like to do this from within a larger perl script I'm developing to process these files. So, how I perform in-place file modification from within a perl script?
View 3 Replies
View Related
Nov 24, 2010
First, I made a simply script which let me download a file from a filehosting site on my server, but I can only put one link there, so I need help how to put multiple links in.Here is my script:
Code:
#!/bin/bash
echo "Enter URL:"
[code]...
View 3 Replies
View Related
Mar 5, 2010
I have a fallowing simple bash script:
Code:
#!/bin/bash
echo -n "Please insert your name: "
read NAME
[Code]....
It works fine, but is it possible to have default values using bash read(1b)? I mean if user is prompter for sex, he just pushes ENTER and by default 'm' is chosen. Or if user is prompted for hometown and he doesn't insert anything, but just pushes ENTER button, 'New-York' sis chosen by default Are such default values possible in bash?
View 1 Replies
View Related
Feb 13, 2011
I would like to find something portable that work on all machines. I have hmtl2text installed, and perl and sh.
Anyone would know if someone already made such thing to read new through the console ?
View 1 Replies
View Related
Jan 25, 2010
I wanna learn how to store data in text file and be able to modify it and save as well using C++.
Note: im using Turbo C++ 3.0
View 9 Replies
View Related
Aug 9, 2009
I'm trying to get multiple numeric valued version information into a variable, all on the same line. I want for example $VERSION=3.1.0.01.002. I'm trying to pull seperate values from the file named version.properties, wherein the file contains;
patch.rel.num=0
sqa.num=01
major.rel.num=3
build.num=002
minor.rel.num=1
So I have the following script:
#!/bin/bash
BUILDVERFILE="version.properties"
PATCH=`grep "patch.rel.num" ${BUILDVERFILE} | awk {'print $1'}`
SQA=`grep "sqa.num" ${BUILDVERFILE} | awk {'print $1'}`
MAJOR=`grep "major.rel.num" ${BUILDVERFILE} | awk {'print $1'}`
BUILD=`grep "build.num" ${BUILDVERFILE} | awk {'print $1'}`
MINOR=`grep "minor.rel.num" ${BUILDVERFILE} | awk {'print $1'}`
P=`echo $PATCH | tr -d .=[:alpha:]`
S=`echo $SQA | tr -d .=[:alpha:]`
MA=`echo $MAJOR | tr -d .=[:alpha:]`
B=`echo $BUILD | tr -d .=[:alpha:]`
MI=`echo $MINOR | tr -d .=[:alpha:]`
VERSION=$MA.$MI.$P.$S.$B
echo $VERSION #
I end up with the value of .002 ?? Seems like it's only getting the last portion because if I run the script thru "bash -x" I get:
...
+ VERSION=$'3
.1
.0
.01
.002
'
+ echo $'3
.1
.0
.01
.002
'
.002
View 7 Replies
View Related
Mar 22, 2011
I have not been able to write bash to use a PID file to ensure no other instance of the same script is running! All three methods I can think of to see if the PID in the PID file is another instance of the script make the script exit with a return code of 1 but the same commands run at the command prompt work as expected.
The first attempt was:
Code:
The first attempted workaround was:
Code:
The second attempted workaround (with debug to make the following command prompt copy and paste meaningful) was:
Code:
Here's the command prompt session, testing with a stale PID file and then manually running the problem command and it behaving as expected:
Code:
This on Slackware64 13.1 which has bash 4.1.7.
In desperation I tried rebooting but the behaviour was the same.
View 2 Replies
View Related
Jul 22, 2010
AKA "zipping on the fly .. the slow-as-molasses way." The list includes full pathnames to each file, and they're all in subfolders of the same parent folder (which, unfortunately, is not the root folder of the drive or system on which the files reside). A cleaned-up and radio-ready portion of the list looks like
Quote:
.../taiga/ahqr-va-choyvp/bv0884-073.jpg
.../taiga/ahqr-va-choyvp/bv2635-073.jpg
.../taiga/ahqr-va-choyvp/bv3067-175.jpg
[code]....
What I'd like to be able to do is zip all the files in the list into a single archive, to avoid the step of having to copy them to the same location (presumably another folder on the HD) and then zip that folder. I'm more inclined to make provisions about extracting to a single folder at some other time. Is this possible in BASH, or would I have to consider a faster, more robust scripting language such as python or perl?
View 8 Replies
View Related
Sep 23, 2010
Here's a challenge I've been struggling for months with:
I have a bash script that reads URL addresses of our internal server and then executes some test commands on them. Something like this:
Code:
read -p "Enter URL: " url
sh execute-what-ever-to $url
After copy-pasting the URL the user taps the enter key and the script proceeds, but here comes the tricky part: I want this to work without the need to press the enter key after copy-pasting the URL.
"read -n" does not work in this case, as the URLs vary greatly in length. However, the URLs always end to the same string. They could be like "http://url1/END", "http://url2/END" and so on. So this ending string "END" could be theoretically used to recognize that the whole URL has been pasted.
View 2 Replies
View Related
May 29, 2011
PI'm trying to write a script to list all open ports in the MINIUNPND chain in iptables and use the procotol, port and destination ip to open ports on another router using upnpc.Here is the output of iptables -L MINIUPNPD
Code:
>iptables -L MINIUPNPD
Chain MINIUPNPD (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere 192.168.3.124 tcp dpt:19955
ACCEPT tcp -- anywhere 192.168.3.124 tcp dpt:20054
ACCEPT tcp -- anywhere 192.168.3.130 udp dpt:10654
ACCEPT tcp -- anywhere 192.168.3.121 tcp dpt:29955
code....
No matter what i do i cant seem to remove the first 4 characters from the MYPROT array to leave only the digits. Also i cant seem to read the array back???
I thought it would simply be a loop reading each line and passing the fields in variables, executing upnpc commands i need then moving to the next line of the file until it reached the EOF.
View 12 Replies
View Related