Reading in two lines at once from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading in two lines at once from a text file
# 1  
Old 04-13-2013
Reading in two lines at once from a text file

Hello everyone,

I have thought about this for quite some time and know what I want to do but am having some trouble at it. I have a text file filled with numbers like this, there are more in the file obviously. Each number is separated by a space.

Code:
1  3  2  4   5
1 -1  1 0  -1 5

The idea is to use these numbers to calculate some kind of a profit. The first line is price per item and the second line indicates an action. 1 means you bought it, -1 means you sold it, 0 means no action. So, to demonstrate things, you buy an item at $1, the running total is -1, you sell (add 3), subtract 2, ignore the no transaction, then add 5, leaving you with 5. Anyway, I have a script called test and if you run it, it allows you to enter a line of numbers separate by spaces, when you hit enter it displays the second line results. I think the idea is to feed lines in from testcase.txt to the check.sh file (the one I'm scripting now) and compare the program output with the expected output in testcase.txt. So, if I can get every odd line from testcase and feed it in, read the result output, then I can compare it. Is there a good way to do this? To call a .sh file within a .sh file?

Code:
set lines=`cat $PWD/testcase.txt`
set i=1

while ($i <= $#lines)
    echo $lines[$i]
    @ i = $i + 1
end

Oh yeah, if I can somehow feed a line from testcase to test.sh and store its result in a variable (ala the second line), then there might be a way to get it done.

Last edited by tastybrownies; 04-13-2013 at 05:48 PM.. Reason: Clarification
# 2  
Old 04-13-2013
To make things easier, I would convert to single lines first:
Code:
$ uname
Linux

Code:
$ cat testcase.txt
1  3  2  4   5
2 -1  1 0  -1 5
3  3  2  4   5
4 -1  1 0  -1 5

Code:
$ cat testcase.sh
sed "N; s/\n/ /" testcase.txt > testcase-2.txt
while read var1 var2 var3; do
  echo $var1
  # do whatever else you need
done < testcase-2.txt

Code:
$ ./testcase.sh
1
3

# 3  
Old 04-13-2013
I am not sure I understand what you are after, but I suppose you could try doing something like this to compare the calculated result with what is in the last column of every second file:

Code:
#!/bin/bash
while read -a P && read -a S
do
  t=0
  for((i=0; i<${#P[@]}; i++)); do
    (( t -= P[i] * S[i] ))
  done
  printf "calculated: %d, file: %d\n" $t ${S[i]}
done < file

awk:
Code:
awk '{split($0,P); getline; t=0; for(i=1; i<NF; i++) t-=P[i]*$i; printf "calculated: %d, file: %d\n",t,$NF}'  file

# 4  
Old 04-13-2013
Actually I think I have a better understanding of what I'm after now. I wrote out some psuedo code with what I'm trying to do but am unsure how to get each line from a file and send each line to the script.

Code:
set dir="$PWD/./test"
set dir2="$PWD/testcase.txt"
set ODD=""
set EVEN=""
set oddlines="sed -n 1~2p dir2 > ODD"
set evenlines=" awk 'NR%2==0' dir2 > EVEN"

set c=0

foreach line in oddlines
  result from test = test (line from odd)
  foreach line in evenlines
    if total from even matches total from test
       this means that this is successful test case
       output something with echo
    endif
  end
end

I apologize for before. What I wrote was kind of confusing and even I didn't fully understand what I was trying accomplish. Now I am sure what I want to do.

Last edited by tastybrownies; 04-13-2013 at 07:51 PM.. Reason: edited
# 5  
Old 04-13-2013
You can't just subtract numbers from the odd lines, it would need to be the sum product of the fields of the odd and even lines, no? That is what I used in the bits of code I suggested...
# 6  
Old 04-14-2013
Yes, you are correct in what you say about the sum. Either doing that or seeing whether the return line provided from the function is the same as the one in the testcase file. I know diff works for files but maybe I can use it for smaller cases.Smilie

---------- Post updated at 03:04 AM ---------- Previous update was at 02:06 AM ----------

Okay, I'm at the point where I am trying to pass an argument(odd line) to the ./test program and it keeps saying "command not found." I am trying to get the return value and store it in a variable. If I just run ./test in the terminal and hit enter, the cursor goes to the next line and I can enter something. When I call ./test in this way inside a script, it doesn't call it like this does it? ./test 0 0 0

Hopefully not. I know some of this code is repetitive and needs cleaned up but I am just trying to figure out how to get a value back from this program. Once I do this I can compare the line I got back to the even line in the evens file. Sorry, I am kind of new to shell programming so my code may look bad.

Code:
set dir="$PWD/./test"
set dir2="$PWD/testcase.txt"
set ODD=""
set EVEN=""
set oddlines=`sed -n 1~2p $PWD/testcase.txt > ODD`
set evenlines=`awk NR%2==0 $PWD/testcase.txt > EVEN`
set odd="`cat $PWD/ODD`"
set even="`cat $PWD/EVEN`"
set FILERETURN=""
set i=1
set j=1

while ($i <= $#odd)
 while ($j <= $#even)
  
  FILERETURN=$dir "$odd[$i]"
  echo FILERETURN
  #echo $odd[$i]
  
  #echo $even[$j]
  
   @ i = $i + 1
   @ j = $j + 1
  end
end

---------- Post updated at 12:55 PM ---------- Previous update was at 03:04 AM ----------

Well, I've almost done it. I managed to pipe pipe odd lines in, send them to the program and put them into a return variable. I've compared them in terminal and I am getting what I want. All the test cases run and I get a 10/10, my only problem is the trailing command not found messages. I do not know why this is happening. It's probably something really small but I don't have enough experience to immediately see it. Any helpful hints as to why I'm getting "command not found." Is there something wrong with my if statement?

Code:
set dir="$PWD/./test"
set dir2="$PWD/testcase.txt"
set ODD=""
set EVEN=""
set oddlines=`sed -n 1~2p $PWD/testcase.txt > ODD`
set evenlines=`awk NR%2==0 $PWD/testcase.txt > EVEN`
set fodd=$PWD/ODD
set odd="`cat $PWD/ODD`"
set even="`cat $PWD/EVEN`"
set FILERETURN=""
set i=1
set j=1
set counttot=0
set countpass=0

while ($i <= $#odd)
 while ($j <= $#even)
  
  set FILERETURN=(`echo $odd[$i] | $dir`)
     
    #echo $FILERETURN
    #echo $even[$j]
    if $FILERETURN == $even[$j] 
         @ countpass = $countpass + 1
    endif
  #echo $odd[$i]
  
  #echo $even[$j]
   @ counttot = $counttot + 1
   @ i = $i + 1
   @ j = $j + 1
  end
end

echo "passed/total tests" $countpass "/" $counttot
rm ODD EVEN

The output in terminal I get when I run check.sh is the following:

Code:
% check.sh
-1: Command not found.
0: Command not found.
-1: Command not found.
-1: Command not found.
-1: Command not found.
-1: Command not found.
-1: Command not found.
0: Command not found.
passed/total tests 10 / 10

# 7  
Old 04-14-2013
You don't mention the shell that you are using (although we might infer it being bourneish). Should it be a modern one (bash, ksh), try running the script with the -x option. In bash, set sth will set positional parameters, not assign variables. If you want to assign variables, don't use spaces around the = sign. I don't know any command that is related/similar to your usage of the @ char.
And, using awkanyway, why don't you get the job done entirely in awk, as scrutinizer pointed out?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

Reading multiple lines in a file

Hello, I am new in shell scripting. I need help regarding following. I have 4 files generated by backups daily. I have stored the names of these 4 files into one file. i.e I have 4 files names as a, b, c & d and these names have been put into one file abcd.txt. Now I want to cat each file in... (7 Replies)
Discussion started by: Ali Sarwar
7 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

Regarding reading lines into a new file

Hi all, I jut use a loop to read lines from the user and redirect it to a file. echo "Enter the line" while read -r LINE do echo $LINE >> FILE if ;then break fi done input app... (1 Reply)
Discussion started by: Ananthdoss
1 Replies

6. Shell Programming and Scripting

skip lines while reading a file

Hi Experts, I am tryin to read a file and while doing so i need to skip the lines which start with a hash (#) char. I thought of using a goto command but a lot of guys on this site say its not the good way to program. Moreover I am using a ksh shell which deos not support goto command. ... (4 Replies)
Discussion started by: bankimmehta
4 Replies

7. Shell Programming and Scripting

reading alternate lines of a file

hi, i have 2 files. file1: 1 2 3 4 5 6 file2: a b c d e f g h i (5 Replies)
Discussion started by: vidyaj
5 Replies

8. UNIX for Dummies Questions & Answers

Help with reading text file

How can i have a while loop as follows while read inputline do <task> done < name_list and also store the values (delimited) on each line to temp variables so as to print them on screen as follows while read inputline do set name | cut -d "," -f1 name_list # #i know this is not... (1 Reply)
Discussion started by: bilal05
1 Replies

9. UNIX for Dummies Questions & Answers

skip reading certain lines in a file

How can I exclude reading lines in a file that contains the following: filesystem:/home/pach/liv_patches 128005120 88456640 37270758 71% /home/patches That is, all lines that contain and begins with filesystem: should not be processed/read from a file (5 Replies)
Discussion started by: paulsew
5 Replies

10. UNIX for Advanced & Expert Users

Reading lines within a Unix file.

I have a file that has a list of numbers in it. Each line has a different number. I am trying to create some sort of loop within a script that will pick the numbers up on lines 1 and 2 and then put those figures into the script. It then goes through the process then loops back and reads lines 2 and... (5 Replies)
Discussion started by: mariner
5 Replies
Login or Register to Ask a Question