Find associated strings in a bash shell script

Hi together,
unfortunately I am not a shell script guru - the following might touch
the depths of awk, substr, split, regexps, where I am still fighting with - but as always the boss needs a fast solution :frowning:
So: I have the following USER/PASSWORD-installation-config-file, from where I want to read out the BELONGING TOGETHER user/passwords pairs to postprocess them in an sqlplus command to test if those users have already an account on the database:

Text1.admin.user=Hans
Text1.admin.password=Dampf

text1.access.user=Klaus
text1.access.password=Krampf

text2.admin.user=Willi
text2.admin.password=stumpf

text2.access.user=heiner
text2.access.password=keiner

text3.admin.user=reiner
text3.admin.password=nocheiner

text3.access.user=thomas
text3.access.password=owas

text4.admin.user=kalli
text4.admin.password=malli

text4.access.user=sack
text4.access.password=mack

Conditions here are:
=> always the same in the strings is "access.user=<username>" (resp. "admin.user=<username>")
=> always the same in the strings is also "access.password=<passwort>" (resp. "admin.passwort=<passwort>")
=> for a belonging together pair for example "admin.user/admin.password" the preceding text (for example "text4") is always the same (the same for the accompanying "access user")
=> the exact text-prefixes I don't know at the time of the shell script run (I know only that they are the same for a belonging together pair of user-password; otherwise I could go there with an exaxt grep on the prefix text and split afterwards with awk -F\= .... )
=> of course there could be also more lines than these examples; so I have to search what pairs are belonging together, according to the prefix text, but how :slight_smile: ?
=> the lines could also stand disordered
=> So as an OUTPUT FILE I would need only the belonging together
user<blank>password pairs regardless if it is admin or access user, must also not be "ordered" necessarily, but with the belonging together user/password pairs:

Hans Dampf
Klaus Krampf
....
sack mack

For me (still :slight_smile: the algorithmic difficulty is, that I don't want to bring mistakenly
for example the "text3.access.user" with the "text1.access.password" together, but the pairs with the same text prefix :slight_smile:

Now I hope, that I did not describe my problem to extensive and that it can be understood.
For a quick help I would be very thankful :slight_smile:

probably not the best way ...but this may work

say sample is the file which is having input then

 
rm -rf one.txt output.txt
grep -i user sample >one.txt
for i in `cat one.txt`
do
 a=`echo $i | awk -F "." '{print $1}'`;
 b=`echo $i | awk -F "." '{print $2}'`;
 c=`grep $a sample  |grep user |grep $b |awk -F "=" '{print $2}'`;
d=`grep $a sample  |grep password |grep $b |awk -F "=" '{print $2}'`;
 echo $c $d >>output.txt;
 done

cat output.txt
1 Like

Base on data-sample using bash only

IFS=\.;while read a b c;do if [ ! -z  $c ];then declare $c;fi;if [ -z $c ];then echo $user $password;fi;done < infile ; echo $user $password

Replace echo by your own action.

1 Like

Thank you both first of all, I really appreciate people here: but the problem I described is a bit harsher here:

  • The "text" prefix can contain "." characters also for example: "dsds.fdfdf.sgdsg" and it is definitely like this, so I cannot say the whole string can be divided by three sub-strings separated by "." :frowning: So I think one has to search explicitly for the patterns for example "admin.user" and compare the text in front to retrieve the correct user/password combination .... Hmm ?

Try to provide a complex data sample and required output.

Hi Danmero,

the more complex realistic data sample could be (instaed of the "textx"
aa.bbb.ccc.admin.user=Hans aa.bbb.ccc.admin.password=Dampf aa.bbb.ccc.access.user=Klaus aa.bbb.ccc.access.password=Krampf gggg.hh.zzz.tttt.admin.user=Willi
gggg.hh.zzz.tttt.admin.password=stumpf gggg.hh.zzz.tttt.access.user=heiner gggg.hh.zzz.tttt.access.password=keiner

# and so on: "textx" is a substring with a variable number of substrings
# delimited by ".", but is each the same for the user/password pair
# belonging together

text3.admin.user=reiner text3.admin.password=nocheiner text3.access.user=thomas text3.access.password=owas text4.admin.user=kalli text4.admin.password=malli text4.access.user=sack text4.access.password=mack

THE reuired output is a file like given before with
the user password pairs:

Hans Dampf
Willi Stumpf
....
.....

Thank you !

---------- Post updated at 11:22 AM ---------- Previous update was at 11:19 AM ----------

Oh i see the input file is not formatted like in my first post, so the lines have a carriage return after the username and password :slight_smile:

Base on original data sample using only bash shell

while read line;do line="${line/${line%.*.*}/}";a=( ${line//[.=]/ } );case ${a[1]} in user) b[${a[0]}]=${a[2]};;password)echo ${b[${a[0]}]} ${a[2]};;*);;esac;done <file
Hans Dampf
Klaus Krampf
Willi stumpf
heiner keiner
reiner nocheiner
thomas owas
kalli malli
sack mack

1 Like

Nice bash shell solution danmero.

Here is one using awk:

awk -F[.,=] '
   NF && $(NF-1) == "user" { usr=$NF}
   NF && $(NF-1) == "password" { print usr " "$NF } ' infile
1 Like

Thank you I will try it out !! and let you know :slight_smile:
Thank you

For a disordered list i'll opt for:

awk -F[.=] '!NF{next}$(NF-1)=="user"{x=$NF;sub($(NF-1)"="$NF,y);a[$0]=x}$(NF-1)=="password"{x=$NF;sub($(NF-1)"="$NF,y);if(a[$0])b[$0]=x}a[$0] && b[$0]{print a[$0],b[$0]}' file
1 Like

My solution missed a requirement:
=> for a belonging together pair for example "admin.user/admin.password" the preceding text (for example "text4") is always the same (the same for the accompanying "access user")

This update should address that:

awk -F[.,=] '
   NF && $(NF-1) == "user" { usr=$NF; sub("user=.*","");pre=$0}
   NF && $(NF-1) == "password" && pre &&
   substr($0,0,length(pre)) == pre { print usr " "$NF;pre = ""} ' infile
1 Like