0

I have two text file. The structure of those two file is same. I have a while loop which reads from those two text file at the same time. But both of those two file , first 4 line is not needed. so what i need is, at the very starting of my program the two head will jump to line number 4 of both of those files and then start to read through my while loop. Here is my current code

while read compareFile1 <&3 && read compareFile2 <&4; do
echo $compareFile1
echo $compareFile2 
done 3<test1.txt 4<test2.txt

Here is my sample file ..

=== Predictions on test data ===

    inst#     actual  predicted error prediction (id)
        1 1:positive 1:positive       0.774 (10001996.txt)
        2 1:positive 2:negative   +   0.889 (10003432.txt)
        3 1:positive 1:positive       1 (10003865.txt)
        4 1:positive 1:positive       0.999 (10004065.txt)
        5 1:positive 1:positive       0.991 (10004266.txt)
        6 1:positive 1:positive       0.999 (10006157.txt)
        7 1:positive 1:positive       0.869 (10007003.txt)
        8 1:positive 2:negative   +   1 (10008447.txt)
        9 1:positive 1:positive       0.998 (10009702.txt)
       10 1:positive 1:positive       0.994 (10011072.txt)

How can i do this by bash? I am using mac. Thanks.

0

3 Answers 3

2

You could use the tail utility. By default, it outputs the last 10 lines of a file, but it also has some quite useful parameters. To skip the first X lines, use -n+X.

Example:

 tail -n+5 myfile.txt

will output the whole file from the 5th line (skipping the first 4).

But in your case, you could simply increment a variable to start the processing on line 4. Example:

l=0 
while read compareFile1 <&3 && read compareFile2 <&4; do
  if [[ $l < 4 ]]; then 
     l=$((l+1)); 
  else
     # do your processing here
     echo $compareFile1
     echo $compareFile2 
  fi  
done 3<test1.txt 4<test2.txt
Sign up to request clarification or add additional context in comments.

2 Comments

please see my edited question. I added my code and file. " tail -n+5 myfile.txt " command is returning the part of the file ..not shifting the pointer head (as far as i understood)
Thanks man, that's a very quick and easy solution. I am wondering, why i did not think about it
0

Here is another alternative, it avoids reading both files in a shell loop:

nl File1.txt | paste - File2.txt | sed -n '5,$ p'

It produces in the first column the line number (via nl), then there is the content of first file, followed by the content of the second file. The sed command removes the first four lines.

Instead of sed you could plug in awk and access the fields from your files.

Depending on the files size this could be faster.

Comments

-1

To read in all but the first 4 lines of myfile: tail -n +4 myfile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.