Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    awk print specific columns one row at a time


    I have the following piece of code:


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


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


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


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


    =`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

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

    Does anyone know what I could be doing wrong?

  2. #2
    Try this:

    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>
    Hope that helps -- I have not tested it though.

  3. #3
    Post your code, including the for loop part. I think the kicker is there.

  4. $spacer_open
  5. #4
    Ok. Here's my "data" file:

    # roleName; roleDescription; roleAuthProfile; mappedUserID; mapType; userID
    And here is the script:

    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"
    Which produces the following output:

    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.

  6. #5
    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