Help to check header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to check header
# 1  
Old 01-09-2013
Help to check header

Hi,

I have two files in different folders, one is file1.tsv, another one is file2.tsv,

how to compare the file2's header to the file1 header.

File1.tsv
Code:
A    B    C    D

File2.tsv

Code:
A    B    C    D
1    a    g    z
2    s    h    x
3    d    j    c
4    f    k    v



output:
File3.tsv

Code:
Headers : OK

If Error

Code:
Headers : Not Matching




# 2  
Old 01-09-2013
Using diff and head

Code:
if diff <(head -1 file1.tsv) <(head -1 file2.tsv) > /dev/null; then
  echo Headers OK
else
  echo Headers not OK
fi

Or awk

Code:
awk 'A[NR]=$0 { nextfile }
END {
if( A[1] == A[2] )
  print "Matching."
else
  print "Not matching."
}' file1.tsv file2.tsv

This User Gave Thanks to Scott For This Post:
# 3  
Old 01-09-2013
It looks like your headers are differently spaced and i suppose this will not count as a difference, right?

If so: first get the headers (line 1 always?) and change all the spacings to a single blank, then compare them ("<b>" and "<t>" mean blank/tab characters):

Code:
header1="$(sed -n '1 s/[<b><t>][<b><t>]*/<b>/gp' file1)"
header2="$(sed -n '1 s/[<b><t>][<b><t>]*/<b>/gp' file2)"

if [ "$header1" != "$header2" ] ; then
     print - "Headers not matching"
else
     print - "Headers OK"
fi

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Header and trailer Check in UNIX

I am planning to automate multiple files through Unix script. Could you please guide for making the automation for multiple files ? Eg. I have to check header and footer for all files in the particular path and the header pattern should match with the filename. Filename- Filename.txt... (2 Replies)
Discussion started by: karthick2020333
2 Replies

2. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

3. Shell Programming and Scripting

To check the file and remove header before copying

Hi Guys, I have below directory where there are certain files. Something like below country_dir aus_01.txt nz_01.txt aus_02.txt bd.txt property.txt aus nz bd I need to remove the header of the file which ends with _01.txt while copying from country directory to another... (15 Replies)
Discussion started by: rohit_shinez
15 Replies

4. UNIX for Dummies Questions & Answers

Check for empty line at end of a dynamic header in each file

Hi Folks, I have a requirement to develop a shell script. PFB my requirement, Requirement: I need to check an empty line after the end of each header in respective file and if a empty line is present simply echo file OK and if empty line is not present echo "Adding empty line" and add an... (6 Replies)
Discussion started by: tpk
6 Replies

5. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

6. UNIX for Dummies Questions & Answers

Help with the Header and Footer check

Hi, I need to check whether the incoming file has a header and footer using a UNIX script. The pattern of the header and footer is fixed as follows: Header: Name,Date Footer: Count, Total Name,Date ------------------------- ------------------------- ------------------------- Count,... (5 Replies)
Discussion started by: Sunny_teotia
5 Replies

7. UNIX for Dummies Questions & Answers

Check header of file, without opening the file

if we happen to have a very big .csv file we cannot or dont care to edit, is there a way to check if the file contains a header row? (2 Replies)
Discussion started by: lydiaflamp
2 Replies

8. Shell Programming and Scripting

Renaming all header to specific header pattern

Input #HAC0253 EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #BASFS12 EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK #ACG5115 TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK #ECG5114 IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK . . Output (5 Replies)
Discussion started by: patrick87
5 Replies

9. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question