How can I pass external variable with Xidel tool? - bash

I have a XQuery expression stored in a file
(: file process.xq :)
declare variable $var external;
...
and use it with Xidel.
xidel --silent --color=never --xml --xquery "$(< process.xq)" my.xml
How can I pass such external variable?

it seems be not possible with "external"
but it can be achieved somehow with extra query expression....
xidel --silent --color=never --xml --xquery "foo := bar" --xquery "$(< process.xq)" my.xml
and just use the $foo as "usual"
(: file process.xq :)
$foo
...

I'm no XQuery expert, but at least for xidel this is how you declare a variable in a query-file:
declare variable $var := "external";
()
And don't forget the (), or you'll get err:XPST0003: Unexpected query end.
Then to load the query-file:
$ xidel -s --extract-file=process.xq -e '$var'
#or
$ xidel -s -e #process.xq -e '$var'
external

Related

How to split a string on the second match

I have a string:
foo="re-9619-add-selling-office";
I'd like to break up the string on the second - (dash) into variable1 and variable2. I want to end up with variable1=re-9619 and variable2=add-selling-office
I tried it using grep and awk, but now I not sure that's the way to go.
Here is a single sed + read way:
foo="re-9619-add-selling-office"
read var1 var2 < <(sed -E 's/^([^-]*-[^-]*)-/\1 /' <<< "$foo")
# check variables
declare -p var1 var2
declare -- var1="re-9619"
declare -- var2="add-selling-office"
Could you please try following once. Where first variable will have value like re-9619 and second shell variable will have value like add-selling-office
first=$(echo "$foo" | sed 's/\([^-]*-[^-]*\)-.*/\1/')
second=$(echo "$foo" | sed 's/\([^-]*\)-\([^-]*\)-\(.*\)/\3/')
Explanation:
echo "$foo" | sed 's/\([^-]*-[^-]*\)-.*/\1/': Printing value of foo variable and passing its output to sed command. In sed I am using substitute capability to perform substitution, \([^-]*-[^-]*\)-.*(which has everything from starting of value to till 2nd occurrence of - in back reference in it). Then substituting whole value with 1st captured back reference value which will become only re-9619.
echo "$foo" | sed 's/\([^-]*\)-\([^-]*\)-\(.*\)/\3/': Logic is same as above mentioned command. Using sed's capability of substitution with using back reference capability of it. Here we are printing everything after 2nd occurrence of -.
NOTE: second=$(echo "$foo" | sed -E "s/$first-(.*)/\1/") could also help as per #User123's comments.
That can be done using parameter expansions, you don't need an external utility.
$ foo="re-9619-add-selling-office"
$ variable2=${foo#*-*-}
$ variable1=${foo%-"$variable2"}
$
$ echo $variable1
re-9619
$ echo $variable2
add-selling-office
You can use cut:
variable1=$(echo $foo | cut -d '-' -f 1-2)
variable2=$(echo $foo | cut -d '-' -f 3-)
This is the result:
>> echo $variable1
re-9619
>> echo $variable2
add-selling-office

Bash curly expansion with space in the string

I would like to use curly expansion to save some typing.
My desire expansion is:
-e uncore_imc0/cas_count_read/ -e uncore_imc1/cas_count_read/ -e uncore_imc2/cas_count_read/ -e uncore_imc3/cas_count_read/ -e uncore_imc4/cas_count_read/ -e uncore_imc5/cas_count_read/ -e uncore_imc6/cas_count_read/ -e uncore_imc7/cas_count_read/
I've tried:
-e uncore_imc{0..7}/cas_count_read/
but this only expand to (with -e only in the beginning)
-e uncore_imc0/cas_count_read/ uncore_imc1/cas_count_read/ uncore_imc2/cas_count_read/ uncore_imc3/cas_count_read/ uncore_imc4/cas_count_read/ uncore_imc5/cas_count_read/ uncore_imc6/cas_count_read/ uncore_imc7/cas_count_read/
If I tried:
{-e, uncore_imc{0..7}/cas_count_read/}
or
"-e uncore_imc{0..7}/cas_count_read/"
Neither would expand.
You can use printf:
printf -- '-e uncore_imc%d/cas_count_read/ ' {0..7}; echo
-e uncore_imc0/cas_count_read/ -e uncore_imc1/cas_count_read/ -e uncore_imc2/cas_count_read/ -e uncore_imc3/cas_count_read/ -e uncore_imc4/cas_count_read/ -e uncore_imc5/cas_count_read/ -e uncore_imc6/cas_count_read/ -e uncore_imc7/cas_count_read/
You can also store this expansion in a variable like this:
printf -v arg -- '-e uncore_imc%d/cas_count_read/ ' {0..7}
If this is really for a script, don't use brace expansion. You only have to write the code once, so readability should be a higher priority. Create an array instead:
opts=()
for((i=0; i < 8; i++)); do
opts+=(-e "uncore_imc$i/cas_count_read/")
done
someCommand "${opts[#]}"

Remove a substring from a bash variable

A bash variable contents are command line arguments, like this:
args="file-1.txt file-2.txt -k file-3.txt -k --some-argument-1 --some-argument-2"
the string -k can appear anywhere in the above string, there are some other arguments that are not -k.
Is it possible to extract all the strings (i.e. file names with all other arguments) except -k from the argument, and assign it to a bash variable?
Using sed
Is is possible to extract all the strings (i.e. file names with all other arguments) except -k from the argument, and assign it to a bash variable?
I am taking that to mean that you want to remove -k while keeping everything else. If that is the case:
$ new=$(echo " $args " | sed -e 's/[[:space:]]-k[[:space:]]/ /g')
$ echo $new
file-1.txt file-2.txt file-3.txt --some-argument-1 --some-argument-2
Using only bash
This question is tagged with bash. Under bash, the use of sed is unnecessary:
$ new=" $args "
$ new=${new// -k / }
$ echo $new
file-1.txt file-2.txt file-3.txt --some-argument-1 --some-argument-2
Piping it to sed should work:
echo $args | sed -e 's/[[:space:]]\-[[:alnum:]\-]*//g'
file-1.txt file-2.txt file-3.txt
and you can assign it to a variable with:
newvar=`echo $args | sed -e 's/[[:space:]]\-[[:alnum:]\-]*//g'`
Command-line arguments in bash should be stored in an array, to allow for arguments that contain characters that need to be quoted.
args=(file-1.txt file-2.txt -k file-3.txt -k --some-argument-1 --some-argument-2)
To extract strings other than -k, just use a for loop to filter them.
newargs=()
for arg in "${args[#]}"; do
[[ $arg = "-k" ]] && newargs+=("$arg")
done

How can I get the content of this string surrounded by single-quote

I have a string like this:
SOMETHING='abc.abc.abc'
How can I extract the content of it ( abc.abc.abc ), inside the single-quotes?
$ awk -F"=\047|\047" '/SOMETHING/{print $(NF-1)}' file
abc.abc.abc
str="SOMETHING='abc.abc.abc'"
substr=$(echo "$str" | cut -d "'" -f 2)
With bash, you could write
substr=$(cut -d "'" -f 2 <<< "$str")
Or, shell only:
IFS="'"
set -- $str
substr=$2
Or use an array
IFS="'"
fields=($str)
substr=${fields[1]}
You should be able to already extract it. Example:
$ cat test.sh
#!/bin/bash
SOMETHING='abc.abc.abc'
echo $SOMETHING
$ ./test.sh
abc.abc.abc
You have tagged this question as shell. So I guess you're able to use regular expressions in grep. Or perhaps you could do something with javascript regular expressions.
this regex would find it
abc\.abc\.abc

Substitution with sed + bash function

my question seems to be general, but i can't find any answers.
In sed command, how can you replace the substitution pattern by a value returned by a simple bash function.
For instance, I created the following function :
function parseDates(){
#Some process here with $1 (the pattern found)
return "dateParsed;
}
and the folowing sed command :
myCatFile=`sed -e "s/[0-3][0-9]\/[0-1][0-9]\/[0-9][0-9]/& parseDates &\}/p" myfile`
I found that the caracter '&' represents the current pattern found, i'd like it to be passed to my bash function and the whole pattern to be substituted by the pattern found +dateParsed.
Does anybody have an idea ?
Thanks
you can use the "e" option in sed command like this:
cat t.sh
myecho() {
echo ">>hello,$1<<"
}
export -f myecho
sed -e "s/.*/myecho &/e" <<END
ni
END
you can see the result without "e":
cat t.sh
myecho() {
echo ">>hello,$1<<"
}
export -f myecho
sed -e "s/.*/myecho &/" <<END
ni
END
Agree with Glenn Jackman.
If you want to use bash function in sed, something like this :
sed -rn 's/^([[:digit:].]+)/`date -d #&`/p' file |
while read -r line; do
eval echo "$line"
done
My file here begins with a unix timestamp (e.g. 1362407133.936).
Bash function inside sed (maybe for other purposes):
multi_stdin(){ #Makes function accepet variable or stdin (via pipe)
[[ -n "$1" ]] && echo "$*" || cat -
}
sans_accent(){
multi_stdin "$#" | sed '
y/àáâãäåèéêëìíîïòóôõöùúûü/aaaaaaeeeeiiiiooooouuuu/
y/ÀÁÂÃÄÅÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜ/AAAAAAEEEEIIIIOOOOOUUUU/
y/çÇñÑߢÐð£Øø§µÝý¥¹²³ªº/cCnNBcDdLOoSuYyY123ao/
'
}
eval $(echo "Rogério Madureira" | sed -n 's#.*#echo & | sans_accent#p')
or
eval $(echo "Rogério Madureira" | sed -n 's#.*#sans_accent &#p')
Rogerio
And if you need to keep the output into a variable:
VAR=$( eval $(echo "Rogério Madureira" | sed -n 's#.*#echo & | desacentua#p') )
echo "$VAR"
do it step by step. (also you could use an alternate delimiter , such as "|" instead of "/"
function parseDates(){
#Some process here with $1 (the pattern found)
return "dateParsed;
}
value=$(parseDates)
sed -n "s|[0-3][0-9]/[0-1][0-9]/[0-9][0-9]|& $value &|p" myfile
Note the use of double quotes instead of single quotes, so that $value can be interpolated
I'd like to know if there's a way to do this too. However, for this particular problem you don't need it. If you surround the different components of the date with ()s, you can back reference them with \1 \2 etc and reformat however you want.
For instance, let's reverse 03/04/1973:
echo 03/04/1973 | sed -e 's/\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9][0-9][0-9][0-9]\)/\3\/\2\/\1/g'
sed -e 's#[0-3][0-9]/[0-1][0-9]/[0-9][0-9]#& $(parseDates &)#' myfile |
while read -r line; do
eval echo "$line"
done
You can glue together a sed-command by ending a single-quoted section, and reopening it again.
sed -n 's|[0-3][0-9]/[0-1][0-9]/[0-9][0-9]|& '$(parseDates)' &|p' datefile
However, in contrast to other examples, a function in bash can't return strings, only put them out:
function parseDates(){
# Some process here with $1 (the pattern found)
echo dateParsed
}

Resources