Concatenating two files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Concatenating two files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-15-2008
Sriramprabu Sriramprabu is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 15 September 2008, 10:47 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb Concatenating two files

HI
I need to concatenate two files which are having headers. the result file should contain only the header from first file only and the header in second file have to be skipped.

file1:
name age
sriram 23

file2
name age
prabu 25

result file should be
name age
sriram 23
prabu 25

how to do this in script?

Thank in advance
Sriram
Sponsored Links
    #2  
Old 09-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 22 September 2014, 3:20 PM EDT
Location: Out running a Marathon.
Posts: 2,329
Thanks: 72
Thanked 153 Times in 142 Posts
Hammer & Screwdriver One approach to solve

> cat file1
name age
sriram 23
> cat file2
name age
prabu 25
> cat file1 >file3 ; tail +2 <file2 >>file3
> cat file3
name age
sriram 23
prabu 25
Sponsored Links
    #3  
Old 09-15-2008
Sriramprabu Sriramprabu is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 15 September 2008, 10:47 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
how to do this if i have n number of files
    #4  
Old 09-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 22 September 2014, 3:20 PM EDT
Location: Out running a Marathon.
Posts: 2,329
Thanks: 72
Thanked 153 Times in 142 Posts
Hammer & Screwdriver One approach; and there are many

The following example assumes that the input files are all in the format of "file*"



Code:
#! /usr/bin/bash

ls file* >my_list
rm big_file 2>/dev/null
first_f=1

while read zf
   do
   if [ $first_f -gt 0 ]
      then
         cat "$zf" >>big_file
         first_f=0
      else
         tail +2 "$zf" >>big_file
   fi
done <my_list

exit 0

Sponsored Links
    #5  
Old 09-15-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 10 January 2014, 5:37 AM EST
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts

Code:
 sed q file1 > file.new;ls file[12] | xargs -i tail +2 {} >> file.new

Sponsored Links
    #6  
Old 09-15-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts

Code:
format head=
name 	age
---------------
.
format content=
@<<<<<<<@>>>>>>
$name $age
.
open(FH,">out.txt") or die "Can not open file!";
select FH;
$^=head;
$~=content;
open(FH1,"<first.txt");
while(<FH1>){
	if($.>1)
	{
		($name,$age)=split(" ",$_);
		write;
	}
	
}
close(FH1);
open(FH1,"<second.txt");
while(<FH1>){
	if($.>1)
	{
		($name,$age)=split(" ",$_);
		write;
	}
}
close(FH1);

Sponsored Links
    #7  
Old 09-15-2008
RahulJoshi's Avatar
RahulJoshi RahulJoshi is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 13 September 2013, 10:47 AM EDT
Location: PUNE
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
you can use
cat file2>>file1
it append file2 after file1.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Concatenating Text Files 222001459 UNIX for Dummies Questions & Answers 5 05-05-2009 08:52 AM
negatively concatenating files raidzero Shell Programming and Scripting 2 07-10-2008 03:52 PM
Concatenating records from 2 files Powcmptr UNIX for Dummies Questions & Answers 4 04-02-2008 03:57 PM
concatenating x files into a one... Nicol UNIX for Dummies Questions & Answers 1 09-30-2004 08:13 AM



All times are GMT -4. The time now is 12:19 AM.