hi i am using a bash script.
here i execute this code.
/usr/bin/mxci <<EOF
SELECT substring(substring(tname from (1+locate('.',tname))),
(1+locate('.',substring(tname from (1+locate('.',tname))))),15 ) as TAB_NAME
FROM table(explain('/home/vivek/MFCABS1031VCAT.VSCH.A9B69395AE3238A4184A90CD1F28C161', '%'))
WHERE OPERATOR LIKE '%FILE_SCAN%';
EOF'
This will return a output as
>>SELECT substring(substring(tname from (1+locate('.',tname))),
+>(1+locate('.',substring(tname from (1+locate('.',tname))))),15 ) as TAB_NAME
+>FROM table(explain('/home/vivek/MFCABS1031VCAT.VSCH.A9B69395AE3238A4184A90CD1F28C161', '%'))
+>WHERE OPERATOR LIKE '%FILE_SCAN%';
TAB_NAME
---------------
TEST1
--- 1 row(s) selected.
>>exit;
Now i know how to store this entire output into a single variable and print it But i am not able to figure out how to store partial information into variable.
Like if i want to store only TEST1 into the variable, what should i do.
Thanks in advance
The output of the varaible Test
/home/vivek: echo "$test"
Hewlett Packard Enterprise NonStop(TM) SQL/MX Conversational Interface 3.7
(c) Copyright 2003-2019 Hewlett Packard Enterprise Development LP.
>>SELECT substring(substring(tname from (1+locate('.',tname))),
+>(1+locate('.',substring(tname from (1+locate('.',tname))))),15 ) as TAB_NAME
+>FROM table(explain('/home/vivek/MFCABS1031VCAT.VSCH.A9B69395AE3238A4184A90CD1F28C161', '%'))
+>WHERE OPERATOR LIKE '%FILE_SCAN%';
TAB_NAME
---------------
TEST1
--- 1 row(s) selected.
>>exit;
End of MXCI Session
/home/vivek:
You can pipe your output to this awk:
your_cmd | awk '/^TAB_NAME$/{n=NR} n && NR==n+3{print; exit}'
TEST1
This awk looks for a line that is equal to TAB_NAMES and stores that line no. Following which it prints a line whose line no is stored no + 3.
To store this shown value in a variable use:
myvar=$(your_cmd | awk '/^TAB_NAME$/{n=NR} n && NR==n+3{print; exit}')
Related
I'm building a script in bash for use on Linux, and I use the output of a executable to fill parameters:
version=$("${path_exec}" -version | awk '{if($1=="kernel" && $2=="release") print $3}')
patch=$("${path_exec}" -version | awk '{if($1=="patch" && $2=="number") print $3}')
This will run the executable defined in "path_exec" twice, which is time consuming. Is there a way to assign the version and path variable with a value using only one execution of "path_exec"?
An example of what I've tried to tackle this is shown below, but I don't think this will do what I want:
${path_hostexec} -version | awk '{if($1=="kernel" && $2=="release") {version_agent = $3;} else if($1=="patch" && $2=="number") {patch_agent = $3;}}'
Could you please try following. Since I didn't have output of path_exec command so couldn't test it.
myarr=($("${path_exec}" -version | awk '{if($1=="kernel" && $2=="release");val=$3} {if($1=="patch" && $2=="number") print val,$3}'))
#Now get every element in the array
for i in "${myarr[#]}"
do
echo $i
done
What I have done is:
Merged your both awk programs into one to make it run on a single time.
Now creating an array by output of awk command output(which should be val1 val2 as an example format)
Once an array created then we could get its all values by for loop or you could get its specific value by mentioning its index eg--> myarr[1] to print 2nd element.
Output both values on a single line, and let read separate the line into its two parts.
IFS=, read version patch < <($path_exec -version |
awk '/kernel release/ {v=$3}
/patch number/ {p=$3}
END {print v","p}
')
Thanks guys, I've managed to get it working thanks to your input:
version_patch_agent=$("${path_hostexec}" -version | awk '/kernel release/ {v=$3} /patch number/ {p=$3} END {print v" patch "p}')
This puts the version and patch number into a variable that I can just echo to get the info on the screen. Thanks again all!!
In the below script the output file is missing the header record.
Some data from the PTF_LIST is also missing in the output
The same script if run with one value in the PTF_LIST
e.g PTF_LIST="LIST1" produces the header record and the data for the list in the output
The script when run with multiple values for PTT_LIST e.g PTF_LIST="LIST1 LIST2 LIST3" does not produce the desired output.The header record is missing and data for the some list values randomly does not appear in the output file.
Only a single instance of the script is run at a time.
#!/bin/ksh
OUTPUT_FILE=report_output.csv
WRKFILE=temp_ouput.tmp
# Header record.
print "column1, column2, column3" > ${OUTPUT_FILE}
PTF_LIST="LIST1 LIST2 LIST3 LIST4 LIST5 LIST6 LIST7"
for PTF_NO in $PTF_LIST
do
/usr/bin/isql -b -s',' -U$USER -P$USERPWD <<-EOT | sed -e 's/ *,/,/g' >${WRKFILE} 2>&1
use aaamaindb
go
set nocount on
set dateformat ymd
go
exec some_function #port_object='${PTF_NO}'
go
EOT
awk -F',' '{ if ( $5 == "YES" ) { print $1","$2","$3","$8","$9","$10","$11","$12","$13}}' < ${WRKFILE} >> ${OUTPUT_FILE}
done #End of for loop
thanks for your comments and answers.
My input.csv file is semicolon separated, with the first line being a header for attributes. The first column contains customer numbers. The function is being called through a script that I activate from the terminal.
I want to delete all lines containing the customer numbers that are entered as arguments for the script. EDIT: And then export the file as a different file, while keeping the original intact.
bash deleteCustomers.sh 1 3 5
Currently only the last argument is filtered from the csv file. I understand that this is happening because the output file gets overwritten each time the loop runs, restoring all previously deleted arguments.
How can I match all the lines to be deleted, and then delete them (or print everything BUT those lines), and then output it to one file containing ALL edits?
delete_customers () {
echo "These customers will be deleted: "$#""
for i in "$#";
do
awk -F ";" -v customerNR=$i -v input="$inputFile" '($1 != customerNR) NR > 1 { print }' "input.csv" > output.csv
done
}
delete_customers "$#"
Here's some sample input (first piece of code is the first line in the csv file). In the output CSV file I want the same formatting, with the lines for some customers completely deleted.
Klantnummer;Nationaliteit;Geslacht;Title;Voornaam;MiddleInitial;Achternaam;Adres;Stad;Provincie;Provincie-voluit;Postcode;Land;Land-voluit;email;gebruikersnaam;wachtwoord;Collectief ;label;ingangsdatum;pakket;aanvullende verzekering;status;saldo;geboortedatum
1;Dutch;female;Ms.;Josanne;S;van der Rijst;Bliek 189;Hellevoetsluis;ZH;Zuid-Holland;3225 XC;NL;Netherlands;JosannevanderRijst#dayrep.com;Sourawaspen;Lae0phaxee;Klant;CZ;11-7-2010;best;tand1;verleden;-137;30-12-1995
2;Dutch;female;Mrs.;Inci;K;du Bois;Castorweg 173;Hengelo;OV;Overijssel;7557 KL;NL;Netherlands;InciduBois#gustr.com;Hisfireeness;jee0zeiChoh;Klant;CZ;30-8-2015;goed ;geen;verleden;188;1-8-1960
3;Dutch;female;Mrs.;Lusanne;G;Hijlkema;Plutostraat 198;Den Haag;ZH;Zuid-Holland;2516 AL;NL;Netherlands;LusanneHijlkema#dayrep.com;Digum1969;eiTeThun6th;Klant;Achmea;12-2-2010;best;mix;huidig;-335;9-3-1973
4;Dutch;female;Dr.;Husna;M;Hoegee;Tiendweg 89;Ameide;ZH;Zuid-Holland;4233 VW;NL;Netherlands;HusnaHoegee#fleckens.hu;Hatimon;goe5OhS4t;Klant;VGZ;9-8-2015;goed ;gezin;huidig;144;12-8-1962
5;Dutch;male;Mr.;Sieds;D;Verspeek;Willem Albert Scholtenstraat 38;Groningen;GR;Groningen;9711 XA;NL;Netherlands;SiedsVerspeek#armyspy.com;Thade1947;Taexiet9zo;Intern;CZ;17-2-2004;beter;geen;verleden;-49;12-10-1961
6;Dutch;female;Ms.;Nazmiye;R;van Spronsen;Noorderbreedte 180;Amsterdam;NH;Noord-Holland;1034 PK;NL;Netherlands;NazmiyevanSpronsen#jourrapide.com;Whinsed;Oz9ailei;Intern;VGZ;17-6-2003;beter;mix;huidig;178;8-3-1974
7;Dutch;female;Ms.;Livia;X;Breukers;Everlaan 182;Veenendaal;UT;Utrecht;3903
Try this in loop..
awk -v variable=$var '$1 != variable' input.csv
awk - to make decision based on columns
-v - to use a variable into a awk command
variable - store the value for awk to process
$var - to search for a specific string in run-time
!= - to check if not exist
input.csv - your input file
It's awk's behavior, when you use -v it can will work with variable on run-time and provide an output that doesn't contain the value you passed. This way, you get all the values that are not matching to your variable. Hope this is helpful. :)
Thanks
This bash script should work:
!/bin/bash
FILTER="!/(^"$(echo "$#" | sed -e "s/ /\|^/g")")/ {print}"
awk "$FILTER" input.csv > output.csv
The idea is to build an awk relevant FILTER and then use it.
Assuming the call parameters are: 1 2 3, the filter will be: !/(^1|^2|^3)/ {print}
!: to invert matching
^: Beginning of the line
The input data are in the input.csv file and output result will be in the output.csv file.
I am working on shellscript with excel sheet. Till now I have done as shown in screenshot by using below command:
bash execution.sh BehatIPOP.xls| awk '/Script|scenario/' | awk 'BEGIN{print "Title\tResult"}1' | awk '0 == NR%2{printf "%s",$0;next;}1' >> BehatIPOP.xls
My requirement is along with the heading Result I want to add(concat) current date also. So I am getting date by using below command:
$(date +"%d-%m-%y %H:%M:%S")
So date will display like this : 25-08-2016 17:00:00
But I am not getting how can use date command in the above mentioned command to achieve heading like below:
| Title | Result # 25-08-2016 17:00:00|
Thanks for any suggestions..
You can pick up the date inside awk and store it in a variable d like this, if that is what you mean:
awk 'BEGIN{cmd="date +\"%d-%m-%y %H:%M:%S\""; cmd |getline d; close(cmd);print "Result # " d}'
Result # 25-08-16 13:44:05
Don't use awk at all for the header, just use date directly:
{ printf "Title\tResult # "; date +"%d-%m-%y %H:%M:%S"; bash execution.sh BehatIPOP.xls |
awk '/Script|scenario/' |
awk '1 == NR%2{printf "%s",$0;next;}1'; } >> BehatIPOP.xls
Note that there's no need for 2 awks, but I'm keeping that here to minimize the diff. Since I've pulled the header out of the awk, the comparison changes from 0==NR%2 to 1==NR%2.
I have a tab delimited txt file that looks like this:
1. C1 34 98
2. C3 2 45
How can I make a batch script (linux) that will
extract the second data in the first line to variable 1
extract the third data in the first line to variable 2
extract the fourth data in the first line to variable 3
then
run a series of scripts with parmeters defined to variable 1,2 and 3
e.g.
script $1 $2 $3 > path/file$1-$2-$3
The script should use the values of variables as parameters and than write out the results to a file named according to the values of the variables, thus each cycle would result in a new file)
Finish the loop when all lines are used up from the tab limited txt file.
I am not a programmer...
This can be done in the shell alone(see below, assuming default value for IFS)
while read -r _ x y z ;
do
echo "$x" "$y" "$z";
done < input.txt
Assuming you're using sh/bash/ksh, the shell gives you what you need:
while read dummy v1 v2 v3 dummy
do
echo $v1 $v2 $v3
./dostuff $v1 $v2 $v3
done < inputFile
How a line is tokenised depends on the IFS variable, which by default consists of a tab, a space and a newline. You can change this, but you must manage its contents carefully as it's easy to break a script by not restoring IFS back to it's default values.
So what we're doing here is reading the file inputFile and splitting into five fields, dummy, v1, v2, v3, and dummy. These could have just as easily been called a, b, c, d, e but calling the fields we want to junk dummy it's obvious what the intention is.
If you know that the file will only ever have four fields, then the final dummy isn't needed (i.e., first line can be while read dummy v1 v2 v3); in essence the last field in the while read [...] sucks up the rest of the line, so if the input was 1. 34 45 12 67 65 then without the final dummy variable v3 would contain 12 67 65. With it, v3 becomes 12 with the rest of the line being read into dummy. This'll make sense if you experiment with it :-)
while read -r line
do
var1=$(echo $line | awk '{print $2}')
var2=$(echo $line | awk '{print $3}')
var3=$(echo $line | awk '{print $4}')
echo $var1 $var2 $var3
./yourscript.sh $var1 $var2 $var3
# Do other stuff with these variables.
done < file