I have multiple txt files with a directory. The text files all contain the same header. I am reading in all the txt files and outputting it all to one file.
Since each individual file contains the same header, It is inserting all of them into the new merged file. How can I remove all the headers in the new merged file and leave one just at the top?
I have been looking at the sort command in unix.
sort filename | uniq
This command works, but removes all other data that is duplicate. Is there anyway to remove just the specific string "This is a header" but leave one at the top?
Current Code
$header = array( "XX-XXXXXXXXX-XXXXXXX-X XXXXXXXXXXXX" );
$files = glob( "/path/to/folder/*.txt" );
$output_file = "newfile_".date( "YmdHis" ).".txt";
$out = fopen( $output_file, "w" );
foreach( $header as $inputHeader ) {
fwrite( $out, $inputHeader );
}
foreach( $files as $file ) {
$in = fopen( $file, "r" );
while ( $line = fgets( $in ) ) {
if( $header !== $line ) {
fwrite( $out, $line );
}
}
fclose( $in );
}
fclose( $out );