Parsing Lines from a text file

Hello Guru's

I know there are people who live to be able to help people like me. That's why I'm here!

I have a text file that has a corresponding address information that I need to be able to get maybe as a Unix Function.

The text file contains the following:

AIX17_JB_C;
scsidev@3.5.0;
AIX17_JB;
scsidev@3.3.0;
AIX17_JB_B;
scsidev@3.1.0;

I want to be able to "grep for" AIX17_JB and then it's corresponding scsidev@3.3.0 line.

Then later I may need AIX17_JB_B with it's corresponding scsidev@3.1.0.

Then I'll need to get AIX17_JB_C with it's corresponding scsidev@3.5.0.

Any help is greatly appreciated.

one way:

#!/bin/ksh

file='myTextFile.txt'
name='AIX17_JB'

getAddr() { # name
 typeset name="$1"

 nawk -F';' -v name="${name}" '$1 == name { getline; print $1; exit}' "${file}"
}

echo "name->[${name}] address->[$(getAddr ${name})]"

That's awesome vgersh99!

It works great. Now in layman's terms, what's it do? :smiley:

#!/bin/ksh

# the name of a file to search
file='myTextFile.txt'

# a name to search
name='AIX17_JB'

# function - takes 1 input parameter - the 'name' to look up
getAddr() { # name

#   assign the 1-st [and only] parameter to a locala variable 'name'
 typeset name="$1"

#   call 'nawk' with the Field separator set to ';' and a local nawk variable 'name'
#   set to the value of shell variable '${name}'
 nawk -F';' -v name="${name}" '
     # if the 1-st field is of the same value as the variable 'name'... execute the block
   $1 == name { 
            # get the NEXT line
        getline; 
            # print the 1-st field
        print $1; 
             # exit this run of 'nawk'
        exit
   }' "${file}"
}

#     this is obvious - print out the 'name' to look up AND call the 
#     function 'getAddr' with the variable 'name' as its only parameter
echo "name->[${name}] address->[$(getAddr ${name})]"