awk print specific columns one row at a time

Hello,

I have the following piece of code:

roleName

[LEFT]=`cat $inputFile | awk -F';' '{ print $1 }'`

roleDescription[/LEFT]

[LEFT]=`cat $inputFile | awk -F';' '{ print $2 }'`

roleAuthProfile[/LEFT]

[LEFT]=`cat $inputFile | awk -F';' '{ print $3 }'`

mappedUserID[/LEFT]

[LEFT]=`cat $inputFile | awk -F';' '{ print $4 }'`

mapType[/LEFT]

[LEFT]=`cat $inputFile | awk -F';' '{ print $5 }'`
userID=`cat $inputFile | awk -F';' '{ print $6 }'`[/LEFT]

What I'm trying to do is extract input from specific columns for a script. I'm using the ";" as my delimeter.

The problem I'm having is that even though I'm putting this code in a loop, I'm getting the value of the whole column as my variable rather than the string within the delimiter.

[LEFT](for properties in $inputfile
do: the piece of code above
done)[/LEFT]

I want to get one line at a time from the specified column.

Does anyone know what I could be doing wrong?

That's a useless use of cat, you don't need to run cat all the time.

What does your input data look like and what data from it do you want? There's probably better ways than running awk in backticks all the time too, like

while IFS=";" read roleName roleDescription roleAuthProfile mappedUserID mapType userID G
do
        echo "column 1 is ${roleName}"
        ...
done < infile

This is more straightforward and hundreds of times faster.

1 Like

Study this example:

% cat >INPUTFILE
x;y;z
% IFS=';' read a b c <INPUTFILE
% echo $a $b $c
x y z

Oops.

[LEFT]Role_1_Name_Example; Role Description 1; DB Auth Profile 1; user1; 1; user1@DBG.ADS.DB.COM
Role_2_Name_Example; Role Description 2; DB Auth Profile 2; user2; 999; user2@DBG.ADS.DB.COM
Role_3_Name_Example; Role Description 3; DB Auth Profile 3; user3; 999; user3@DBG.ADS.DB.COM
Role_4_Name_Example; Role Description 4; DB Auth Profile 4; user4; 1; user4@DBG.ADS.DB.COM
Role_5_Name_Example; Role Description 5; DB Auth Profile 5; user5; 1; user5@DBG.ADS.DB.COM
[/LEFT]

Great, now please answer the other half of my question: