Shell Scripting - Traversing each line in a file.

I have to manipulate a file using shell script. The file contains say 250 lines and has 6 columns separated by tab space. I need to subtract the 4th column from teh 2nd column and if the result is not zero, then I need to write to another file.

For Example, let us assume I have a file named myfile.dat which contains the following

abc   1250  xyz 1250 zzz 111

def    100   xyz  90    zzz 112

ghi   5555   xyz 100  xzz 113

jkl    100   xyz  100    zzz 114

I need to get the 3rd and 5th lines (please note that there is an empty line between each line) which are

def    100   xyz  90    zzz 112
ghi   5555   xyz 100  xzz 113

in another file say diff.dat

This is kind of urgent. Any help would be greatly appreciated. Thanks!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What OS?
perl -ane 'print if $F[1] != $F[3]' myfile.dat  > diff.dat
or in MSwindows
perl -ane "print if $F[1] != $F[3]"  myfile.dat  > diff.dat
Here is running code for vb

Private Sub processes_Click()

Dim temp As String
Dim cnt As Integer
'On Error GoTo errr
Dim myToken() As String
cnt = 0
Open "c:\bb.txt" For Input As #1 'path of input file
Open "c:\oFile.txt" For Output As #2 'path to output file
While Not EOF(1)
Line Input #1, temp
temp = Replace(temp, "       ", " ", 1)
temp = Replace(temp, "      ", " ", 1)
temp = Replace(temp, "     ", " ", 1)
temp = Replace(temp, "    ", " ", 1)
temp = Replace(temp, "   ", " ", 1)
temp = Replace(temp, "  ", " ", 1)

myToken = Split(temp, " ", -1)
res = CInt(myToken(1)) - CInt(myToken(3))
If res <> 0 Then
     Print #2, LTrim(RTrim(temp))
End If

Close #1
Close #2
MsgBox "done......."
Exit Sub
Close #1
Close #2
MsgBox Err.Source & "::" & Err.Description
End Sub
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

gdamodaranAuthor Commented:
It is in Unix Shell Scripting
HonorGodSoftware EngineerCommented:
head -3 filename | tail -1 ; head -6 filename | tail -1
HonorGodSoftware EngineerCommented:
ozo's is the right way to do it.
gdamodaranAuthor Commented:

Thanks a lot! it works. But I have one question.

Your command is this.

perl -ane 'print if $F[1] != $F[3]' myfile.dat  > diff.dat

for me, the myfile.dat is basically a o/p from a shell script say Can I do this so that I get the same result? Please do let me know. Thanks! | perl -ane 'print if $F[1] != $F[3]'  > diff.dat
gdamodaranAuthor Commented:
Thank you so much. One last question. if I want a blank space between each line what should I be doing?
perl -ane 'print "$_\n" if $F[1] != $F[3]'
perl -ane 'BEGIN{$\="\n"} print  if $F[1] != $F[3]'
or if you are know there are always blank lines between each line of the input
perl -00 -ane 'print  if $F[1] != $F[3]'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gdamodaranAuthor Commented:
Awesome! Cannot find words to Thank You! I really appreciate your help. You have a good one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.