I would require some help in inserting data from CSV file to CUSTOMER table using Shell script. Is it possible to retrieve the CSV data by comma instead of the fixed position of the data? For example, the data for my Remarks does not have a fixed position, i.e. it could contain 10 characters or 15 characters, hence the variable changes.
#!/bin/bash
PASSFILE=/credentials/systemcredential.properties
USERID=$(cat $PASSFILE | grep UserID | cut -f2 -d=)
PASSWORD=$(cat $PASSFILE | grep Pwd | cut -f2 -d=)
# connect to database
source /opt/db2home/db2profile
db2 connect to DBRPTGU user $USERID using $PASSWORD
#--------------------------------------------------#
# TABLE: CUSTOMER
#--------------------------------------------------#
#db2 "select count(*) from udbcuser.CUSTOMER"
db2 "load from /batchload/data/CUSTOMER.csv of asc
method L(1 7, 9 23, 25 39, 41 47, 49 68)
insert_update into udbcuser.CUSTOMER(CUSTOMER_ID,CUSTOMER_NAME,ITEM_PURCHASED,AMOUNT_PURCHASED,REMARKS)"
Sample data as requested:
9000001,Michael Tan,Wallet,$30,First time customer
9000002,Sally Gomez,Jacket,$90,
9000003,Cheng Ning,Boots,$80,Member
9000004,Richard Chin,Sunglasses,$30,Member
Thank you!
Here is a sample script.
echo '9000001,Michael Tan,Wallet,$30,First time customer' > dat.csv
echo '9000002,Sally Gomez,Jacket,$90,' >> dat.csv
echo '9000003,Cheng Ning,Boots,$80,Member' >> dat.csv
echo '9000004,Richard Chin,Sunglasses,$30,Member' >> dat.csv
db2 -v drop db db1
db2 -v create db db1
db2 -v connect to db1
db2 -v "create table db2inst1.customer (num int, name char (30), item char (20), price char(10), remark char(20))"
db2 -v "load from dat.csv of del insert into db2inst1.customer"
db2 -v "select * from db2inst1.customer"
When you run, the last select * from db2inst1.customer returns as:
NUM NAME ITEM PRICE REMARK
----------- ------------------------------ -------------------- ---------- --------------------
9000001 Michael Tan Wallet $30 First time customer
9000002 Sally Gomez Jacket $90 -
9000003 Cheng Ning Boots $80 Member
9000004 Richard Chin Sunglasses $30 Member
4 record(s) selected.
There is a lot of options for load command and here is V11.5 manual page for your reference:
LOAD command
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0008305.html
Hope this helps.
Related
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}')
I am trying to write a script that can be run on my FreeNas (FreeBSD) box, that connects to an ESXi host via SSH and gracefully shuts down VMs. What I need to run for a list of VM IDs is:
vim-cmd vmsvc/power.shutdown VMID
Am after some assistance in filtering the output of the commands used to retrieve the IDs, and then passing it to the shutdown command.
The command to retrieve all VMs is:
vim-cmd vmsvc/getallvms
It outputs data like this:
Vmid Name File Guest OS Version Annotation
12 Eds-LINUX [Eds-FS-Datastore-1] Eds-LINUX/Eds-LINUX.vmx ubuntu64Guest vmx-13
13 Eds-RT [Eds-FS-Datastore-1] Eds-RT/Eds-RT.vmx freebsd64Guest vmx-13
14 Eds-DC [Eds-FS-Datastore-1] Eds-DC/Eds-DC.vmx windows9Server64Guest vmx-13
15 Eds-STEAM [Eds-FS-Datastore-1] Eds-STEAM/Eds-STEAM.vmx windows9_64Guest vmx-13
16 Eds-DL [Eds-FS-Datastore-1] Eds-DL/Eds-DL.vmx windows9Server64Guest vmx-13
17 Eds-RD [Eds-FS-Datastore-1] Eds-RD/Eds-RD.vmx windows9Server64Guest vmx-13
18 Eds-PLEX [Eds-FS-Datastore-1] Eds-PLEX/Eds-PLEX.vmx windows9Server64Guest vmx-13
19 Eds-MC [Eds-FS-Datastore-1] Eds-MC/Eds-MC.vmx windows9Server64Guest vmx-13
2 Eds-FS [Eds-ESXi-Datastore-1] Eds-FS/Eds-FS.vmx freebsd64Guest vmx-13
I have determined I can use a pipe into sed, to delete the first line, using:
vim-cmd vmsvc/getallvms | sed '1d'
I am then able to retrieve the ID of the VM I want to filter out, by using:
vim-cmd vmsvc/getallvms | awk '/Eds-FS.vmx/{print$1}'
This gives me the ID of 2. I am unclear however, how to store this in a variable for later use.
I need to know of a way to select just the first column from this data, and for each ID in the list, put it in an array. I then need to loop through the array and for each ID, run the below to get the power state of the VM:
vim-cmd vmsvc/power.getstate VMID
This outputs data like this, with a status of either power on or off:
Retrieved runtime info
Powered on
For each one that is found to be powered on, I need to store the VM ID in a second array to later pass to the shutdown command, except for where the ID is equal to that of the VM I want to power off.
Thanks to anubhava who gave me enough assistance to get something working (although probably not following standards or best practices)
I have this script saved on my ESXi host, which I connect to with SSH and trigger a run of:
freenasid=`vim-cmd vmsvc/getallvms | sed '1d' | awk '/Eds-FS.vmx/{print$1}'`
vmids=`vim-cmd vmsvc/getallvms | sed '1d' | awk '{print$1}'`
for vmid in $vmids
do
if [ $vmid != $freenasid ]
then
powerstate=`vim-cmd vmsvc/power.getstate $vmid | sed '1d'`
if [ "$powerstate" = "Powered on" ]
then
onvmids="$onvmids $vmid"
fi
fi
done
for vmid in $onvmids
do
vim-cmd vmsvc/power.shutdown $vmid
done
exit 0
This correctly shutsdown all running VMs.
To list IDs from first column use awk like this:
vim-cmd vmsvc/getallvms | awk 'NR>1{print $1}'
To store IDs in a shell array use:
readarray -t arr < <(vim-cmd vmsvc/getallvms | awk 'NR>1{print $1}')
To loop through array and run another command:
for id in "${arr[#]}"; do
vim-cmd vmsvc/power.getstate "$id"
done
To store one particular id use command substitution:
vmid1=$(vim-cmd vmsvc/getallvms | awk '/Eds-FS\.vmx/{print$1}')
There are supposed to be 2 files. One file has list of users and the other files has list of passwords.
User file has 5 users and password file also has 5 passwords. What I have to do here is create a new file and pair each user with all the available passwords in password file. For example: user 1 is to be paired with password 1, 2,3,4,5. user 2 is to be paired with password 1,2,3,4,5 and so on.
My question is:
1. What loop should be used here?
2. My head says for pairing we need to use nested for loop. is it correct?
3. I can somehow imagine the 1st part of copying the contents but I'm not able to picture how to pair them. So I need help and suggestions.
EDIT:
Sample input are the 2 files named Username and Password.
Username file:
User1
User2
..
User5
Password file:
Pass1
Pass2
..
Pass5
Expected output is:
User1-Pass1
User1-Pass2
..
User1-Pass5
User2-Pass1
User2-Pass2
..
User2-Pass5
and so on till we reach User5-Pass5
Thanks.
Ad 1., 2. Yes, nested loop required.
Let's expect one user per line and one password per line, then code would be:
> concat # delete file content
for usr in `cat file_with_users`
do
for pwd in `cat file_with_passwords`
do
echo "$usr-$pwd" >> result_file
done
done
If you write input sample and expected output, I can write something more specific.
always there is a better way
$ join -t, -j 99 users pwords | cut -d, -f2-
for example
$ echo u{1..5} | tr ' ' '\n' > users
$ echo p{1..5} | tr ' ' '\n' > pwords
$ join -t, -j 99 users pwords | cut -d, -f2-
u1,p1
u1,p2
u1,p3
u1,p4
u1,p5
u2,p1
u2,p2
...
u5,p4
u5,p5
for - delimiter change to join -t- -j 99 users pwords | cut -d- -f2-
This is using a fictional column (99th) to create a join between each rows, which is called cross-product. Since the key is missing in the first position of the output we need to cut it out at the end.
Do either of these help:
grep -Fwf file1 file2 or
awk 'NR==FNR{A[$1];next}$1 in A' file1 file2
I have a log.text file with this structure:
user session login_time application database db_connect_time request request_time connection_source connection_ip request_state
+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------
admin 0 9 0 none 0 Not Requested* a00:bf32::
admin 989855740 1335 DRRDEVMH DRRPRODB 1201 none 0 Not Requested a00:8a45::
admin 1768947706 932 test test 916 none 0 Not Requested a00:94b6::
WARNING - 1241024 - Possible string truncation in column 1.
WARNING - 1241028 - Output column defined with warnings.
WARNING - 1241024 - Possible string truncation in column 9.
WARNING - 1241028 - Output column defined with warnings.
WARNING - 1241024 - Possible string truncation in column 10.
WARNING - 1241028 - Output column defined with warnings.
OK/INFO - 1241044 - Records returned: [3].
As we can see in the last line of log.txt there is a string Records returned: [3]. That digit 3 is my target, extracting that digit (as 3 in this case) I want to print following line in a separate file.
The total records returned = 3
I am using:
sed -n 's#^.*Records returned.*[\(.*\)$#\1#p' log.txt > out.txt
but its not giving the result. What mistake I am making here, please ?
you need to escape [, try this one
sed -n 's#^.*Records returned.*\[\(.*\)\].*$#\1#p' log.txt > out.txt
Edit
If you want to print out the string like this
The total records returned = 3
just prepend The total records returned = before \1, so the script will be
sed -n 's#^.*Records returned.*\[\(.*\)\].*$#The total records returned = \1#p' log.txt > out.txt
Using awk
awk -F "[][]" '$0~t {print "The total",t,"=",$2}' t="Records returned" log.txt > out.txt
cat out.txt
The total Records returned = 3
sed -n '$ s/.*\([[:digit:]]\{1,\}\)].$/The total records returned = \1/p'
Assuming, as your sample and explaination state, that info is on the last line with this format.
Suppose Your Data in a Test.txt file then You can simply use below Command
echo "Total Records Count = `cat Test.txt | tail -n 1 | cut -d '[' -f2 | cut -d ']' -f1` "
Total Records Count = 3
I'm trying to get psql to format nicely and am following the docs here. Right now, whenever I do a query on tables with lots of columns, no matter how big I make my screen each line overflows into the next line and producing a whole screen of unreadable junk.
The docs (link is above) say there's a way to align columns nicely for more readable output.
Normally, to start psql, I just type:
psql
and hit Enter. Now I'm trying:
psql \pset format aligned
And getting an error:
could not change directory to "/root"
psql: warning: extra command-line argument "aligned" ingored
psql: FATAL: Indent authentication failed for user "format"
Any ideas as to how I could get these command-line args to work for me?
These are not command line args. Run psql. Manage to log into database (so pass the hostname, port, user and database if needed). And then write it in the psql program.
Example (below are two commands, write the first one, press enter, wait for psql to login, write the second):
psql -h host -p 5900 -U username database
\pset format aligned
Use \x
Example from postgres manual:
postgres=# \x
postgres=# SELECT * FROM pg_stat_statements ORDER BY total_time DESC LIMIT 3;
-[ RECORD 1 ]------------------------------------------------------------
userid | 10
dbid | 63781
query | UPDATE branches SET bbalance = bbalance + $1 WHERE bid = $2;
calls | 3000
total_time | 20.716706
rows | 3000
-[ RECORD 2 ]------------------------------------------------------------
userid | 10
dbid | 63781
query | UPDATE tellers SET tbalance = tbalance + $1 WHERE tid = $2;
calls | 3000
total_time | 17.1107649999999
rows | 3000
-[ RECORD 3 ]------------------------------------------------------------
userid | 10
dbid | 63781
query | UPDATE accounts SET abalance = abalance + $1 WHERE aid = $2;
calls | 3000
total_time | 0.645601
rows | 3000
psql --pset=format=FORMAT
Great for executing queries from command line, e.g.
psql --pset=format=unaligned -c "select bandanavalue from bandana where bandanakey = 'atlassian.confluence.settings';"