LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2010, 12:09 AM   #1
rajesekhar
LQ Newbie
 
Registered: Sep 2010
Posts: 4

Rep: Reputation: 0
concatenate two files with a new line character between them


Hello,

I am trying to concatenate two files with a carriage return.

Iam not sure how we can implement this using .sh script.

For ex:

Data for 1.txt -->
A
B

Data for 2.txt -->
C
D

Data for 3.txt should be-->
A
B
C
D

I tried concatenating using
cat 1.txt 2.txt > 3.txt

I got an output like
A
BC
D

Please help me out!!

Thanks, Raj
 
Old 09-02-2010, 12:18 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

The problem is not with the process but with the data; in
your example the file 1.txt has no new-line after the B.


[edit]
As a work-around you could do the following


echo -e "\n" >> 1.txt
[/edit]


Cheers,
Tink

Last edited by Tinkster; 09-02-2010 at 12:21 AM.
 
Old 09-02-2010, 12:24 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
No need to even be that fancy. Echo creates a newline by default, so just use echo >> 1.txt.
 
Old 09-02-2010, 12:25 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
In separate steps
Code:
cat 1.txt >3.txt
echo >>3.txt
cat 2.txt >>3.txt
The 'echo' sends a newline char ....
 
1 members found this post helpful.
Old 09-02-2010, 04:44 PM   #5
rajesekhar
LQ Newbie
 
Registered: Sep 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Chris.

It worked.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script: concatenate variables with period character the182guy Linux - Software 4 12-28-2009 02:41 PM
how to find some files and concatenate them? yumener Linux - Newbie 7 03-10-2009 04:41 PM
concatenate mpeg (vob) files, command line tool. kaz2100 Linux - Software 2 02-09-2008 06:42 PM
Script to concatenate several files docaia Linux - General 10 02-03-2008 02:59 PM
Concatenate PDF files? mykrob Linux - Software 5 11-07-2006 06:25 AM


All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration