LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-27-2011, 10:53 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Rep: Reputation: Disabled
CAT command | multiple lines to multiple lines


Dear all,

A very silly question here: I have two txt files (ALPHA.txt and BETA.txt) with one line each, as below:

ALPHA.txt
red red fed

BETA.txt
der fed frt

I just want to put them into a new GAMMA.txt file in which the two lines from the two files are kept separated:

Expected GAMMA.txt
red red fed
der fed frt

Unfortunately, the command Cat ALPHA.txt BETA.txt > GAMMA.txt merges them in a way that the two strings appear in the same line:

Current GAMMA.txt:
red red fed der fed frt

??

Suggestions are highly appreciated!

Best,

Udiubu
 
Old 10-27-2011, 11:02 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,179

Rep: Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780Reputation: 1780
Well my question would be were the files created on a Windows machine? If not I am unable to reproduce your results
 
Old 10-27-2011, 11:06 AM   #3
RHTopics
Member
 
Registered: May 2006
Posts: 35

Rep: Reputation: 3
You can do it this way with two commands:

cat ALPHA.txt > GAMMA.txt
cat BETA.txt >> GAMMA.txt
 
Old 10-27-2011, 11:19 AM   #4
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Thanks for the quick response guys,
@ grail: it's a linux machine
@RHTopics: done already, but got the same results as above.

How about splitting the single line within GAMMA.txt into two lines with three columns each?

Thank you very much again for your help!
 
Old 10-27-2011, 11:30 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Like grail, I also believe that the files used aren't "normal" ascii files.

What is the output of the following command:
Code:
file ALPHA.txt BETA.txt
 
Old 10-27-2011, 11:35 AM   #6
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Hi druuna,

Here we go:

ASCII text, with no line terminators
 
Old 10-27-2011, 11:47 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,
Quote:
Originally Posted by udiubu View Post
Here we go:

ASCII text, with no line terminators
Assuming this is from either the alpha or beta file: This is your problem, it is not a normal ascii file, it lacks a line terminator.

If at all possible create normal ascii input files or use dos2unix to create normal ascii files.

Hope this helps.
 
1 members found this post helpful.
Old 10-27-2011, 11:50 AM   #8
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Hey druuna,

Thanks for your answers. I got the problem.
I managed to solve the issue by using my mac.

All the best,

U.
 
Old 10-27-2011, 11:54 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
Old 10-27-2011, 04:07 PM   #10
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 101Reputation: 101
I don't think there is anything wrong with a single line ASCII file without a line-end.

Anyway, here's another solution: create a single character file 'newline' e.g. with
Code:
echo > newline
Then use
Code:
cat ALPHA.txt newline BETA.txt newline > GAMMA.txt
 
1 members found this post helpful.
Old 10-28-2011, 12:58 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
You don't even need to create a file. Just use process substitution (assuming bash).

Code:
cat ALPHA.txt <(echo) BETA.txt <(echo) > GAMMA.txt
You could also group multiple commands together.

Code:
{ cat ALPHA.txt ; echo ; cat BETA.txt ; echo ; } > GAMMA.txt
P.S. ascii is a character encoding, not a file structure. If every character the file contains exists in the ascii character set, then you could call it an ascii file (see man ascii for the full list). It doesn't matter if there's a newline at the end or not.

Don't forget though that *nix and dos treat line endings differently. *nix files use only the ascii <LineFeed> character to terminate lines, while dos files use <CarriageReturn><LineFeed>. If you run a dos file through a unix command, the extra invisible CRs at the end sometimes cause trouble.

Old-style mac files used yet a third style of line ending, which was just <CarriageReturn>. In *nix those files would be treated as containing a single long line. But OSX now uses the unix style.

Last edited by David the H.; 10-28-2011 at 01:16 AM. Reason: addendum
 
2 members found this post helpful.
Old 10-28-2011, 06:09 AM   #12
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
Echo > newline works great! Thanks SecretCode..

@ at David the H. thanks, too. Process subs optin is also working..

All the best,

Emiliano
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple lines at the command line - how?! jsp_1983 Linux - Newbie 4 02-25-2011 08:04 PM
SED multiple lines masterdam79 Linux - Newbie 6 10-29-2009 04:25 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 09:34 AM
ISDN multiple lines schimmelpilz Linux - Newbie 1 02-24-2004 05:39 PM


All times are GMT -5. The time now is 04:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration