Sponsored Content
Top Forums Shell Programming and Scripting Reading in two lines at once from a text file Post 302793839 by tastybrownies on Sunday 14th of April 2013 12:55:59 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 07:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy