You are not logged in.

#1 2008-11-27 05:00:58

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Script isnt reading the file

So this might be t he problem..

if [[ $lines =~ "^Total:" ]]; then

Just wanting the lines in the file that start with "Total:"
Heres the script as a whole:

for file in $*
do

              if [ -e $file ]; then
                 echo "$file exists"
              fi
      while read lines
do
                    if [[ $lines =~ "^Total:" ]]; then
                        echo `$lines | cut -d':' -f5`

#cant even print an arbitrary value
                        echo "1"

                    fi
done > $file


done

Im checkn to see if the file exists, and if it does i want to read the contents of it. But its not giving me anything..

I have a sample file that im reading:

Total number of digits in pi: 101
I've tried awk to emulate the cut command and different operators for the first if contional...

omg its deleting everything i've been trying to read..LOL

Last edited by oxoxo (2008-11-27 05:09:26)

Offline

#2 2008-11-27 05:26:22

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Script isnt reading the file

Ok so i got my value printed:

total="0"
for file in $*
do

        echo "Processing $file"
        while read line
        do
         if [[ $line =~ "^Total:" ]];
          then

             tmp=`$line | cut -d':' -f4`

             total=`expr $total + $tmp`
             echo "$total"
         fi
        done < $file
done
echo "$line"

But why isnt it cutting the feilds?

Offline

#3 2008-11-27 07:53:20

klixon
Member
From: Nederland
Registered: 2007-01-17
Posts: 525

Re: Script isnt reading the file

please post a relevant portion of the input files and the reason why you think the fields are not cut (is the output not right? if so, please post the output)


Stand back, intruder, or i'll blast you out of space! I am Klixon and I don't want any dealings with you human lifeforms. I'm a cyborg!

Offline

#4 2008-11-27 12:51:59

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Script isnt reading the file

I'm not sure if this is what's going wrong, but you can't use quotes there:

$ [[ "Total" =~ ^To ]] && echo yes
yes
$ [[ "Total" =~ "^To" ]] && echo yes

Offline

#5 2008-11-28 05:49:40

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Script isnt reading the file

Ultimately what im trying to do is read multiple files.

in my script im using $file as the file specification, so when i enter  ./script test[12].txt it should read both test1.txt and test2.txt and then search for lines that match my regex in every file i specify.

What i've been trying to do is:
1) Make sure all files that are specified are read. ;; done
2) Look for a pattern in all of the file specifcations and print the value ( should be last feild ) ;; kinda done;;
3) Take that field and add it to a running count or total for each file specfied ;; the goal;;
..

So for item 3 I would need to increment some varaible with the value from the next file, and so on.
Example:

Givn that:
for test2.txt: "Total number of digits in Pi: 102"
for test3.txt: "Total number of digits in Pi: 103"


$ ./script test[1].txt
Processing: test1.txt
101
----------------------------

$ ./script test[12].txt
Processing: test1.txt
Processing: test2.txt
203

--------------------------------------------------------------------------------
Whereas my Output reads the last file and not both of them together.
--------------------------------------------------------------------------------

$ ./script test[12].txt
Processing test1.txt
Processing test2.txt
Total number of digits in Pi: 102

Also my cut command decided not to work today, so im unable to read the last field..and i've tried the awk $NF but its not being nice.

total="0"
for file in $*
do

        echo "Processing $file"
        while read line
        do
         if [[ "$line" =~ ^Total: ]];
             then

             Result=$( echo $line | awk '{ print $NF }' )

             total=`expr $total + $Result` # try to add a running count of each file spec
             echo "Total is $total"
         fi
        done < $file
done

echo "$Result"
echo "$total"

Last edited by oxoxo (2008-11-28 05:50:28)

Offline

#6 2008-11-28 11:50:14

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Script isnt reading the file

I copied your script exactly:

$ bash script.sh myfile[123].txt
Processing myfile1.txt
Total is 5
Processing myfile2.txt
Total is 10
Processing myfile3.txt
Total is 15
5
15

That makes me wonder what the actual files look like. I ran this on some that only contained:

Total: 5

Offline

#7 2008-11-28 14:44:25

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: Script isnt reading the file

You may be having several problems. Here are two of them (you may have discovered one of both of these already on your own)...

oxoxo wrote:
for file in $*
...
                        echo `$lines | cut -d':' -f5`
...
done > $file
...

When you redirect output to $file in this way, the shell will truncate $file to 0 bytes and start writing to it as soon as the code block whose output is being redirected starts. In your case, I think that will mean that $file will be empty when you try to read from it. You need to redirect output to a different file than you're reading from.

Edit: I see now from your second post that you really wanted

done < $file

.

Second, why are you trying to extract field 5 with your cut command? From the sample data you posted, the lines only had a single colon in them, and you wanted the data after that colon. So that would be:

cut -d':' -f2

Last edited by Profjim (2008-11-28 14:46:55)

Offline

#8 2008-11-28 20:34:54

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Script isnt reading the file

Procyon wrote:

I copied your script exactly:

$ bash script.sh myfile[123].txt
Processing myfile1.txt
Total is 5
Processing myfile2.txt
Total is 10
Processing myfile3.txt
Total is 15
5
15

That makes me wonder what the actual files look like. I ran this on some that only contained:

Total: 5

Thats strange how its working almost how it should for u, but not for me..

Did u change anything?

The problem im having now is that its not cutting the feilds..
Heres my current script:

#!/bin/bash

total="0"
for file in $*
do

        echo "Processing $file"
        while read line
        do
         if [[ $line =~ "^Total:" ]];
             then

             tmp=`$line | cut -d':' -f2`

             total=`expr $total + $tmp`
             echo "Total is $total"
         fi

        done < $file
done
echo "$line"

Very Mysterious in its Simplicity

cat hi.txt | grep "^Total" | cut -d':' -f2

Yes, but my script needs to do that..lol

Last edited by oxoxo (2008-11-28 20:42:48)

Offline

#9 2008-11-28 21:21:37

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Script isnt reading the file

I used this script in post #6, which is the same as yours in #5 except $(..) instead of `..`

total="0"
for file in $*
do

        echo "Processing $file"
        while read line
        do
         if [[ "$line" =~ ^Total: ]];
             then

             Result=$( echo $line | awk '{ print $NF }' )

             total=$(expr $total + $Result) # try to add a running count of each file spec
             echo "Total is $total"
         fi
        done < $file
done

echo "$Result"
echo "$total"

The above script #8 has some errors:
quotes around the second argument in [[ ]]
`$line ... instead of `echo $line ...
at the end echoing $line instead of $total

If I use:

#!/bin/bash

total="0"
for file in $*
do

        echo "Processing $file"
        while read line
        do
         if [[ "$line" =~ ^Total: ]];
             then

             tmp=$(echo $line | cut -d':' -f2)

             total=$(expr $total + $tmp)
             echo "Total is $total"
         fi

        done < $file
done
echo "$total"

the output is correct:

$ bash script2.sh myfile[12].txt 
Processing myfile1.txt
Total is 5
Processing myfile2.txt
Total is 10
10

Offline

#10 2008-11-28 22:29:13

oxoxo
Member
Registered: 2008-10-31
Posts: 66

Re: Script isnt reading the file

I dont understand why this:

cat hi.txt | grep "^Total:" | cut -d':' -f2

Isnt doing the same thing as this:

tmp=$(echo $line | cut -d':' -f2)

when i echo the varaible in my script


And its finding the line with total in this text file

OK so now my task is to cut the fields
shortly after, depending if im called or not is to;
add the values together and display the total..
Total Number of digits in Pi: 103

But not in this text file

******************************************
I think its just opening it.


Nope.. fhgfhjgjhg.

Total number of digits in Pi: 102

But my main concern is displaying the last feild of which awk is incapable of doing?

tmp=$( echo $line | awk '{ print $NF }' )

....

Is this even right?

while read line
do
if [[ "$line" =~ ^Total: ]]; then
echo "$line"
done

Since im specifying a file, it should use the line varaible to read through the text file..
What exactly am i using their? grep?
It doesnt make any sense why its not working for another file...it has the exact line that it matched for previously, its just a larger file..
Why does the cut command hate me?

Last edited by oxoxo (2008-11-28 22:46:54)

Offline

#11 2008-11-28 22:54:25

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Script isnt reading the file

Total Number of digits in Pi: 103

It doesn't start with Total:

If I use this code with the text file you specified it gives the right result:

#!/bin/bash

total="0"
for file in $*
do

        echo "Processing $file"
        while read line
        do
         if [[ "$line" =~ ^Total ]];
             then
             tmp=$(echo $line | cut -d':' -f2)

             total=$(expr $total + $tmp)
             echo "Total is $total"
         fi

        done < $file
done
echo "$total"

Offline

Board footer

Powered by FluxBB