Problems while reading each lines and performing operations in each line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • creeds
    New Member
    • Feb 2008
    • 14

    Problems while reading each lines and performing operations in each line

    Hello !!
    i have a text file, from where i have to perform an operation such as awk for every lines.
    i can count the number of lines with cat filename | wc -l , no problem with that.

    my scenario:
    i take a input from a file, i count the length ofcertian paramater with substring and do the if else condition.
    but i have to perform this action in every line....

    a code snippet...

    echo $File
    #count =$( cat /home/postgres/edt1/$File-spice-7676.txt.edt1 |wc -l)
    #echo $count
    #while [ "$count" != $check ]
    #do
    #count=$count-1

    for LINE in $( cat /home/postgres/edt1/$File-spice-7676.txt.edt1 )
    do
    v=$(awk -F "|" '{ print length($2)}' /home/postgres/edt1/$File-spice-7676.txt.edt1)

    echo $v

    if [ $v -lt $X ]

    then

    awk -F"|" '{print $1"|"substr($2, length($2)-6,length($2))"| "$3"|"$4}' /home/postgres/edt1/$File-spice-7676.txt.edt1 >> /home/postgres/edt1/test.txt
    echo " inside less"
    else
    echo " no"
    awk -F"|" '{print $1"|"$2"|"$3"|1 |"$4}' /home/postgres/$File-spice-7676.txt.edt > /home/postgres/edt1/test.txt

    fi
    done

    ..........
    i am not able to proceed with LINE as it thinks as a new line when it finds spaces within a line,
    i tried to count the number of lines in the file and think of decresing its value evey time, but also i didnt get the result

    my file /home/postgres/edt1/$File-spice-7676.txt.edt1 )
    is sum wot like that...

    2008-04-18 11:11:52|+97798 04808287|spice-6464|16|
    2008-04-18 11:11:52|spice-7676|+977980480 8287|16|
    2008-04-18 11:12:43|+97798 04808287|spice-6464|16|
    2008-04-18 11:12:43|spice-7676|+977980480 8287|16|
    2008-04-18 11:14:04|+97798 04808287|spice-6464|16|

    so how can i perform this task

    thanks for any suggestion
    with regards,
    Creeds
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    change this line:
    for LINE in $( cat /home/postgres/edt1/$File-spice-7676.txt.edt1 )

    to:

    cat /home/postgres/edt1/$File-spice-7676.txt.edt1 | while read LINE

    Comment

    • creeds
      New Member
      • Feb 2008
      • 14

      #3
      hello
      yeah i already did that, but i again got strucked with

      if [ "$V" -gt "$X" ]
      the error shows unary operator expected,
      i tried with all combination like "" or "$x" or "10" etc etc,
      but shows error like interger expected or unary operator error...
      so hw to solve this basic problem???
      with regards,
      suman

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Originally posted by creeds
        hello
        yeah i already did that, but i again got strucked with

        if [ "$V" -gt "$X" ]
        the error shows unary operator expected,
        i tried with all combination like "" or "$x" or "10" etc etc,
        but shows error like interger expected or unary operator error...
        so hw to solve this basic problem???
        with regards,
        suman

        just print values of $V and $X
        to use -gt/-lt/-eq in comparison input must be integers...

        Comment

        • creeds
          New Member
          • Feb 2008
          • 14

          #5
          Hello
          Thanks for suggestion ,
          now i cud solve my problem but with a lengthy process by sendign value to a file and getting its value from there..it was lengthy but i cud do my work now still it gave me sum headache
          --------------------------------
          do
          echo $line
          V= echo $line |awk 'BEGIN{FS="|"} {print length($3)}' > /home/postgres/v.txt
          #echo "THE V IS:"$V
          #echo "THE V IS:"$V
          Y=$(awk '{print $0}' /home/postgres/v.txt)
          echo "THE Y IS:"$Y
          if [ "$Y" -gt "14" ]; then
          echo "greater"
          echo $line |awk 'BEGIN{FS="|"} {print $1"|"$2"|"subst r($3,length($3)-13,length($3))" |1|"$4}' >> /home/postgres/edt1/final.txt
          else
          echo "lesser"

          echo $line |awk 'BEGIN{FS="|"} {print $1"|"$2"|"$3"|1 |"$4}' >> /home/postgres/edt1/final1.txt
          fi
          done

          thanks
          Suman

          Comment

          Working...