How to take string format from command line? - jq

https://stedolan.github.io/jq/manual/#Stringinterpolation-%5C(foo)
I want to pass string format from the command line of jq, instead of embedding the format in string interpolation.
Let's say the format string is "my string %s". How to modify the following jq command to take the format string in its argument so that the output will be "my string x"?
jq --arg format "my string %s" -r -e . <<< '"x"'

Put your (dynamic) jq expression inside \(…), which in turn is part of a (static) string expression "…". If all you want is passing on the input, use the identity function ., and your example should read
jq -r -e '"my string \(.)"' <<< '"x"'
my string x
Demo
Having "my string %s" as a parameter string, you cannot use it for string interpolation, as string interpolation is a language construct, and jq does not provide an eval (or similar) function to evaluate a variable's content as code.
What you can do instead is to (naively) replace %s with something else using sub or gsub:
jq -r -e -n --arg f "my string %s" '$f | sub("%s"; input)' <<< '"x"'
my string x
Demo

Related

HP-UX KSH scripting - passing blank parameters with $#

I have a 'problem' with a script I'm developing in HP-UX KSH. The script contains many functions, and I need to pass the same set of parameters between them. All well and good, but some parameters can be blank. Its easy to pass blank parameters using double double-quotes (""), but what if I want to pass a complete set of parameters from one function into another using ${#}, including blanks? And to make things tricky, there can be a variable number of parameters each time, so the method has to be dynamic.
Example: I've got a function called test1 that takes a number of parameters. Any of them can be blank. I've also created a function called test2 into which all the parameters of test1 are passed:
test1()
{
echo 1-1: ${1}
echo 1-2: ${2}
test2 ${#}
}
test2()
{
echo 2-1: ${1}
echo 2-2: ${2}
}
# test1 "" hello
1-1:
1-2: hello
2-1: hello
2-2:
The trouble is, if ${1} is blank, ${2} from test1 appears as ${1} in test2. So to work around the problem I created this code, which effectively creates a function string with all parameters surrounded with double quotes:
test1()
{
typeset var FUNC="test2"
typeset -i var COUNT=1
echo 1-1: ${1}
echo 1-2: ${2}
while [ ${COUNT} -le ${##} ]; do
typeset var PARAM=$(eval "echo \$${COUNT}")
FUNC="${FUNC} \"${PARAM}\""
((COUNT=COUNT+1))
done
eval "${FUNC}"
}
# test1 "" hello
1-1:
1-2: hello
2-1:
2-2: hello
This works very nicely, thank you. Now to my 'problem'.
Is it actually possible to encapsulate the above code in a function of its own? It seems a catch 22 to me, in that you have to run that code to pass the blank parameters. I have to repeat this code snippet many times in my script because I can't find another way. Is there one?
Any help or guidance will be gratefully received.
Here is how I would write your functions:
show_params() {
typeset funcname=$1
typeset -i n=0
shift
for arg; do
((n++))
printf "%s:%d >%s<\n" "$funcname" $n "$arg"
done
}
test1() { show_params "${.sh.fun}" "$#"; test2 "$#"; }
test2() { show_params "${.sh.fun}" "$#"; }
test1 "" 'a string "with double quotes" in it'
test1:1 ><
test1:2 >a string "with double quotes" in it<
test2:1 ><
test2:2 >a string "with double quotes" in it<
Using your definition of test1, which builds up a string containing a command, adding double quotes around all the paramers, and then eval-ing the string, I get this result
$ test1 "" 'a string "with double quotes" in it'
1-1:
1-2: a string "with double quotes" in it
test2:1 ><
test2:2 >a string with<
test2:3 >double<
test2:4 >quotes in it<
That's because you're doing this:
eval "test2 \"\" \"a string \"with double quotes\" in it\""
# ......... A A A B B A
# A = injected quotes
# B = pre-existing quotes contained in the parameter

Match function in unix to find if string ends with particular input value?

I need a regular expression that I can use in match() function to see if value given at command line argument exists at the end of a given string.
I am using awk then trying use match function to get above result:
while read line; do
cat $line | awk -v value="$2.$" '{ if ( match("$1,value) != 0 ) print match("arvind","ind.$") " " "arvind" }'
done < xreffilelist.txt

single quote added in the space of a variable value in unix script

I have a date variable in a script and I have a string of parameter list comes from another file. Have tried to do eval to evaluate the date value in the parameter string. The date value would be like "2017-03-08 13:23:00". When the value gets evaluated in the parameter string, the space between the date and time part is enclosed with single quote, which consider the whole parameter string as invalid argument while passing to execute a command. Below are the examples.
Parameter string in the file:
-param Start_date=${St_date} -param End_date=${End_date} -param test='tst'\''
Lines in the script:
test1='tst'
St_date='2017-03-08 13:23:00'
End_date='2017-03-09 13:22:00'
#parameter string will be grep and assigned to variable param_list
#eval cmd below
paramlist=$(eval echo $(echo $param_list))
# when use paramlist to call a job, it displays below
-param Start_date='\''2017-03-08' '13:23:00'\'' -param End_date='\''2017-03- 09' '13:22:00'\'' -param test='\''tst'\''
How to remove the single quote between date and time part. Please suggest.
Thanks in Advance

jq: How to output quotes on raw output on windows

Using raw output I have to quote some values of the output.
echo [{"a" : "b"}] | jq-win64.exe --raw-output ".[] | \"Result is: \" + .a + \".\""
generates
Result is: b.
but how can I generate
Result is: "b".
Unfortunately it has to run on Windows called from inside a CMD file.
You need to escape the slashes to escape a "
$ echo [{"a" : "b"}] | jq-win64.exe --raw-output ".[] | \"Result is: \\\"\" + .a + \"\\\".\""
Result is: "b".
A hacky workaround with less backslashing could be:
jq -r ".[] | \"Result is: \" + (.a|tojson)"
[REVISED to reflect OP goal.]
Since you're trying to output double quotes in a double quoted string, you need to escape the inner quotes. And to escape the inner quotes, you need to also escape the escaping backslashes. So a literal double quote would have to be entered as \\\". You can do this a little cleaner by using string interpolation instead of regular string concatenation.
jq -r ".[] | \"Result is: \\\"\(.a)\\\".\""

Unix awk Substring string comparison

I want to find if a substring is contained in a string using Unix AWK command.
eg, pseudocode:
a= commandline
b=line
if(b is contained in a)
print "success "
$ awk 'BEGIN{a="commandline";b="line";if (a ~ b){print "success"}}'
success

Resources