Find the answer to your Linux question:
Results 1 to 5 of 5
I am new to programming. I am stuck and hope someone can help. Input file looks like this: Rock - Franz Ferdinand - single - Do You Want To - ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    2

    How to use WHILE loop to do some processing

    I am new to programming. I am stuck and hope someone can help.
    Input file looks like this:
    Rock - Franz Ferdinand - single - Do You Want To - 3:55 - 235
    Rock - Ani DiFranco - Reprieve - Millenium Theater - 3:14 - 194
    Rock - Enter The Haggis - Soapbox Heroes - New Monthly Flavour - 3:23 - 203

    My WHILE loop is coded as below, but it's not working. It ended up printing every row, instead of the rows than meet the criteria $col6 -le $val1
    typeset -i var1=1000

    while IFS="-" read col1 col2 col3 col4 col5 col6
    do
    if $col6 -le $val1;then
    awk -F"-" '{print $1" - "$2" - "$3" - "$4" - "$5}' >> print.out
    fi
    done < data.dat

    Can someone please help?

  2. #2
    Just Joined!
    Join Date
    Jan 2007
    Location
    Tamil Nadu, India
    Posts
    18
    There is only simple mistake.

    Replace "if $col6 -le $val1;then" with "if [ $col6 -le $var1 ];then" and you will get the result.

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Location
    Tamil Nadu, India
    Posts
    18
    You need to enclose the conditions within [ ] brackets which are an alternate for 'test' command.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    var1=1000
    while IFS="-" read col1 col2 col3 col4 col5 col6
    do
        col6=${col6//[^0-9]/}
        case "$col6" in
            [0-9][0-9][0-9])echo "$col1 $col2 $col3 $col4 $col5 $col6" ;;
        esac
        # second way
        if [ "$col6" -lt "$var1" ];then
            echo "$col1 $col2 $col3 $col4 $col5 $col6"
        fi
    done < "file"

  5. #5
    Just Joined!
    Join Date
    Nov 2009
    Posts
    2
    Thanks! My code works after I put double brackets for the if condition and replaced the awk statement with echo.

    while IFS="-" read col1 col2 col3 col4 col5 col6
    do
    if [[ "$col6" -lt "$var1" ]];then
    echo "$col1 $col2 $col3 $col4 $col5"
    #awk -F"-" '{print $1" - "$2" - "$3" - "$4" - "$5}' >> print.out
    fi
    done < data.dat

Posting Permissions

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