Format spaces in bash output with echo [duplicate] - bash

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;

Related

Append to a list in a JSON document from bash [duplicate]

This question already has answers here:
how to add json object to json file using shell script
(2 answers)
Closed 2 years ago.
I have a file static-nodes.json, which has the following content >>>
[
"enode://70399c3d1654c959a02b73acbdd4770109e39573a27a9b52bd391e5f79b91a42d8f2b9e982959402a97d2cbcb5656d778ba8661ec97909abc72e7bb04392ebd8#127.0.0.1:21000?discport=0&raftport=50000",
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
]
I want to add another enode >>>
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50002"
But when I am trying to append it using the following command—>>>>>
Echo "enode to be appended " >> static-nodes.json
It is appended outside of the brackets as shown below
[
"enode://70399c3d1654c959a02b73acbdd4770109e39573a27a9b52bd391e5f79b91a42d8f2b9e982959402a97d2cbcb5656d778ba8661ec97909abc72e7bb04392ebd8#127.0.0.1:21000?discport=0&raftport=50000",
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
]
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
How should I fix this.?
Please have a look at
how to add json object to json file using shell script
what you can do it
Option 1:
create a temp json file using and then merge the temp json to nodes-json
toBeAppended = "enode to be appended"
echo "[ $toBeAppended ]" > tempNodes.json
jq -s add tempNodes.json static-nodes.json
Option 2. go with sed, awk, grep utilities

Bash script won't add #gmail.com to end of .csv input column [duplicate]

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!

Populate a value in a particular column in csv

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.

Removing Timestamp from multiple files [duplicate]

This question already has answers here:
remove date from filename but keep the file extension
(2 answers)
Closed 4 years ago.
Is there a quick and clever way to remove various timestamps from multiple files with different names? The timestamp format always remains the same, although the values differ. An example of my files would be...
A_BB_CC_20180424_134312
A_B_20180424_002243
AA_CC_DD_E_20180424_223422
C_DD_W_E_D_20180423_000001
with the expected output
A_BB_CC
A_B
AA_CC_DD_E
C_DD_W_E_D
Notice the last file has a different timestamp, I don't mind if this is a day specific timestamp removal or all, or two variations. My problem is I can't think of the code for an ever changing time value :(
Thanks in advance
EDIT - Adding edit in to show why this is not a duplicate as Tripleee thinks. His duplicate link is for files with the same prefix, my question is about files with different names so the answer is different.
Using parameter expansion %% bashism that removes the end of the filename:
for i in /my/path/*; do mv "$i" "${i%%_2018*}"; done
This relies on the timestamp that start with 2018...
Using awk:
for i in /my/path/*; do mv "$i" $(awk -v FS=_ 'NF-=2' OFS="_" <<< "$i"); done
This awk script is based on the field separator _. It prints the filename without the last 2 field representing the timestamp.
In order to rename a set of files and apply regular expressions in the renaming process you can use the rename command.
So in your example:
rename 's#_[0-9]*_[0-9]*##' *_[0-9]*
This renames all files in the current directory ending with _ followed by digits.
It cuts away all _ followed by digits followed by _ followed by digits.

How to save a Redshift SELECT atribute into a script variable [duplicate]

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).

Resources