Find associated strings in a bash shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find associated strings in a bash shell script
# 1  
Old 08-07-2011
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 :-(
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:
Code:
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 :-) ?
=> 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:
Code:
Hans Dampf
Klaus Krampf
....
sack mack

For me (still :-) 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 :-)

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 :-)

Last edited by fpmurphy; 08-07-2011 at 11:23 AM..
# 2  
Old 08-07-2011
probably not the best way ...but this may work


say sample is the file which is having input then
Code:
 
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


Last edited by fpmurphy; 08-07-2011 at 11:24 AM.. Reason: code tags please!
This User Gave Thanks to shipra_31 For This Post:
# 3  
Old 08-07-2011
Base on data-sample using bash only
Code:
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.
This User Gave Thanks to danmero For This Post:
# 4  
Old 08-07-2011
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 "." :-( 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 ?

Last edited by fpmurphy; 08-07-2011 at 11:25 AM.. Reason: correct numerous spelling errors
# 5  
Old 08-07-2011
Try to provide a complex data sample and required output.
# 6  
Old 08-07-2011
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 :-)
# 7  
Old 08-07-2011
Quote:
Originally Posted by Sofie
---------- Previous update was at 11:19 AM ----------[/SIZE][/COLOR]

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 :-)
Base on original data sample using only bash shell
Code:
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


Last edited by danmero; 08-07-2011 at 03:48 PM..
This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Perl script to find process and exclude strings from the output

Hi team, I'm a newbie of Perl Script and looking to create a simple perl script that will run in the Linux system: 1) to find process, such as ps -ef | grep process name 2) to exclude strings from the output if it found, for instance if i see abc from usr process, then will exclude it from... (1 Reply)
Discussion started by: hoffman2503
1 Replies

4. Shell Programming and Scripting

Script to find NOT common strings in two files

Hi all, I'd like you to help or give any advise about the following: I have two (2) files, file1 and file2, both files have information common to each other. The contents of file1 is a subset of the contents of file2: file1: errormsgadmin esdp esgservices esignipa iprice ipvpn irm... (18 Replies)
Discussion started by: hnux
18 Replies

5. Shell Programming and Scripting

Script to find NOT common strings in two files

Hi all, I'd like you to help or give any advise about the following: I have two (2) files, file1 and file2, both files have information common to each other. The contents of file1 is a subset of the contents of file2: file1: errormsgadmin esdp esgservices esignipa iprice ipvpn irm... (0 Replies)
Discussion started by: hnux
0 Replies

6. Shell Programming and Scripting

Simple script to find common strings in two files

Hi , I want to write a simple script. I have two files file1: BCSpeciality Backend CB CBAPQualDisp CBCimsVFTRCK CBDSNQualDisp CBDefault CBDisney CBFaxMCGen CBMCGeneral CBMCQualDisp file2: CSpeciality Backend (8 Replies)
Discussion started by: ramky79
8 Replies

7. Shell Programming and Scripting

Find out the day in Bash Shell script

Hello All, I need a bash shell script to find out a day from the date.For example we give the date(20100227/YYYYMMDD) then we get the day 'Saturday'. Thanks in advance, Satheesh (5 Replies)
Discussion started by: satheesh4093
5 Replies

8. Shell Programming and Scripting

Quoting issue: Trouble with bash strings in script

I can do this on the command line: sqsh -S 192.168.x.x -o tmp -U user -P fakepass -D horizon -C "\ select second_id from borrower where btype like '%wsd%' " I can also just leave the SQL at the end intact on one line .... ... However, when I throw this in a script like: $SQSH -o... (4 Replies)
Discussion started by: Bubnoff
4 Replies

9. Shell Programming and Scripting

Shell script to replace strings to and from a file

Hello All, I have 2 files 1 ) source file eg asasa 1.2.3.4 adfhsdfsdfasdf zxzxzx 2.3.4.56 dsadasdasdsadasd kjjkjkjk 30.3.4.5 asdsadsadsadsadsad vxcvxcvx 1.2.3.4 qwewqewqeqweqwe 2) patern file 1.2.3.4 A 2.3.4.56 B 30.3.4.5 C I need the source to be changed to asasa A... (2 Replies)
Discussion started by: nitinkgoud
2 Replies

10. Homework & Coursework Questions

strings in shell script!

hi ppl, I have a basic doubt as to how shell treats strings. e.g:given a line of data like this "5","6","45","77","89" extracting the value from each field to a variable will conatin a string("5") or just the number? If it conatins "5", how do we perform mathematical operations with that... (3 Replies)
Discussion started by: beetel
3 Replies
Login or Register to Ask a Question