Is that a way to remove dashed line --------- in SQL cmd. E.g:
Name ID
--------
Alex 13
Alice 22
I found a best solution:
(1)Get the column headers
sqlcmd -S DEV\SQLEXPRESS -d www -U dbadmin -P Admin123$ -Q "set nocount on;select top 0 * from dbo.www1" -o "C:\export-sql-server-to-csv\20.csv" -s "|"
(2)Remove hyphen line
findstr /R /C:"^[^-]*$" C:\export-sql-server-to-csv\20.csv > C:\export-sql-server-to-csv\21.csv
(3)Get data without headers
sqlcmd -S DEV\SQLEXPRESS -d www -U dbadmin -P Admin123$ -i "C:\export-sql-server-to-csv\12.sql" -s "|" -h -1 -o "C:\export-sql-server-to-csv\22.csv"
(4)Append header and data
type C:\export-sql-server-to-csv\21.csv C:\export-sql-server-to-csv\22.csv > C:\export-sql-server-to-csv\23.csv
There is no way you can stop the sqlcmd to give you the result in that form. All you can do is post-process it to remove the line containing hyphens. Like:
sqlcmd ... | sed -e '2d'
These two steps will get you the table extract in a .csv file with no hyphen and no row counts. (Tried and Tested)
Output columns with headers (this will have the hyphens in the o/p file)
sqlcmd -S YourServer -Q "set nocount on;select columnnames from YourDB.YourSchema.TheTableOrView" -o "Extract.csv" -s "," -W 700
Strip the ---'s from this file and get the expected format in the newly created csv file.
findstr /R /C:"^[^-]*$" Extract.csv > Extract1.csv
Related
I have read literally every answer on the net that I could find. Nothing is similar to my problem, so here it is:
I have a bash script with curl and I get a variable back. I want to update my database with this variable, however it doesn't work.
My variable is $stream and no matter what I do, I always get the word "$stream" into the database instead of the result of the curl.
My script is:
#!/bin/bash
stream=$(curl --silent "https://player.mediaklikk.hu/playernew/player.php?video=mtv1live&noflash=yes&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=&osfamily=Windows&osversion=10&browsername=Chrome&browserversion=97.0.4692.99&title=M1&contentid=mtv1live&embedded=0" | grep -Po 'file": "\K(.*?)(?=")' | sed 's/\\\/\\\//https:\\\/\\\//g')
echo $stream
mysql
use mydatabase;
UPDATE my_table SET my_url = "$stream" WHERE my_name = 'stream_name';
You can use the -e option to execute a query. Put this in double quotes and variables will be expanded.
#!/bin/bash
stream=$(curl --silent "https://player.mediaklikk.hu/playernew/player.php?video=mtv1live&noflash=yes&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=&osfamily=Windows&osversion=10&browsername=Chrome&browserversion=97.0.4692.99&title=M1&contentid=mtv1live&embedded=0" | grep -Po 'file": "\K(.*?)(?=")' | sed 's/\\\/\\\//https:\\\/\\\//g')
echo $stream
mysql mydatabase -e "UPDATE my_table SET my_url = '$stream' WHERE my_name = 'stream_name';"
When I'm trying to execute script below and getting error: "curl: (3) URL using bad/illegal format or missing URL"
#!/bin/bash
stage="develop"
branch="branch_name"
getDefinition=$(curl -u user#example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions?api-version=5.1")
for def in $(echo "$getDefinition" | jq '.value[] | select (.path=="\\Some_path\\'$stage'") | .id'); do
getBuildInfo=$(curl -u user#example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1")
# echo $def
body=$(echo "${getBuildInfo}" | jq '.repository.defaultBranch = "refs/heads/release/'"${branch}"'"' | jq '.options[].inputs.branchFilters = "[\"+refs/heads/release/'"${branch}"'\"]"' | jq '.triggers[].branchFilters[] = "+refs/heads/release/'"${branch}"'"')
echo ${body} > data.json
done
It happens when I'm trying to pass variable ${def} into a line:
curl -u user#example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"
But when I declare an array, curl works as expected.
Example:
declare -a def
def=(1 2 3 4)
curl -u user#example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"
Could you please suggest how can I pass variable into URL properly?
Do you need to call curl for 4 times? If so.
for def in 1 2 3 4; do curl -u user#example.com:password -X GET "https://dev.azure.com/organization/project/_apis/build/definitions/${def}\?api-version=5.1"; done
Set IFS=$' \t\r\n' at the top of your script.
IFS is the Interactive Field Separator.
In UNIX, IFS is space, tab, newline, or $' \t\n', but on Windows this needs to be $' \t\r\n'.
The ^M character is \r.
I have used the method to assign the SQL output to a variable as below.
dbRole=$(${SQLPLUSPGM} -s / as sysdba <<-EOF
set head off
set verify off
set feedback off
select trim(translate(database_role,' ','_')) from v\$database;
exit;
EOF
)
But the variable o/p appending a "\n" character i.e \nPHYSICAL_STANDBY
However, when I use the below method it is working fine
${SQLPLUSPGM} -s / as sysdba <<-EOF | grep -v '^$' | read dbRole
set head off
set verify off
set feedback off
select trim(translate(database_role,' ','_')) from v\$database;
exit;
EOF
Any suggestion why it is appending `\n' and how we can get rid of it.
Appreciate your suggestions.
Your second method, with the grep -v, removes the additional line.
You can use some filter inside your first method with additional parentheses.
dbRole=$((cat | grep -v "^$") <<-EOF
1
2
3
5
EOF
)
Alternative filters with some differences are grep ., head -1, sed '$d'.
I have a command that I'd like to run and get output from. Here's the command:
sh execute_odi_script.sh Audit_ExecutionLogStart.sql heelo
It should return
Begin Informatica Script 18 Redshift Delta copy completed at: 04/10/17 18:46:20 END
Now I need to grab just the output from the SQL file, which is 18 in this case.
Someone was able to help me achieve that, but only if I do it in 2 steps like so:
> logStart=$( sh execute_odi_script.sh Audit_ExecutionLogStart.sql heelo )
> echo $logStart | sed -r s/.*Begin\ Informatica\ Script\ \(\.*\)\ Redshift\ Delta.*/\\1/ ); echo $logStart
18
I've been trying to play around with how I'm doing the command substitution but am having trouble understanding what to even change. How do I do all this in one line?
First -- if you only care about one line, grep for that line:
logStart=$( sh execute_odi_script.sh Audit_ExecutionLogStart.sql heelo \
| grep -e "Begin Informatica Script" \
| sed -r 's/.*Begin Informatica Script (.*) Redshift Delta.*/\1/' )
echo "$logStart"
Alternately, you the entire pipeline inside the command substitution:
logStart=$( sh execute_odi_script.sh Audit_ExecutionLogStart.sql heelo \
| tr '\n' ' ' \
| sed -r 's/.*Begin Informatica Script (.*) Redshift Delta.*/\1/' )
echo "$logStart"
If you want to avoid the echo as a separate step, you might add a | tee /dev/stderr to the end of the pipeline (again, inside $()).
I'd like to run an ldapsearch query repeatedly substituting the uid from a list and output results to a new file.
ldapsearch -h ldap.com -p 389 -x -b "dc=top,dc=com" \
"uid=**value_from_a_text_file**" >>ldap.query.results.
Are there any suggestions on how to accomplish this?
Assuming your file is a list of UIDs, one-per-line, and is named uidfile.txt
for line in `cat uidfile.txt`; do
ldapsearch -h ldap.com -p 389 -x -b "dc=top,dc=com" "uid=${line}" >>ldap.query.results
done
Assuming data in CSV format with first field as UID
awk -F "," '{print $1}' data.csv | \
while read uiddata
do
ldapsearch -h ldap.com -p 389 -x -b "dc=top,dc=com" "uid=${uiddata}" >> ldap.query.results
done