Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I have the following piece of code: roleName =`cat $inputFile | awk -F';' '{ print $1 }'` roleDescription =`cat $inputFile | awk -F';' '{ print $2 }'` roleAuthProfile =`cat ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    1

    awk print specific columns one row at a time


    Hello,

    I have the following piece of code:


    roleName

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

    roleDescription



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

    roleAuthProfile



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

    mappedUserID



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

    mapType



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


    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.



    (for properties in $inputfile
    do: the piece of code above
    done)


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


    Does anyone know what I could be doing wrong?

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    Try this:

    Code:
    cat $inputfile | while read LINE ; do
      roleName=$(echo $LINE | cut -f1 -d\;)
      roleDescription=$(echo $LINE | cut -f2 -d\;)
      roleAuthProfile=$(echo $LINE | cut -f3 -d\;)
      mappedUserID=$(echo $LINE | cut -f4 -d\;)
      mapType=$(echo $LINE | cut -f5 -d\;)
      userID=$(echo $LINE | cit -f6 -d\;)
    
      <do the real stuff>
    done
    Hope that helps -- I have not tested it though.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Post your code, including the for loop part. I think the kicker is there.

  4. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    Ok. Here's my "data" file:

    Code:
    # roleName; roleDescription; roleAuthProfile; mappedUserID; mapType; userID
    rolename1;descr1;profile1;mappeduserid1;type1;userid1
    rolename2;descr2;profile2;mappeduserid2;type1;userid2
    rolename3;descr3;profile3;mappeduserid3;type1;userid3
    rolename4;descr4;profile4;mappeduserid4;type1;userid4
    rolename5;descr5;profile5;mappeduserid5;type1;userid5
    rolename6;descr6;profile6;mappeduserid6;type1;userid6
    And here is the script:

    Code:
    #!/bin/sh
    
    inputfile=data.txt
    
    cat $inputfile | grep -v ^# | grep -v ^$ | while read LINE ; do
      roleName=$(echo $LINE | cut -f1 -d\;)
      roleDescription=$(echo $LINE | cut -f2 -d\;)
      roleAuthProfile=$(echo $LINE | cut -f3 -d\;)
      mappedUserID=$(echo $LINE | cut -f4 -d\;)
      mapType=$(echo $LINE | cut -f5 -d\;)
      userID=$(echo $LINE | cut -f6 -d\;)
    
      echo "roleName: $roleName, roleDescription: $roleDescription, roleAuthProfile: $roleAuthProfile, mappedUserID: $mappedUserID, mapType: $mapType, userID: $userID"
    done
    Which produces the following output:

    Code:
    roleName: rolename1, roleDescription: descr1, roleAuthProfile: profile1, mappedUserID: mappeduserid1, mapType: type1, userID: userid1
    roleName: rolename2, roleDescription: descr2, roleAuthProfile: profile2, mappedUserID: mappeduserid2, mapType: type1, userID: userid2
    roleName: rolename3, roleDescription: descr3, roleAuthProfile: profile3, mappedUserID: mappeduserid3, mapType: type1, userID: userid3
    roleName: rolename4, roleDescription: descr4, roleAuthProfile: profile4, mappedUserID: mappeduserid4, mapType: type1, userID: userid4
    roleName: rolename5, roleDescription: descr5, roleAuthProfile: profile5, mappedUserID: mappeduserid5, mapType: type1, userID: userid5
    roleName: rolename6, roleDescription: descr6, roleAuthProfile: profile6, mappedUserID: mappeduserid6, mapType: type1, userID: userid6
    I can't see any problems with loops. And if you want to filter for special properties, just add a "grep <property" before "while read LINE" or do an "if [ "$mapType" == "property" ]" (if you're looking for 'property' at the column 'mapType') in front of the echo.

  5. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    yeah, there's no problem with loops, per se - i was just trying to fix the OP's code, in case there was a typo, etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •