I have script which works for me at least but the padding or say it markers for aligning or justifying the columns are not working good and i'm struggling to get the best solutions for it.
Below is the script which basically gets the Os Release version and Perl version running on the system.
if you look at the Result section the outer lining or markers which i defined are getting destorted .
Script:
#!/bin/bash
printf "\n"
marker=$(printf "%0.s-" {1..65})
printf "|$marker|\n"
printf "|%-10s | %-13s | %29s |\n" "Hostname" "RedHat Vesrion" "Perl Version"
printf "|$marker|\n"
remote_connect() {
target_host=$1
marker=$(printf "%0.s-" {1..65})
remote_data=($(
ssh -i ssh_prod "root#${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no "
rhelInfo=\$(cat /etc/redhat-release | awk 'END{print \$7}')
perlInfo=\$(rpm -qa | grep -i mod_perl)
echo \$rhelInfo \$perlInfo
"))
rhelInfo=${remote_data[0]}
perlInfo=${remote_data[1]}
if [[ $? -eq 0 ]]
then
printf "|%-10s | %-13s | %20s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "|%-10s | %-13s | %20s |\n" "$target_host" "Unable to get the ssh connection"
fi
} 2>/dev/null
export -f remote_connect
< CVE-2011-2767-hostsList.txt xargs -P30 -n1 -d'\n' bash -c 'remote_connect "$#"' --
Result:
|-----------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|-----------------------------------------------------------------|
|fsx9015 | | |
|fsx1258 | | |
|fsx1195 | | |
|fsx1063 | | |
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1210 | 6.7 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1484 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
Required:
|-----------------------------------------------------------------|
|Hostname | RedHat Vesrion| Perl Version |
|-----------------------------------------------------------------|
|fsx9015 | | |
|fsx1258 | | |
|fsx1195 | | |
|fsx1063 | | |
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1210 | 6.7 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1484 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
please help me understand or get a better way of doing it.
I'd suggest you don't try to start formatting until you have all the data at hand: each column should be as wide as the longest value in it.
Also, to make your code simpler, use column instead of the "fancy" table. I'm sure there's a utility that can draw ASCII table borders, but is it really necessary?
This version just prints comma-separated values from the ssh connections, then uses column to add the whitespace.
Note that I'm using a quoted heredoc for the code to send to the target host, which simplifies quoting.
#!/bin/bash
remote_connect() {
target_host=$1
if remote_data=$(
ssh -i ssh_prod "root#${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no <<'END_SSH'
rhelInfo=$(awk 'END{print $7}' /etc/redhat-release)
perlInfo=$(rpm -qa | grep -i mod_perl)
echo "$rhelInfo,$perlInfo"
END_SSH
)
then
echo "${target_host},${remote_data}"
else
echo "${target_host},?,?"
fi
} 2>/dev/null
export -f remote_connect
{
echo "Hostname,RedHat Version,Perl Version"
< CVE-2011-2767-hostsList.txt xargs -P30 -n1 -d'\n' bash -c 'remote_connect "$#"' --
} | column -s"," -t
You should use as simplistic as it could be, define your column alignment as per the requirement with the printf formatter..
try below, it should work , however i did not tested it.
#!/bin/bash
###########
printf "\n"
marker=$(printf "%0.s-" {1..73})
printf "|$marker|\n"
printf "| %-15s | %-15s | %-35s |\n" "Hostname" "RedHat Vesrion" "Perl Version"
printf "|$marker|\n"
remote_connect() {
target_host=$1
remote_data=($(
ssh -i /home/nxf59093/.ssh/ssh_prod "root#${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no "
rhelInfo=\$(cat /etc/redhat-release | awk 'END{print \$7}')
perlInfo=\$(rpm -qa | grep -i mod_perl)
echo \$rhelInfo \$perlInfo
"))
rhelInfo=${remote_data[0]}
perlInfo=${remote_data[1]}
if [[ $? -eq 0 ]]
then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "?" "?"
fi
} 2>/dev/null
export -f remote_connect
< CVE-2011-2767-hostsList.txt xargs -P15 -n1 -d'\n' bash -c 'remote_connect "$#"' --
printf "|$marker|\n"
How to put some values in the missing columns, as you guys see the below code is working but in the in else condition it not filling the column values.
What i need to do to put the somevalues in missing areas. Specially i want to fill like nologin?.
#!/bin/bash
###########
printf "\n"
marker=$(printf "%0.s-" {1..73})
printf "|$marker|\n"
printf "| %-15s | %-15s | %-35s |\n" "Hostname" "RedHat Vesrion" "Perl Version"
printf "|$marker|\n"
remote_connect() {
target_host=$1
remote_data=($(
ssh -i /home/nxf59093/.ssh/ssh_prod "root#${target_host}" -o StrictHostKeyChecking=no -o PasswordAuthentication=no "
rhelInfo=\$(cat /etc/redhat-release | awk 'END{print \$7}')
perlInfo=\$(rpm -qa | grep -i mod_perl)
echo \$rhelInfo \$perlInfo
"))
rhelInfo=${remote_data[0]}
perlInfo=${remote_data[1]}
if [[ $? -eq 0 ]]
then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "?" "?"
fi
} 2>/dev/null
export -f remote_connect
< CVE-2011-2767-hostsList.txt xargs -P15 -n1 -d'\n' bash -c 'remote_connect "$#"' --
printf "|$marker|\n"
Result:
|---------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|---------------------------------------------------------------|
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1243 | 6.9 | |
|fsx1244 | | |
|fsx1245 | | |
|---------------------------------------------------------------|
You should look at PARAMETER Expansion of Bash by using a default value. For an example, provide in the bash wiki page, for an example below ..
${PARAMETER:-WORD}
${PARAMETER-WORD}
Where, If the parameter PARAMETER is unset (never was defined) or null (empty), this one expands to WORD, otherwise it expands to the value of PARAMETER, as if it just was ${PARAMETER}. If you omit the : (colon), like shown in the second form, the default value is only used when the parameter was unset, not when it was empty.
Looking at your code, taking the above in consideration, your below line of code just need minor correction and that should do the Job for you.
if [[ $? -eq 0 ]];then
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "$rhelInfo" "$perlInfo"
else
printf "| %-15s | %-15s | %-35s |\n" "$target_host" "${rhelInfo:-?}" "${perlInfo:-?}"
fi
You will get like below:
|---------------------------------------------------------------|
|Hostname | RedHat Vesrion | Perl Version |
|---------------------------------------------------------------|
|fsx1241 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1242 | 6.9 | mod_perl-2.0.4-12.el6_10.x86_64 |
|fsx1243 | 6.9 | |
|fsx1244 | ? | ? |
|fsx1245 | ? | ? |
|---------------------------------------------------------------|
hope this will be usefull.
I am trying to format my script output in tabular format something like this:
Data: 11001,4071 | 10008,4578 | 10500,2100 ...
==================================================
20160720 55454 545454 55454
somehow i am able to achieve it but script asks for password and prints catrunner --> Using standard unix daf factory libpal-daf inline.
like:
Date: 11001,4071 | 10008,4578 | 10500,2100 | 40000,4021 | 10003,1102 | 11001,9230 | 10001,1001 | 40000,4020 | 10001,1102 | 10008,4569 | 10008,4568 | 10003,1200 | 40000,4006
======================================================================================================================================================================================
20160720 Password: // for switch user, how can i ommit password request?
su: incorrect password
catrunner --> Using standard unix daf factory libpal-daf //How can i get ride of this line?
13116
catrunner --> Using standard unix daf factory libpal-daf
9167
catrunner --> Using standard unix daf factory libpal-daf
Here is my script:
#!/bin/bash
clear
DT=`date +%Y%m%d`
Yesterday=$(date --date="-1 day" +"%Y-%m-%d")
CODES="$(< error_codes.txt)"
divider==============
divider1=$divider$divider$divider$divider$divider$divider$divider$divider$divider$divider$divider$divider$divider$divider
header="\n %-10s %8s %10s %21s\n"
width=43
echo " Date: " `sed -e :a -e '$!N; s/\n/ | /; ta' error_codes.txt`
echo $divider1;
printf "%s\t" $DT
su - adv
cd /advdata/datashareB/FFFF/continuousDownstream/` echo $Yesterday`
for CODE in $CODES; do
EventType=$(echo $CODE | cut -c1-5)
ErrorCode=$(echo $CODE | cut -c7-10)
eventExport -printEvents -file Run_`echo $fileName`*_*.tar -filter "ErrorCode=`echo $ErrorCode`;EventType=`echo $EventType`" -names -silent | wc -l
done
printf "$s\n"
how to use for loop in linux to generate the following lines?
zcat ~/tools/sample66621D.fastq | bowtie2 -a - | samtools ->sample66621D.BAM
zcat ~/tools/sample66622D.fastq | bowtie2 -a - | samtools ->sample66622D.BAM
zcat ~/tools/sample66623D.fastq | bowtie2 -a - | samtools ->sample66623D.BAM
I've tried:
for ((a=21;a<=23;a++))
echo "zcat ~/tools/sample666$aD.fastq | bowtie2 -a - | samtools ->sample666$aD.BAM"
done
but it turned to be three identical lines .
zcat ~/tools/sample666.fastq | bowtie2 -a - | samtools ->sample666.BAM
zcat ~/tools/sample666.fastq | bowtie2 -a - | samtools ->sample666.BAM
zcat ~/tools/sample666.fastq | bowtie2 -a - | samtools ->sample666.BAM
thanks
$aD is a valid variable name. You must delimit the variable name with brackets, i.e. ${a}.
For example suppose I wish for a CSV file with 5 columns. How could I convert
a,b,c,d
d,e,f
into
a,b,c,d,
d,e,f,,
Easy in Awk:
awk -F, -vOFS=, '{$5=$5}1'
Not too hard in Bash:
IFS=,
while read -a F; do
for ((i=0; i<5; i++)); do
F[i]=${F[i]}
done
echo "${F[*]}"
done
Example with awk:
$ awk -F, '{printf("%s,%s,%s,%s,%s\n",$1,$2,$3,$4,$5)}' example.txt
a,b,c,d,
d,e,f,,
or
$ awk -F, -v OFS=, '{print $1,$2,$3,$4,$5}' example.txt
I found a solution with GNU sed:
sed -r 's/^([^,]*)(,|)([^,]*)(,|)([^,]*)(,|)([^,]*)(,|)$/\1,\3,\5,\7,/'
# | | | | ^ ^ ^ ^
# | | | | | | | |
# |__________|__________|__________|__________| | | |
# |__________|__________|_____________| | |
# |__________|________________| |
# |___________________|
Proof:
echo -e "a,b,c,d\nd,e,f\nx" | sed -r 's/^([^,]*)(,|)([^,]*)(,|)([^,]*)(,|)([^,]*)(,|)$/\1,\3,\5,\7,/'
a,b,c,d,
d,e,f,,
x,,,,
Another approach which I like very much is this:
gawk '{print $0 ",,,,,"}' a | cut -d \, -f 1-5
I think it is much more simple and clear