Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge the files in unix 2

Status
Not open for further replies.

NIT2003

Programmer
Nov 22, 2007
2
CA
Hi,

I am new to unix..so need the tip to do the below ..

say 3 files with below format

File A
header 111
detail 1
detail 2
detail 3

File C
header 111
detail 4
detail 5
detail 6


File A
header 111
detail 7
detail 8
detail 9


output as

File B

header 111
detail 1
detail 2
detail 3
detail 4
detail 5
detail 6
detail 7
detail 8
detail 9


Thanks..

Vics
 
So you want the header off any file - lets say the first one - and all but the header of all the files so
Code:
#!/bin/ksh
FIRST=1
for file in FileA FileB FileC
do
  # if first file output fist line
  [[ $FIRST -eq 1 ]] { head -1 $file; FIRST=0; }
  # use \awk to print all but the first line
  awk 'NR != 1 {print}' $file
done

Ceci n'est pas une signature
Columb Healy
 
You've got 2 fileAs. Assume you have 3 files fileA, fileB, fileC and you wish to merge them into fileM.

First join all the files together.

cat fileA fileB fileC

Then sort them

cat fileA fileB fileC | sort

Then just get 1 header

cat fileA fileB fileC | sort | uniq

And stick the result in fileM

cat fileA fileB fileC | sort | uniq > fileM
 
Perhaps simply this ?
sort -u fileA fileB fileC > fileM

Or this ?
awk 'NR==1 || FNR!=1' fileA fileB fileC > fileM

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top