Hi Friends,
I have 2 files A and B . I want to compare the 3rd line of file A and B .
(I dont want to compare the 2 files, using diff or cmp). I just want to know whether 3rd line of A matches the 3 rd line of B. Can anybody share their knowledge on the same?
3 - line number
p - print command
q - quit command
3{p;q;} If the current line is third line in the file, then print that line and quit sed command. For example if your file has 50,000 lines and if you dont provide quit command, sed will process all the lines in the file. If you use quit command, it will save time.
Thanx anbu for your quick reply and a nice explanation.
i also want to know that how can i compare field by fileld of two files, like File A and File B?
as you have compared the lines i want to compare the each and every fields of each line of both the file...
like this.
i just want to compare 1st field of FILE A with 1st field of FILE B...
like that
if the all fields are matches it wiil print a success msg else it will throw an error...
awk -F"|" ' NR == FNR { arr[$1]=1; next };
!arr[substr($3, length($3)-2)] { print "Error" ;exit }
END { print "Success" }
' filea fileb
NR == FNR { arr[$1]=1; next } Keep first field as index and assign one to it. This block is executed till the end of first file.
!arr[substr($3, length($3)-2)] { print "Error" ;exit } Get the last three digit of third field and check whether it is an index in array arr. If not exit the processing.