NetSystemAdmin
NetSystemAdmin

Reputation: 545

Input from command line with incorrect result

I am trying to read a file that was outputted with table format. I am able to read the file that has Volume description, snapshotId and TimeStarted list from an aws region. I am asking user to input a volume name and output the snapshotId for the volume entered. The list contains volumes0 through volumes30.

The issue is, when user enters Volume0, it outputs the snapshotId correctly but if the user enters Volume20, it will only output |. My guess is because the original file being read is in table format, it is doing this. Initially I though I could put in a condition, where if user enters Volume0, print snapshotId else if user enters Volume20 then print snapshotid.

I am looking for a better way to do this. How can I ignore table format when reading the file, should I convert it to text format? How? Or how can I ignore any format when reading? Here is my bash script:

readoutput() {

  echo "Hello, please tell me what Volume you are searching for..(Volume?):"
    read volSearch
    echo "Searching for newest SnapshotIds from /Users/User/Downloads/GetSnapId for:" $volSearch
    sleep 5
    input="/Users/User/Downloads/GetSnapId"

    if x=$(grep -m 1 "$volSearch" "$input")
      then
        echo "$x"
    else
      echo "$volSearch not found..ending search"
    fi


        extractSnap=$(echo "$x" | grep "snap-" | awk '{print $7}')

        echo $extractSnap

}

readoutput

Upvotes: 0

Views: 41

Answers (1)

Saboteur
Saboteur

Reputation: 1428

The issue that awk is not too smart, and tries to determine table separator automatically. In first row with Volume0 you have space before vertical line, so it things that both of space and vertical line are separators, but in next row, you have no space, so it takes wrong column. Try next:

extractSnap=$(echo "$x" | cut -d'|' -f3 | cut -d'-' -f 2)

Upvotes: 1

Related Questions