Reputation: 1335
I have different files like below format
Scenario 1 :
File1
no,name
1,aaa
20,bbb
File2
no,name,address
5,aaa,ghi
7,ccc,mn
I would like to get column list which is having more number of columns and if it is in the same order
**Expected output for scenario 1 :**
no,name,address
Scenario 2 :
File1
no,name
1,aaa
20,bbb
File2
no,age,name,address
5,2,aaa,ghi
7,3,ccc,mn
Expected Results :
Both file headers and positions are different as a message
I am interested in any short solution using bash / perl / sed / awk.
Upvotes: 0
Views: 90
Reputation: 242133
Perl solution:
perl -lne 'push @lines, $_;
close ARGV;
next if @lines < 2;
@lines = sort { length $a <=> length $b } @lines;
if (0 == index "$lines[1],", $lines[0]) {
print $lines[1];
} else {
print "Both file headers and positions are different";
}' -- File1 File2
-n
reads the input line by line and runs the code for each line-l
removes newlines from input and adds them to printed linesno,names
is correctly rejected)Upvotes: 1