Iterate over a range shell - shell

for a in bar
do
for b in 1000000
do
montage -geometry 500 $a-$b-*-${0..20000..1000}.png \
$a-$b-${0..20000..1000}-final.jpg
done
done
I'm unable to get all the images with number 0 1000 2000 ... 20000 using $(0..20000.1000) .
Is there an other way in shell to do this?

There must be no $ before {START..END..STEP}
% echo -{0..20000..1000}-
-0- -1000- -2000- -3000- -4000- -5000- -6000- -7000- -8000- -9000- -10000- -11000- -12000- -13000- -14000- -15000- -16000- -17000- -18000- -19000- -20000-
That being said, you need a loop to go over these numbers. The word containing a range is just replaced by its expansion. That means the command line is not called for each element alone, but for all of them together. It also means, that, even if you are using the same range twice, their expansion will not conveniently be combined.
Compare
% echo start a-{1..3}-b A-{1..3}-B end
start a-1-b a-2-b a-3-b A-1-B A-2-B A-3-B end
and
% for n in {1..3}; do echo start a-$n-b A-$n-B end; done
start a-1-b A-1-B end
start a-2-b A-2-B end
start a-3-b A-3-B end
So in your example instead of
montage -geometry 500 $a-$b-*-${0..20000..1000}.png \
$a-$b-${0..20000..1000}-final.jpg
you probably want to do
for n in {0..20000..1000}; do
montage -geometry 500 $a-$b-*-$n.png $a-$b-$n-final.jpg
done

#!/usr/bin/env python
import os
file_names= ["a","b"]
ranges = list(xrange(0, 20000, 1000))
l_s = list()
for f in range(0, len(file_names)):
for a in range(0, len(ranges)):
stringing = str(file_names[f]) + "--1000000-0-0-0-1-" + str(ranges[a]) + ".log.png"
l_s.append(stringing)
final = " ".join(l_s)
foo = "montage -geometry 500 " + str(final) + " " + str(file_names[f])+ "-final.jpg"
os.system(foo)

Related

bash set -e causing random exit from for loop

The simplified loop below exits at random times when I use the set -e option. If I remove the set -e option it always completes. I would like to use the set -e option if possible but so far I am at a loss as to why it is exiting and why it happens at random loop iterations each time I run it (try it!). As you can see the only commands are let and echo. Why would the let or echo commands return a non-zero code at random times, or is something else going on?
#!/bin/bash
# Do Release configuration builds so we can set the build parameters
set -e
CFG=Release
for CASE in {0..511}
do
# CASE [0...511] iterate
# MMMM [2...255] random test cases
# NNNN [1..MMMM) random test cases
# RRRR [0...255] random test cases
# XXXX [0...255] random test cases
# DSXX [1...128] random test cases
# OASM [1...255] random test cases
# OLSM [1...255] random test cases
let "MMMM = $RANDOM % 254 + 2"
let "NNNN = $RANDOM % ($MMMM - 1) + 1"
let "RRRR = $RANDOM % 256"
let "XXXX = $RANDOM % 256"
let "DSXX = $RANDOM % 128 + 1"
let "OASM = $RANDOM % 255 + 1"
let "OLSM = $RANDOM % 255 + 1"
echo CFG = $CFG, CASE = $CASE, MMMM = $MMMM, NNNN = $NNNN, RRRR = $RRRR, XXXX = $XXXX, DSXX = $DSXX, OASM = $OASM, and OLSM = $OLSM
# Some other stuff (build and test), that is not causing the problem, goes here
done
# Some other stuff, that is not causing the problem, goes here
exit 0
Append || true to your let commands or use $((...)) for calculations.
From help let:
Exit Status: If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.

Real time process list

I'm trying to make a real time process list at python. I want that every 5 seconds the list i made will be updated. Moreover I want to get cpu time usage for each process but the output is 0 for some reason....
This is a part from the code:
def start(self):
while True:
#-----------------------------------------------------------------------------
processes = []
for process in self.wmi_object.Win32_Process ():
size = int(process.WorkingSetSize) / 1024
try:
p = psutil.Process(process.ProcessId)
cpu_percent = 0# p.cpu_percent(interval=1)
processes.append( process.Name + " " + str(process.ProcessId) + " " + str(size) + "kb" + " " + str(cpu_percent) )
except : pass
self.clientNetwork.send ( pickle.dumps(processes) )
time.sleep(5)
I don't know how to make this a right. Every 5 second the processes just added to the list the previous stay instead of being deleted.
Appriciate help :)

Ruby - How to subtract numbers of two files and save the result in one of them on a specified position?

I have 2 txt files with different strings and numbers in them splitted with ;
Now I need to subtract the
((number on position 2 in file1) - (number on position 25 in file2)) = result
Now I want to replace the (number on position 2 in file1) with the result.
I tried my code below but it only appends the number in the end of the file and its not the result of the calculation which got appended.
def calc
f1 = File.open("./file1.txt", File::RDWR)
f2 = File.open("./file2.txt", File::RDWR)
f1.flock(File::LOCK_EX)
f2.flock(File::LOCK_EX)
f1.each.zip(f2.each).each do |line, line2|
bg = line.split(";").compact.collect(&:strip)
bd = line2.split(";").compact.collect(&:strip)
n = bd[2].to_i - bg[25].to_i
f2.print bd[2] << n
#puts "#{n}" Only for testing
end
f1.flock(File::LOCK_UN)
f2.flock(File::LOCK_UN)
f1.close && f2.close
end
Use something like this:
lines1 = File.readlines('file1.txt').map(&:to_i)
lines2 = File.readlines('file2.txt').map(&:to_i)
result = lines1.zip(lines2).map do |value1, value2| value1 - value2 }
File.write('file1.txt', result.join(?\n))
This code load all files in memory, then calculate result and write it to first file.
FYI: If you want to use your code just save result to other file (i.e. result.txt) and at the end copy it to original file.

UNIX find every line in a text file that contains a string and output last word of these

I have a text file that contains a number of lines that starts with "# Control Point No"
I managed to get an output of only these lines by doing
grep '# Control Point No'
Now I want only to keep the last word of all these line.
The lines look like
"# Control Point No 39217: 1.52520046527084"
So I want to output only the last numbers as 1.52520046527084
and then:
-find lowest value
-find highest value
-calculate average value
All this I want to do is not all included in the post title, sorry
Thanks
Python is your friend:
#!/usr/bin/python
import re, fileinput, sys
numlines = 0
lowest = sys.float_info.max
highest = sys.float_info.min
total = 0.0
for line in fileinput.input():
m = re.match(r'# Control Point No (\d+): (.+)', line)
if m:
value = float(m.group(2))
numlines += 1
if value < lowest:
lowest = value
if value > highest:
highest = value
total += value
print "lowest=", lowest, ", highest=", highest, ", average=", (total / numlines)
$ chmod 0755 procdata.py
$ ./procdata.py < testdata
lowest= 1.0 , highest= 67.9 , average= 7.31550797863

Ruby data extraction from a text file

I have a relatively big text file with blocks of data layered like this:
ANALYSIS OF X SIGNAL, CASE: 1
TUNE X = 0.2561890123390808
Line Frequency Amplitude Phase Error mx my ms p
1 0.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0
2 0.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0
(they contain more lines and then are repeated)
I would like first to extract the numerical value after TUNE X = and output these in a text file. Then I would like to extract the numerical value of LINE FREQUENCY and AMPLITUDE as a pair of values and output to a file.
My question is the following: altough I could make something moreorless working using a simple REGEXP I'm not convinced that it's the right way to do it and I would like some advices or examples of code showing how I can do that efficiently with Ruby.
Generally, (not tested)
toggle=0
File.open("file").each do |line|
if line[/TUNE/]
puts line.split("=",2)[-1].strip
end
if line[/Line Frequency/]
toggle=1
next
end
if toggle
a = line.split
puts "#{a[1]} #{a[2]}"
end
end
go through the file line by line, check for /TUNE/, then split on "=" to get last item.
Do the same for lines containing /Line Frequency/ and set the toggle flag to 1. This signify that the rest of line contains the data you want to get. Since the freq and amplitude are at fields 2 and 3, then split on the lines and get the respective positions. Generally, this is the idea. As for toggling, you might want to set toggle flag to 0 at the next block using a pattern (eg SIGNAL CASE or ANALYSIS)
file = File.open("data.dat")
#tune_x = #frequency = #amplitude = []
file.each_line do |line|
tune_x_scan = line.scan /TUNE X = (\d*\.\d*)/
data_scan = line.scan /(\d*\.\d*E[-|+]\d*)/
#tune_x << tune_x_scan[0] if tune_x_scan
#frequency << data_scan[0] if data_scan
#amplitude << data_scan[0] if data_scan
end
There are lots of ways to do it. This is a simple first pass at it:
text = 'ANALYSIS OF X SIGNAL, CASE: 1
TUNE X = 0.2561890123390808
Line Frequency Amplitude Phase Error mx my ms p
1 0.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0
2 0.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0
ANALYSIS OF X SIGNAL, CASE: 1
TUNE X = 1.2561890123390808
Line Frequency Amplitude Phase Error mx my ms p
1 1.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0
2 1.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0
ANALYSIS OF X SIGNAL, CASE: 1
TUNE X = 2.2561890123390808
Line Frequency Amplitude Phase Error mx my ms p
1 2.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0
2 2.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0
'
require 'stringio'
pretend_file = StringIO.new(text, 'r')
That gives us a StringIO object we can pretend is a file. We can read from it by lines.
I changed the numbers a bit just to make it easier to see that they are being captured in the output.
pretend_file.each_line do |li|
case
when li =~ /^TUNE.+?=\s+(.+)/
print $1.strip, "\n"
when li =~ /^\d+\s+(\S+)\s+(\S+)/
print $1, ' ', $2, "\n"
end
end
For real use you'd want to change the print statements to a file handle: fileh.print
The output looks like:
# >> 0.2561890123390808
# >> 0.2561890123391E+00 0.204316425208E-01
# >> 0.2562865535359E+00 0.288712798671E-01
# >> 1.2561890123390808
# >> 1.2561890123391E+00 0.204316425208E-01
# >> 1.2562865535359E+00 0.288712798671E-01
# >> 2.2561890123390808
# >> 2.2561890123391E+00 0.204316425208E-01
# >> 2.2562865535359E+00 0.288712798671E-01
You can read your file line by line and cut each by number of symbol, for example:
to extract tune x get symbols from
10 till 27 on line 2
to extract LINE FREQUENCY get
symbols from 3 till 22 on line 6+n

Resources