unix shell script to process value stored in a variable - shell

I have the result for the query:
SELECT leg_store_wh_code || ',' || rms_location col1 from SKS_CNV_LOCATION_XREF as
101,101 1,601 202,602 3,603 4,604 207,607 8,608 9,609 10,610 212,612 613,613 14,614 16,616 17,617 18,618 619,619 20,620 21,621 23,623 24,624 85,625 26,626 28,628 29,629 30,630 31,631 32,632 90,633 34,634 635,635 36,636(store_list_result holds this)
I want to use all the values inside the loop. But only the first value is printing.its going to the next values.Can anyone help me
store_list="SELECT leg_store_wh_code || ',' || rms_location col1 from SKS_CNV_LOCATION_XREF ;"
store_list_result=`sqlplus -s $UP <<EOF
SET FEEDBACK OFF
SET HEAD OFF
SET AUTOPRINT OFF
SET LINESIZE 1000
SET TAB OFF
SET ECHO OFF
SET PAGESIZE 0
SET TERMOUT OFF
SET TRIMSPOOL ON
${store_list}
exit
EOF`
for i in store_list_result
do
LEG_ID=`echo $store_list_result | cut -d',' -f1`
echo $LEG_ID
RMS_ID=`echo $store_list_result | cut -d',' -f2 | cut -d' ' -f1`
echo $RMS_ID
result I got is:
101
101

for i in store_list_result do LEG_ID=`echo $store_list_result | cut -d',' -f1`
How is it ever supposed to work? You iterating once ($i="store_list_result", not this variable's value), then you using $store_list_result every time (still once, but it's wrong nevertheless). And format your code properly, grave signs are getting eaten.

Related

Executing sensors command (lm-sensor) in Lua

I originally have this Lua script
function temp_watch()
warn_value=60
crit_value=80
temperature=tonumber(conky_parse("${hwmon 1 temp 1}"))
if cpu_tmp<warn_value then
settings_table[1]['fg_colour']=normal
elseif cpu_tmp<crit_value then
settings_table[1]['fg_colour']=warn
else
settings_table[1]['fg_colour']=crit
end
end
but for some reason, hwmon 1 temp 1 is just stuck reporting 25C. For this reason I switched to sensors. In conky I am executing it using this:
${exec sensors | grep 'Package id 0' | cut -d ' ' -f 5 | cut -c 2,3,4,5,6,7}
I tried using this solution: https://unix.stackexchange.com/questions/666250/how-to-use-conky-variable-with-external-command. Basically, replacing the temperature=tonumber... to
temperature=tonumber(conky_parse("${eval $${exec sensors | grep 'Package id 0' | cut -d ' ' -f 5 | cut -c 2,3,4,5,6,7}}"))
I also tried this: is it possible to pipe output from commandline to lua?. Replaced temperature=tonumber... to
local cpu_tmp = io.popen("exec sensors | grep 'Package id 0' | cut -d ' ' -f 5 | cut -c 2,3,4,5,6,7")
temperature=tonumber(cpu_tmp)
Both outputted this error:
llua_do_call: function conky_main execution failed: /home/joe/conky/conky-grapes/rings-v2_gen.lua:530: attempt to compare nil with number
am I missing some variable conversion or is there any other syntax to execute bash in lua?
Thanks in advance :-)
lua.org gave me an answer. The Complete I/O Model.
local file= io.popen("sensors -u | awk '/temp1_input:/ {print $2; exit}'")
local temperature = tonumber(file:read('*a'))
<SOME CODE HERE>
file:close()
end
It is not elegant but it seems to work. I will be running the lua script for quite some time to ensure it wont have that too many open files error
Many thanks to #Fravadona for pointing me in the right direction :-)

Bash: Two "cut" functions in while-loop, only one effective

in a cygwin environment i want to read a csv line for line, and try to get the values from two columns.
So i have
while read line ; do echo `cut -d";" -f5`; done < allk.lst
and the right values are shown.
But:
while read line ; do echo `cut -d";" -f5`; echo `cut -d";" -f4`; done < allk.lst
again shows the values as before...
Any hints to show both values?
Thanks, Bommel
cut -f accepts a list of fields, so there no need to call cut twice
Using cut command to remove multiple columns
echo cut -d";" -f5; does not do what you'd expect. At first, the variable line is missing.
After applying those fixes, you command would look something like:
while read line; do echo $line | cut -d";" -f4-5 ; done < test.txt
Try a demo online!
Hmmm, thanks at first.
Some curiosity:
When using
$ time while read line; do echo $line | cut -d";" -f4-5 ; done < allk.txt
K700W1666;S728A0103
K700W1651;S727A7570
K700W1654;S727A7579
K700W1657;S727A7581
K700W1660;S727A7582
K700W3040;S728A0099
K700W3043;S728A0107
K700W3042;S732A4280
K700W3594;S724A5213
K700W3600;S727A7609
K700W3603;S727A7615
K700W3597;S727A7617
K700W3604;S727A7589
K700W3624;S728A1599
K700W2164;S728A0091
K700W2165;S728A0110
K700W3565;S727A7577
K700W3568;S727A7578
K700W3560;S725A4806
K700W3563;S725A8285
K700W3559;S726A1925
K700W3562;S728A0197
K700W2016;S726A1929
K700W2012;S725A5172
K700W2015;S728A0056
K700W2014;S728A0061
K700W2017;S728A0067
real 0m12.165s
user 0m0.482s
sys 0m1.390s
it takes 12 seconds, and there is a semicolon between.
Whereas
$ time while read line; do echo `cut -d";" -f4-5` ; done < allk.txt
K700W1651;S727A7570 K700W1654;S727A7579 K700W1657;S727A7581 K700W1660;S727A7582 K700W3040;S728A0099 K700W3043;S728A0107 K700W3042;S732A4280 K700W3594;S724A5213 K700W3600;S727A7609 K700W3603;S727A7615 K700W3597;S727A7617 K700W3604;S727A7589 K700W3624;S728A1599 K700W2164;S728A0091 K700W2165;S728A0110 K700W3565;S727A7577 K700W3568;S727A7578 K700W3560;S725A4806 K700W3563;S725A8285 K700W3559;S726A1925 K700W3562;S728A0197 K700W2016;S726A1929 K700W2012;S725A5172 K700W2015;S728A0056 K700W2014;S728A0061 K700W2017;S728A0067
real 0m0.308s
user 0m0.015s
sys 0m0.030s
takes only 0.3 sec... but also with a semic.
So: what is the best way to read this values in two variables (for building SQL commands)?

Replace value of a specific column from a file

I have file whose size is approx 1 GB and that file has a data in below format .
A|CD|44123|0|0
B|CD|44124|0|0
C|CD|44125|0|0
D|CD|44126|0|0
E|CD|44127|0|0
F|CD|44128|0|0
J|CD|44129|0|0
I|CD|44130|0|0
In this file I have to replace the third column value from a value which i will get after conversion . For which i have to open this file and then read the file and replace it . This process is taking around 5 hours .Below is the code which i am using
cat $FILE_NAME |\
while read REC
do
DATE=`echo "$REC" | cut -d\| -f3`
DATE_NEW=`$UTIL $DATE | head -1 |cut -d" " -f12`
RECORD="$DATE_NEW,"
echo "$RECORD" >> $New_File
done
Is there a way we can make this more better and fast.
Desired output will be like this where DATE_NEW value will be placed on each 3rd column DATE_NEW value will be the converted value which I will get from this
DATE_NEW=`$UTIL $DATE | head -1 |cut -d" " -f12`
A|CD|10/20/2020|0|0
B|CD|10/25/2020|0|0
C|CD|10/25/2020|0|0
D|CD|10/25/2020|0|0
E|CD|11/15/2020|0|0
F|CD|11/14/2020|0|0
J|CD|11/16/2020|0|0
I|CD|11/17/2020|0|0
After the comment from #Sundeep Why is using a shell loop to process text considered bad practice? I wrote the logic in Perl and from 5-7 hours processing time in Perl it took 99 Seconds to get the job done.
Give this a try:
awk -v cmd="Cmd2GetNEWDATE" 'BEGIN{FS=OFS="|"}{cmd|getline v;close(cmd)}$3=v' file

Bash: concatenated variables derived from text file using grep gives confused output

In my directory, I have a multiple nifti files (e.g., WIP944_mp2rage-0.75iso_TR5.nii) from my MRI scanner accompanied by text files (e.g., WIP944_mp2rage-0.75iso_TR5_info.txt) containing information on the acquisition parameters (e.g., "Series description: WIP944_mp2rage-0.75iso_TR5_INV1_PHS_ND"). Based on these parameters (e.g., INV1_PHS_ND), I need to change the nifti file name, which are echoed in $niftibase. I used grep to do this. When echoing all variables individually, it gives me what I want, but when I try to concatenate them into one filename, the variables are mixed together, instead of delimited by a dot.
I tried multiple forms of sed to cut away potentially invisible characters and identified the source of the problems: the "INV1_PHS_ND" part of 'series description' gives me troubles, which is the $struct component, potentially due to the fact that this part varies in how many fields are extracted. Sometimes this is 3 (in the case of INV1_PHS_ND), but it can be 2 as well (INV1_ND). When I introduce this variable into the filename, everything goes haywire.
for infofile in ${PWD}/*.txt; do
# General characteristics of subjects (i.e., date of session, group number, and subject number)
reco=$(grep -A0 "Series description:" ${infofile} | cut -d ' ' -f 3 | cut -d '_' -f 1)
date=$(grep -A0 "Series date:" ${infofile} | cut -c 16-21)
group=$(grep -A0 "Subject:" ${infofile} | cut -d '^' -f 2 | cut -d '_' -f 1 )
number=$(grep -A0 "Subject:" ${infofile} | cut -d '^' -f 2 | cut -d '_' -f 2)
ScanNr=$(grep -A0 "Series number:" ${infofile} | cut -d ' ' -f 3)
# Change name if reco has structural prefix
if [[ $reco = *WIP944* ]]; then
struct=$(grep -A0 "Series description: WIP944" ${infofile} | cut -d '_' -f 4,5,6)
niftibase=$(basename $infofile _info.txt).nii
#echo ${subStudy}.struct.${date}.${group}.${protocol}.${paradigm}.nii
echo ${subStudy}.struct.${struct}.${date}.${group}.${protocol}${number}.${paradigm}.n${ScanNr}.nii
#mv ${niftibase} ${subStudy}.struct.${struct}.${date}.${group}.${protocol}${number}.${paradigm}.n${ScanNr}.nii
fi
done
This gives me output like this:
.niit47.n4lot.Noc002
.niit47.n5lot.Noc002D
.niit47.n6lot.Noc002
.niit47.n8lot.Noc002
.niit47.n9lot.Noc002
.niit47.n10ot.Noc002
.niit47.n11ot.Noc002D
for all 7 WIP944 files. However, it needs to be in the direction of this:
H1.struct.INV2_PHS_ND.190523.Pilot.Noc001.Heat47.n11.nii, where H1, Noc, and Heat47 are loaded in from a setup file.
EDIT: I tried to use awk in the following way:
reco=$(awk 'FNR==8 {print;exit}' $infofile | cut -d ' ' -f 3 | cut -d '_' -f 1)
date=$(awk 'FNR==2 {print;exit}' $infofile | cut -c 15-21)
group=$(awk 'FNR==6 {print;exit}' $infofile | cut -d '^' -f 2 | cut -d '_' -f 1 )
number=$(awk 'FNR==6 {print;exit}' $infofile | cut -d '^' -f 2 | cut -d '_' -f 2)
ScanNr=$(awk 'FNR==14 {print;exit}' $infofile | cut -d ' ' -f 3)
which again gave me the correct output when echoing the variables individually, but not when I tried to combine them: .niit47.n11022_PHS_ND.
I used echo "$struct" | tr -dc '[:print:]' | od -c to see if there were hidden characters due to line endings, which resulted in:
0000000 I N V 2 _ P H S _ N D
0000013
EDIT: This is how the text file looks like:
Series UID: 1.3.12.2.1107.5.2.34.18923.2019052316005066316714852.0.0.0
Study date: 20190523
Study time: 153529.718000
Series date: 20190523
Series time: 160111.750000
Subject: MDC-0153,pilot_003^pilot_003
Subject birth date: 19970226
Series description: WIP944_mp2rage-0.75iso_TR5_INV1_PHS_ND
Image type: ORIGINAL\PRIMARY\P\ND
Manufacturer: SIEMENS
Model name: Investigational_Device_7T
Software version: syngo MR B17
Study id: 1
Series number: 5
Repetition time (ms): 5000
Echo time[1] (ms): 2.51
Inversion time (ms): 900
Flip angle: 7
Number of averages: 1
Slice thickness (mm): 0.75
Slice spacing (mm):
Image columns: 320
Image rows: 320
Phase encoding direction: ROW
Voxel size x (mm): 0.75
Voxel size y (mm): 0.75
Number of volumes: 1
Number of slices: 240
Number of files: 240
Number of frames: 0
Slice duration (ms) : 0
Orientation: sag
PixelBandwidth: 248
I have one of these for each nifti file. subStudy is hardcoded in a setup file, which is loaded in prior to running the for loop. When I echo this, it shows the correct value. I need to change the names of multiple files with a specific prefix, which are stored in $reco.
As confirmed in comments, the input files have DOS carriage returns, which are basically invalid in Unix files. Also, you should pay attention to proper quoting.
As a general overhaul, I would recommend replacing the entire Bash script with a simple Awk script, which is both simpler and more idiomatic.
for infofile in ./*.txt; do # no need to use $(PWD)
# Pre-filter with a simple grep
grep -q '^Series description: [^ _]*WIP944' "$infofile" && continue
# Still here? Means we want to rename
suffix="$(awk -F : '
BEGIN { split("Series description:Series date:Subject:Series number", f, /:/) }
{ sub(/\r/, ""); } # get rid of pesky DOS carriage return
NR == 1 { nifbase = FILENAME; sub(/_info\.txt$/, ".nii", nifbase) }
$1 in f { x[$1] = substring($0, length($1)+2) }
END {
split(x["Series description"], t, /_/); struct=t[4] "_" t[5] "_" t[6]
split(x["Series description"], t, /_/); reco = t[1]
date=substr(x["Series date"], 16, 5)
split(x["Subject"], t, /\^/); split(t[2], tt, /_/); group=tt[1]
number=tt[2]
ScanNr=x["Series number"]
### FIXME: protocol and paradigm are still undefined
print struct "." date "." group "." protocol number "." paradigm ".n" ScanNr
}' "$infofile")"
echo mv "$infofile" "$subStudy.struct.$suffix"
done
This probably still requires some tweaking (at least "protocol" and "paradigm" are still undefined). Once it seems to print the correct values, you can remove the echo before mv and have it actually rename files for you.
(Probably still better test on a copy of your real data files first!)

unix find the difference from a file row wise

I have some data like
[09359]0000.365604| =>SttSasph_Hmbm_bSPO_PhQmOm (Hmbm_PhQmOm_utWmP.asp)
[09359]0000.365687| =>Hmbm_bSPO_PhQmOm_Wd (Hmbm_PhQmOm_utWmP.asp)
[09359]0000.365879| =>SttSasph_Hmbm_quOuO_PhQmOm (Hmbm_PhQmOm_utWmP.asp)
[09359]0000.365890| =>Hmbm_quOuO_PhQmOm_Wd (Hmbm_PhQmOm_utWmP.asp)
[09359]0000.365979| WSmmOT SDDQ vSQWSbmO not POt, QOvOQtWnH to Onv mOthod
[09359]0001.625300| db_HOt_POPPWon_Wd: aspuQQOnt POPPWon WD WP 1016,59
[09359]0002.365979| WSmmOT SDDQ vSQWSbmO not POt, QOvOQtWnH to Onv mOthod
Every Line starts with a process number (Which can change) in square brackets
Then Seconds after the module (0001) in this case
Then MicroSeconds after the fullstop.
Then a Pipe to terminate.
Rest part can be ignored
What I need is to caluclate
Convert Seconds into MircoSeconds
Add the Microsconds to Converted Microseconds (From 1)
Find out the difference in microseconds. for eg. line2-line1 , line3-line2, line4- line3 and so.
Print the result in seperate file.
I tried to use this logic. But, it didnt work.
May I get suggestions with optimised way to do it or
improvement in my existing logic
sec=$(grep '^\[.\{1,\}\]' mass.May28.1 | cut -d "| " -f1 | cut -c8- | cut -d"." -f1)
msec=$(grep '^\[.\{1,\}\]' mass.May28.1 | cut -d "| " -f1 | cut -c8- | cut -d"." -f2)
$f_msec=$((sec * 1000000 + msec)) > final_difference_file
If you are comfortable with awk, then you can use this script:
script.awk
BEGIN{ FS="[\\[\\]\\|]+" }
{ printf("[%s]%011.6f|%s\n", $2,$3-prev,$4)
prev = $3 }
Use it like this: awk -f script.awk yourfile
The first line setups the fieldsplitting to use the brackets and pipe (ignore the backslashes they are need to escape the symbols that are regexp metacharacters). The second line prints the fields and calculates the timediff. The last line stores the current time for the calculation in the next line.
This can also be done with a bash script. Since bash lacks floating point arithmetic, we have to gather seconds and microseconds seperately (or call an external tool like bc for each line):
script.sh
IFS='|[].'
factor=1000000
prev=0
while read dummy pid secs msecs text;
do
msecs=$(( $secs * $factor + $msecs ))
timediff=$(( $msecs - $prev ))
prev=$msecs
secs=$(( $timediff / $factor ))
msecs=$(( $timediff - $secs * $factor ))
printf "[%s]%04d.%06d|%s\n" "$pid" "$secs" "$msecs" "$text"
done
Use it like this: bash script.sh yourfile

Resources