conversion of SQL IN clause to multile ORs - algorithm

I have a string "VAR1 = ABCD AND VAR2 IN ('AB' , 'CD' , 'EF')"
I want to convert that into something like this
VAR1 = ABCD AND (VAR2 ='AB' OR VAR2= 'CD' OR VAR3 = 'EF')
what is the best method to handle this via an algorithm ?

Related

Best way to alter a file in a bash script [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
i have a file that have variables and values
objective: open the file and replace all id by input id
[FILE]
var1 = 2
id = 3
var3 = 5
id = 12
var4 = 5
and i can't replace the id values to new ones.
here's my code, any help or something will help. thanks
#!/bin/bash
filename=$1
uuid=$2
input="./$filename"
# awk -v find="id " -v field="5" -v newval="abcd" 'BEGIN {FS=OFS="="} {if ($1 == find) $field=newval; print $1}' $input
while IFS= read -r line
do
awk -v find="id " -v field="5" -v newval="abcd" 'BEGIN {FS=OFS="="} {if ($1 == find) $field=newval;}' $input
echo $line
done < "$input"
expected output
execute
./myscript.sh file.cnf 77
expected output:
[FILE]
var1 = 2
id = 77
var3 = 5
id = 77
var4 = 5
I think sed is the right tool for this. You can even use its -i switch and update the file in-place.
$ cat file.txt
var1 = 2
id = 3
var3 = 5
id = 12
var4 = 5
$ NEW_ID=1234
$ sed -E "s/(id\s*=\s*)(.+)/\1${NEW_ID}/g" file.txt
var1 = 2
id = 1234
var3 = 5
id = 1234
var4 = 5
The string inside the quotes is a sed script for substituting some text with different text, and its general form is s/regexp/replacement/flags where "regexp" stands for "regular expression".
In the above example, the script looks for the string "id = ..." with any number of spaces or tabs around the "=" character. I divided the regexp into 2 groups (using parentheses) because we only want to replace the part to the right of the "=" character, and I don't think sed allows partial substitutions, so as a workaround I used \1 in the "replacement", which inserts the contents of the 1st group. The ${NEW_ID} actually gets evaluated by the shell so the value of the variable ("1234") is already part of the string by the time sed processes it. The g at the end stands for "global" and is probably redundant in this case. It makes sure that all occurrences of the regex on every line will get replaced; otherwise sed would only replace the first occurrence on each line.
Not sure. Bash scripts are extremely sensitive. I'm guessing your touch is what is causing this issue for a couple of reasons.
First whenever you touch a file name is should not consist of an operand or prefix unliss it is part of the shell script and $filename is shell or inline block quote. Touch is usually used for binaries or high priority data objects.
Second I'd try changing input and adjusted to $done and instead of echoing the $line echo the entire script using esac or end if instead of a do while loop.

Read and Tokenize from a file in Bash

I want to read something from a file and tokenize it into several variables in Bash, but I'm not sure how.
Example:
Enter file path: foo/bar.txt
var1 = 'a'
var2 = 'b'
var3 = 'c'
var4 = 'd'
The contents of bar.txt would simply be "a,b,c,d". It would have to be a one line file. I was thinking of using grep somehow. Is there an easy way of doing this, or am I making things to complicated?
IFS=, read var1 var2 var3 var4 < bar.txt
Set field separator
Set input file
Set resultant variables

Unable to execute printf command by using formatter and argument variable

I am trying to store all the printf formatter and arguments into their own respective variables to be execute later. Example code:
var="abc123"
var2="def 456"
printfArgument=$var" "$var2
formatter="%-10s"
formatter2="%-10s"
printfFormatter=$formatter" "$formatter2"\n"
printf "$printfFormatter" $printfArgument
output:
abc123 def
456
It seems like the space in var2 causes the 456 to display improperly. Any way to fix it?
You're correct; the space in var2 is being used for word-splitting. printf is receiving 3 arguments after the format string: abc123, def, and 456. The first two fill the two format specifiers for the first line of output. Since there is a remaining argument, the format string is used again to produce the second line of output.
You need to use an array for printfArgument:
printfArgument=( "$var" "$var2" )
printf "$printfFormatter" "${printfArgument[#]}"
or just use var and var2 separately:
printf "$printfFormatter" "$var" "$var2"

Use of variable name in shell script

First of all I'm sorry for a probable bad title, but I don't even know what to call this.
I'm trying the following:
#!/bin/sh
VAR1="28-00000202070c"
VAR2="28-0000018776d3"
VAR3="28-0000033a6174"
for sensor in VAR1 VAR2 VAR3
do
echo "$sensor: $$sensor"
done
The expected output would be:
VAR1: 28-00000202070c
VAR2: 28-0000018776d3
VAR3: 28-0000033a6174
The real output is:
VAR1: 24038sensor
VAR2: 24038sensor
VAR3: 24038sensor
and the strange prefix number keeps growing...
VAR1: 24039sensor
VAR2: 24039sensor
VAR3: 24039sensor
...
I'd like to ask:
1) What are the correct terms/keywords that describe what I'm trying to do here
2) How to get to the expected output
Thanks,
Joaoabs
This is something that sh does not support, while bash does.
The correct syntax you should use is:
echo "$sensor ${!sensor}"
Test
$ cat a
#!/bin/bash <----- note I changed /bin/sh to /bin/bash
VAR1="28-00000202070c"
VAR2="28-0000018776d3"
VAR3="28-0000033a6174"
for sensor in VAR1 VAR2 VAR3
do
echo "$sensor ${!sensor}"
done
$ ./a
VAR1 28-00000202070c
VAR2 28-0000018776d3
VAR3 28-0000033a6174

In a shell script, how do I replace the first occurrence of a string, after a different string

I have a simple config file that looks a bit like this:
[sectionA]
url = value1
username = value2
password = value3
[sectionC]
url = value1
username = value2
password = value3
[sectionB]
url = value1
username = value2
password = value3
And I want to replace the username for SectionB to be valueX without touching SectionA's or SectionC's username.
I've tried some variations on sed, but as far as I've managed to fathom it seems to operate on individual lines.
How do I do the equivalent of
Search for StringA (in this case [SectionB])
Find the next occurrence of StringB (username = value2)
Replace with StringC ('username = valueX`)
sed:
sed '/sectionB/,/\[/s/username.*/username = valueX/' input
awk:
awk -vRS='' -vFS='\n' -vOFS='\n' '
$1~/sectionB/{
sub(/=.*$/, "= valueX", $3)
}
{
printf "%s\n\n", $0
}' input
This multi-line sed should do the trick:
sed -E -n '1h;1!H;${;g;s/\[sectionB\]([^[]*)username = [a-zA-Z0-9_]+/\[sectionB\]\1username = valueX/g;p;}' input.txt

Resources