problem with cat and echo

Hi,

I'm using Fedora Core 6 Linux with shell zsh. I have two files a.txt
and b.txt, which I want to concatenate to make a third file, "c.txt".
The only difference is in the new file "c.txt", I would like a new
first line. Sadly, this doesn't work

echo 'first_line' | xargs cat a.txt b.txt > c.txt

What can I do? Thanks, - Dave
laredotornado [ Di, 01 April 2008 20:11 ] [ ID #1932409 ]

Re: problem with cat and echo

laredotornado wrote:

> Hi,
>
> I'm using Fedora Core 6 Linux with shell zsh. I have two files a.txt
> and b.txt, which I want to concatenate to make a third file, "c.txt".
> The only difference is in the new file "c.txt", I would like a new
> first line. Sadly, this doesn't work
>
> echo 'first_line' | xargs cat a.txt b.txt > c.txt

A "new first line" is a line to replace a.txt's first line or a completely
new, extra line?

In the latter case:

echo 'first_line' | cat - a.txt b.txt > c.txt

In the former case:

{ echo 'first_line'; tail -n +2 a.txt; } | cat - b.txt > c.txt

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
PK [ Di, 01 April 2008 20:37 ] [ ID #1932411 ]

Re: problem with cat and echo

On 4/1/2008 1:11 PM, laredotornado wrote:
> Hi,
>
> I'm using Fedora Core 6 Linux with shell zsh. I have two files a.txt
> and b.txt, which I want to concatenate to make a third file, "c.txt".
> The only difference is in the new file "c.txt", I would like a new
> first line. Sadly, this doesn't work
>
> echo 'first_line' | xargs cat a.txt b.txt > c.txt
>
> What can I do? Thanks, - Dave

{ echo 'first_line'; cat a.txt b.txt } > c.txt
Ed Morton [ Di, 01 April 2008 20:28 ] [ ID #1932413 ]

Re: problem with cat and echo

Ed Morton wrote:

> { echo 'first_line'; cat a.txt b.txt } > c.txt

I think you need a ";" after "b.txt" }, or use round parenthesis instead.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
PK [ Di, 01 April 2008 20:44 ] [ ID #1932414 ]

Re: problem with cat and echo

On 4/1/2008 1:44 PM, pk wrote:
> Ed Morton wrote:
>
>
>>{ echo 'first_line'; cat a.txt b.txt } > c.txt
>
>
> I think you need a ";" after "b.txt" }, or use round parenthesis instead.
>

Yes. Thanks.

Ed.
Ed Morton [ Di, 01 April 2008 20:32 ] [ ID #1932415 ]

Re: problem with cat and echo

On 2008-04-01, laredotornado <laredotornado [at] zipmail.com> wrote:
> Hi,
>
> I'm using Fedora Core 6 Linux with shell zsh. I have two files a.txt
> and b.txt, which I want to concatenate to make a third file, "c.txt".
> The only difference is in the new file "c.txt", I would like a new
> first line. Sadly, this doesn't work
>
> echo 'first_line' | xargs cat a.txt b.txt > c.txt

You don't seem to understand what xargs does. It builds
command lines using its arguments and appending standard
input at the end (by default). This tries to execute the
command line "cat a.txt b.txt first_line", with the output
redirected to c.txt. This is the wrong approach, as cat
doesn't process text in its command line, only file names.
For this to make sense, you would have to have a file
named "first_line" that you wanted to concatenate.
Fortunately, you can tell cat to process standard input as
one of the files to be processed:

echo "first_line" | cat - a.txt b.txt

>
> What can I do? Thanks, - Dave

Stop using xargs and use "-" as an argument to cat to
tell it to use standard input in the concatenation.


--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
Chris Mattern [ Di, 01 April 2008 23:27 ] [ ID #1932424 ]

Re: problem with cat and echo

On 2008-04-01, Ed Morton <morton [at] lsupcaemnt.com> wrote:
>
>
> On 4/1/2008 1:44 PM, pk wrote:
>> Ed Morton wrote:
>>
>>
>>>{ echo 'first_line'; cat a.txt b.txt } > c.txt
>>
>>
>> I think you need a ";" after "b.txt" }, or use round parenthesis instead.
>>
>
> Yes. Thanks.
>
I still think that 'echo "first_line" | cat - a.txt b.txt > c.txt' is
more straightforward and more easily read.

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
Chris Mattern [ Di, 01 April 2008 23:29 ] [ ID #1932425 ]
Linux » comp.unix.shell » problem with cat and echo

Vorheriges Thema: Re: Get the parameter pattern from command line
Nächstes Thema: need help with grep in eval