When I execute my query in PostgreSQL:
SELECT names
from user;
I obtain the following result:
names
--------------
Anna
Julius
Perico
(3 rows)
What I want to get is the following output:
Anna Julius Perico
I need this because is a bash script ant I need to save it in a variable.
The string_agg function might be what you want here:
select string_agg(names, ' ' order by names) from user;
I said "might" because this assumes that the names column itself could be used to generate the order in the single string output. If you want a different order, then you would need another column.
If you want to use it in a shell script, this would be the best way:
myvar=`psql --no-align --quiet --tuples-only --command='SELECT name FROM users'`
No need to have them in one line; the shell accepts a line feed as field separator as well:
for n in $myvar; do
echo "name: $n"
done
name: Anna
name: Julius
name: Perico
Related
I have a table with column called "names". Can i store each name into different variable?
Right now I am using a for loop and storing the results into single variable.
for j in `psql database_name -U admin -c "select names from table;"`
; do
done
When i do echo$j it prints below result
tom
Harry
steve
How do i store each values(names) in different variables?
Creating one variable per value is impractical. Think about it: How would you access these variables? What you want is an array.
You can define the array as follows:
mapfile -t names < <(psql database_name -U admin -c 'select names from table;')
This will add every line printed by psql as an entry of the array names. To access the entries use ${names[0]}, ${names[1]}, and so on. The size of the array is ${#names[#]}.
Since the question is called »Convert for loop results into array in bash« here's another way to build up the array. However, I wouldn't use a for ... in $(...) loop here as names with spaces will be split and symbols like * will expand.
names=()
for name in $(psql database_name -U admin -c 'select names from table;'); do
names+=("$name")
done
If you want the loop to run some additional commands use the first approach in this answer and loop over the array – this is safer:
mapfile ... # see above
for name in "${names[#]}"; do
# do something with $name
done
I have a folder where there are 50 excel sheets in CSV format. I have to populate a particular value say "XYZ" in the column I of all the sheets in that folder.
I am new to unix and have looked for a couple of pages Here and Here . Can anyone please provide me the sample script to begin with?
For example :
Let's say column C in this case:
A B C
ASFD 2535
BDFG 64486
DFGC 336846
I want to update column C to value "XYZ".
Thanks.
I would export those files into csv format
- with semikolon as field separator
- eventually by leaving out column descriptions (otherwise see comment below)
Then the following combination of SHELL and SED script could more or less do already the trick
#! /bin/sh
for i in *.csv
do
sed -i -e "s/$/;XZY/" $i
done
-i means to edit the file in place, here you could append the value to all lines
-e specifies the regular expresssion for substitution
You might want to use a similar script like this, to rename "XYZ" to "C" only in the 1st line if the csv files should contain also column descriptions.
In our environment we do not have access to Hive meta store to directly query.
I have a requirement to generate tablename , columnname pairs for a set of tables dynamically.
I was trying to achieve this by running "describe extended $tablename" to a file for all tables and pick up tablename and column name pairs from the file.
is there any easier way it is done/it can be done other than this way .
The desired output is like
table1|col1
table1|col2
table1|col3
table2|col1
table2|col2
table3|col1
This script will print columns in desired format for single table. AWK parses strings from describe command, takes only column_name, concatenates with "|" and table_name variable, each string printed with \n as a delimiter between them.
#!/bin/bash
#Set table name here
TABLE_NAME=your_schema.your_table
TABLE_COLUMNS=$(hive -S -e "set hive.cli.print.header=false; describe ${TABLE_NAME};" | awk -v table_name="${TABLE_NAME}" -F " " 'f&&!NF{exit}{f=1}f{printf c table_name "|" toupper($1)}{c="\n"}')
You can easily modify it for generating output for all tables using show tables command for example.
The easier way is to access metadata database directly.
This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 7 years ago.
I want to create a script to automate some processes with Redshift. Specifically, I want find an attribute of one of my tables with a SELECT and then use it in a INSERT. My script looks like this:
psql -h ... -c "SELECT id_process FROM process WHERE de_process = 'EMR'"
psql -h ... -c "INSERT INTO execution (id_process) values (X);"
In the first sentence I get a unique value, the ID I'm looking for, in a format like this:
id_proceso
------------
2
(1 row)
Then I would like to use it as the value to insert in the second sentence, substituting the "X, but I don't know how to save into a variable and then reuse the output of the first sentence.
Any suggestion?
P.D. In other question it shows how to do it in a unique sentence, but I need to save the value for a future use.
Check psql options, but sample script can be the following:
psql -h localhost -d testdb <<EOF
\out sample.txt
\pset border 1
WITH test_data AS ( SELECT 2 AS id_process)
SELECT id_process FROM test_data;
\out
EOF
Result for cat sample.txt will be:
id_process
------------
2
(1 row)
If you want to get just pure value from SELECT statement, consider following params in the example above:
\t:
Toggles the display of output column name headings and row count
footer. This command is equivalent to \pset tuples_only and is
provided for convenience.
\pset format unaligned:
unaligned format writes all columns of a row on one line, separated by
the currently active field separator. This is useful for creating
output that might be intended to be read in by other programs (for
example, tab-separated or comma-separated format).
I need to store the result of a Hive query in a variable whose value will be used later. So, something like:
$var = select col1 from table;
$var_to_used_later = $var;
All this is part of a bash shell script. How to form the query so as to get the desired result?
Hive should provide command line support for you. I am not familiar with hive but I found this: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Cli, you can check whether that works.
Personally, I used mysql to achieve similar goal before. The command is:
mysql -u root -p`[script to generate the key]` -N -B -e "use XXXDB; select aaa, bbb, COUNT(*) from xxxtable where some_attribute='$CertainValue';"
I used the method shown here and got it! Instead of calling a file as shown, I run the query directly and use the value stored in the variable.