I Have a problem with a psql query in bash.
I really don't know why the PSQL understands the value HUB is a Column.
psql -q -A -h Some_host
-U User -d datashema -p 1111 -t -f query.txt -c 'SELECT id, text FROM great_201704 WHERE id = 10 and
text = 'HUB' ;'
ERROR: column "hub" does not exist in great_201704
You read your single quotes as if they nest:
-c 'SELECT id, text FROM great_201704 WHERE id = 10 and text = 'HUB' ;'
^---------------------------------1--------------------------------^
^-2-^
Bash reads them as two single quoted string with a literal between them:
-c 'SELECT id, text FROM great_201704 WHERE id = 10 and text = 'HUB' ;'
^------------------------------1----------------------------^
^2-^
This is equivalent to not having single quotes around HUB, which is why psql thinks its a column.
The easiest way to embed one set of quotes in another string is to just use two different types of quotes:
psql -q -A -h Some_host -U User -d datashema -p 1111 -t -f query.txt \
-c "SELECT id, text FROM great_201704 WHERE id = 10 and text = 'HUB' ;"
Related
I have a list of groups and I need to extract users by knowing partially memberOf value
Example:
# for group AAA
ldapsearch -w V1ZEYK -D "cn=XXXXXX,ou=Service Users,ou=User Accounts,dc=uuu,dc=yyy,dc=xxx,dc=net" -H ldaps://<link>:<port> -b "dc=uuu,dc=yyy,dc=xxx,dc=net" -s sub memberOf="CN=AAA,OU=Groups,DC=uuu,DC=yyy,DC=xxx,DC=net" | grep "cn:"
# returns "cn: 12345"
# for group BBB
... -s sub memberOf="CN=BBB,DC=uuu,DC=yyy,DC=xxx,DC=net" | grep "cn:"
# returns nothing, meaning memberOf DC part is different that I dont know of
How should I pass partial filter so the search could return user cns?
Is there a way (and should I) pass wildcard filters for flags -D and -b?
Tried:
... -s sub memberOf="CN=BBB*"...
... -s sub memberOf="*CN=BBB*"...
# returns nothing
The LDAP specification do not allow substring searches of Distinguished Names.
(like "CN=BBB,DC=uuu,DC=yyy,DC=xxx,DC=net")
I think you will need to write some code.
I have following working copy command:
export PGPASSWORD=****;psql -h "****" -U "****" -c "\copy (SELECT id, \"accountType\" as \"ACCOUNT TYPE\", \"actReasonCode\" as \"ACT REASON\",\"adrCity\" as \"ADR CITY\" FROM schemaName.\"tableName\" where id > 0 and id<=8238226) TO STDOUT CSV HEADER" dbName >/sourcefile/test.CSV
Now, Instead giving alias name in the query,my requirement is to get the header as input and append it in the copy command, something like below:
export PGPASSWORD=****;psql -h "****" -U "****" -c "\copy (SELECT id, \"accountType\", \"actReasonCode\",\"adrCity\" FROM schemaName.\"tableName\" where id > 0 and id<=8238226) TO STDOUT CSV HEADERS(ID,\"ACCOUNT TYPE\",\"ACT REASON\",\"ADR CITY\") " dbName >/sourcefile/test.CSV
Can anyone please help me with this.
You might have misunderstood the HEADER keyword.
HEADER [boolean]
Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and on input, the first line is ignored. This option is allowed only when using CSV format.
From https://www.postgresql.org/docs/9.2/sql-copy.html
As you can see, the option allows you to enable or disable the printing of a header line. It is not meant to be used to select which columns to print. That is part of your SELECT in the beginning of your query.
I have a sqlite3 database. One column has the TEXT type, and contains blobs which I would like to save as file. Those are gzipped files.
The output of the command sqlite3 db.sqlite3 ".dump" is:
INSERT INTO "data" VALUES(1,'objects','object0.gz',X'1F8B080000000000000 [.. a few thousands of hexadecimal characters ..] F3F5EF')
How may I extract the binary data from the sqlite file to a file using the command line ?
sqlite3 cannot output binary data directly, so you have to convert the data to a hexdump, use cut to extract the hex digits from the blob literal, and use xxd (part of the vim package) to convert the hexdump back into binary:
sqlite3 my.db "SELECT quote(MyBlob) FROM MyTable WHERE id = 1;" \
| cut -d\' -f2 \
| xxd -r -p \
> object0.gz
With SQLite 3.8.6 or later, the command-line shell includes the fileio extension, which implements the writefile function:
sqlite3 my.db "SELECT writefile('object0.gz', MyBlob) FROM MyTable WHERE id = 1"
You can use writefile if using the sqlite3 command line tool:
Example usage:
select writefile('blob.bin', blob_column) from table where key='12345';
In my case, I use "hex" instead of "quote" to retrieve image from database, and no need "cut" in the command pipe. For example:
sqlite3 fr.db "select hex(bmp) from reg where id=1" | xxd -r -p > 2.png
I had to make some minor changes on CL's answer, in order to make it work for me:
The structure for the command that he is using does not have the database name in it, the syntax that I am using is something like:
sqlite3 mydatabase.sqlite3 "Select quote(BlobField) From TableWithBlod Where StringKey = '1';" | ...
The way he is using the cut command does not work in my machine. The correct way for me is:
cut -d "'" -f2
So the final command would be something like:
sqlite3 mydatabase.sqlite3 "Select quote(BlobField) From TableWithBlod Where StringKey = '1';" | cut -d "'" -f2 | xxd -r -p > myfile.extension
And in my case:
sqlite3 osm-carto_z14_m8_m.mbtiles "select quote(images.tile_data) from images where images.tile_id = '1';" | cut -d "'" -f2 | xxd -r -p > image.png
Hello folks!
First a code I have now:
for CLSGRPID in `${${`/usr/bin/snmpwalk \
-v 1 -c $COMM $HOST $OID.11.1.1.1`##*:}%\n} | xargs` ; do
I'd like to first process
/usr/bin/snmpwalk -v 1 -c $COMM $HOST $OID.11.1.1.1
which used alone returns lines
.2.3.1.2.1.5.11.1.1.1.1 = INTEGER: 1
.2.3.1.2.1.5.11.1.1.1.2 = INTEGER: 2
.2.3.1.2.1.5.11.1.1.1.3 = INTEGER: 3
and then for every return line I'd like to cut it like ${line##*:} and then from the other side ${line%\n} and then all of those lines put to xargs and process it.
So requested output would be
1 2 3
Is it possible? Please get me some ideas how to do it.
net-snmp commands have many options that modify their behaviour and output. I recommend that you read the man pages for each of snmpcmd, snmpwalk, and snmp.conf.
Check the -O option group in snmpwalk (see below).
The -Oqv combination results in a column with just the numbers:
$ clsgrpids=$(
/usr/bin/snmpwalk -Oqv -v 1 -c $COMM $HOST $OID.11.1.1.1
)
$ echo "$clsgrpids"
1
2
3
Remove the quotes to let the shell print a single line:
$ echo $clsgrpids
1 2 3
Here are the remaining options in the -O option group that control the output for snmpwalk:
-O OUTOPTS
Toggle various defaults controlling output display:
0: print leading 0 for single-digit hex characters
a: print all strings in ascii format
b: do not break OID indexes down
e: print enums numerically
E: escape quotes in string indices
f: print full OIDs on output
n: print OIDs numerically
q: quick print for easier parsing
Q: quick print with equal-signs
s: print only last symbolic element of OID
S: print MIB module-id plus last element
t: print timeticks unparsed as numeric integers
T: print human-readable text along with hex strings
u: print OIDs using UCD-style prefix suppression
U: don't print units
v: print values only (not OID = value)
Verification (actual running code)
$ snmpwalk -Ov -v1 -c public localhost sysUptime
Timeticks: (66595) 0:11:05.95
$ snmpwalk -Oqv -v1 -c public localhost sysUptime
0:0:11:35.13
$ snmpwalk -Otqv -v1 -c public localhost sysUptime
70012
You can use awk.
for CLSGRPID in `/usr/bin/snmpwalk -v 1 -c $COMM $HOST $OID.11.1.1.1 | awk '{print $NF}'`; do
echo $CLSGRPID
done
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';"