I am just trying to convert the batch script to bash script and i am stuck at one point where I have the below code
for /f "delims=" %%a in (a.txt) do (
for /f "tokens=1,2,3* delims==" %%i in ("%%a") do (
for /f "tokens=1,2,3* delims= " %%x in ("%%i") do (
if "%%x"=="Group" (
echo %%i
set VAR1=%%i
set VAR2=%%j
The above code just look for a word "Group" before "=" sign and if it is present it will assign the LHS to VAR1 and RHS to VAR2.
a.txt looks like below
Username = #, %, $, ^
Group 1= Group A
Group 2= Group F, Group G, Group H, Group I,Group J
Country = AUS, NEW, RUS, ITA
I am just wondering is there a way to do the same in bash scripting? can anyone help me?
me@someplace:~/tmp$ cat x.sh
#!/bin/bash
while IFS='=' read LHS RHS
do
echo "[${LHS}]=[${RHS}]"
if [ "${LHS#Group}" != "${LHS}" ]
then
VAR1="${LHS}";
VAR2="${RHS}";
fi
done < a.txt
unset IFS
echo "VAR1:[${VAR1}]"
echo "VAR2:[${VAR2}]"
me@someplace:~/tmp$ ./x.sh
[Username ]=[ #, %, $, ^]
[Group 1]=[ Group A]
[Group 2]=[ Group F, Group G, Group H, Group I,Group J]
[Country ]=[ AUS, NEW, RUS, ITA]
VAR1:[Group 2]
VAR2:[ Group F, Group G, Group H, Group I,Group J]
By using below code, "Group 2" assigned to "VAR1" and "Group F, Group G, Group H, Group I,Group J" assigned to "VAR2", but my need is to assign all the Groups present on LHS to one variable and corresponding RHS to another variable.
expected output is:
VAR1[0] = Group 1
VAR2 [0]= Group A
VAR1[1] = Group 2
VAR2 [1]= Group F, Group G, Group H, Group I,Group J
If Group 3 present on a.txt the array should be increased as VAR1[2] and VAR2[2].