This question already has answers here:
How to prepare a statement from the CLI interpreter?
(2 answers)
How to escape single quotes within single quoted strings
(25 answers)
Closed 1 year ago.
I am trying to store a list of rows inside of a variable like in this example:
list=$(bash -c 'sqlite3 /home/ubuntu/myplace/mydb.db "SELECT row1 FROM table1"')
But when I want to add parameters like in this example:
list=$(bash -c 'sqlite3 /home/ubuntu/myplace/mydb.db "SELECT row2 FROM table2 WHERE row2='somevalue'"')
It does not work because the command given to bash inside the '' ends after the WHERE row2=.
I tried some other variants where I first wrote the results of
sqlite3 /home/ubuntu/myplace/mydb.db "SELECT row2 FROM table2 WHERE row2='somevalue'"
to some txt, csv or json file just to read it out one line later but that just came out to be not practical.
Is there any other practical possibility?
Related
This question already has answers here:
Shell script to properly align columns
(1 answer)
How to printf align my output like a table?
(1 answer)
Closed 9 months ago.
Im trying to have some symple output, the issue is that I can't seem to set the spaces. I have to manually calculate what each variable will have. Is there a way to format table outputs better with echo?
This code
echo """
Datasets available to download are:
Dataset - File Format
${dataset_one[6]} - ${dataset_one[5]}
${dataset_two[6]} - ${dataset_two[5]}
${dataset_three[6]} - ${dataset_three[5]}
You currently have downloaded to hdfs the the following datasets:
"""
Returns:
Dataset - File Format
dataset1 - csv
ds2 - csv
datasnytaxi - parquet
I think column -t is what you are looking for:
-t Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the characters supplied using the -s option. Useful for pretty-printing displays.
Try using '\t' with echo command. you can also refer to Echo tab characters in bash script for how to use '\t' with echo in bash.
alternative you can also try printf;
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
while IFS=, read -r col1 col2 col3
do
email="$col3"
email+='#gmail.com'
echo $email
done < ~/Desktop/Names.csv
I don't have to do anything special with Column 1 or 2, but adding #gmail.com to column 3 just outputs #gmail.com or if the input string is longer #gmail.com+longer bit of input string.
ie.
If column 3 was Dekkars, I get #gmail.com. If it is aaaaaaaaaaa (one length longer than #gmail.com) I get #gmail.coma
I'm sure this is something to do with the # sign, but I've tried using \ to escape it, single quotes, etc. Any ideas?
I've already read concatenating bash strings, and I'm doing what it suggests with different outcomes than are expected.
Here is input Data
Test Name,8,aaaaaaaaaaa
John Doe,8,bbbbbbbbbbbb
Name,Grade,ID
(Note, I have columns at bottom because otherwise my while loop won't read the bottom row)
Output
#gmail.coma
#gmail.combb
dos2unix fixed the problem. Incorporated it into my script so I don't have to remember to call it every time I change the .csv...
Thank you all!
This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 1 year ago.
I have a folder with 18 pairs of files with the same name except one has an R1 and the other an R2 , e.g. SM000907_S1_R1.fastq and SM000907_S1_R2.fastq.
I would like to throw a command in a loop for all these pairs.
I've tried the following loop, but it's not working (it throws the error "wrong substitution"):
for sample in ${seq 1 18}; do
merge-paired-reads.sh SM000907_S$sample_R1.fastq SM000907_S$sample_R2.fastq > output
done
You can use either $(seq 1 18) or {1..18}, but not both at the same time.
Also, your command line does not work because you are using different variable names inadvertently. Surround them with braces.
Finally, as a good practice, quote all the strings that contain a variable:
for sample in {1..18}; do
merge-paired-reads.sh "SM000907_S${sample}_R1.fastq" "SM000907_S${sample}_R2.fastq" > output
done
This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 4 years ago.
I have the following command which does not work:
sqlite3 my_db.sqlite "SELECT name FROM sqlite_master WHERE type = 'table';" | for i in $(cat) ; do sqlite3 my_db.sqlite 'SELECT * FROM "${i}"'; done
To explain it quickly: the first part below is supposed to retrieve the table names from a sqlite file that I have:
sqlite3 my_db.sqlite "SELECT name FROM sqlite_master WHERE type = 'table';"
And this part is supposed to display the entire content of each table recursivelyin the stdout:
for i in $(cat) ; do sqlite3 my_db.sqlite 'SELECT * FROM "${i}"'; done
The problem is that I have no idea how I am supposed to pass i to the sqlite command. I tried with "${i}" but obviously it is interpreted as a classic string to find a matching table name, and just return Error: no such table: ${i}
How should I pass i ?
Thank you in advance for your help.
You must use double quotes around the query to allow bash to recognize the variable. This implies that you have to correctly escape the double quotes that are part of the SQL statement:
... | for i ... ; do sqlite3 my_db.sqlite "SELECT * FROM \"${i}\""; done
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).