Unix Shell - 'paste' and 'xargs'?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > 'paste' and 'xargs'?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author 'paste' and 'xargs'?
Seb

2007-09-28, 1:24 pm

Hi,

I have a hard time understanding how 'xargs' works, but it seems to be the
tool necessary for having 'paste' take two args from stdin. The idea is
as:


echo -e "1 2\n3 4\n5 6" > paste_try
paste $(head -n 1 paste_try) $(tail -n 1 paste_try)


which of course fails, but hope you can see what I mean. Would 'xargs'
work here? I'd appreciate any pointers. Thanks.


--
Seb
Cyrus Kriticos

2007-09-28, 1:24 pm

Seb wrote:
>
> I have a hard time understanding how 'xargs' works, but it seems to be the
> tool necessary for having 'paste' take two args from stdin. The idea is
> as:
>
> echo -e "1 2\n3 4\n5 6" > paste_try
> paste $(head -n 1 paste_try) $(tail -n 1 paste_try)
>
> which of course fails, but hope you can see what I mean. Would 'xargs'
> work here? I'd appreciate any pointers. Thanks.


[bash]

$ paste <(head -n 1 paste_try) <(tail -n 1 paste_try)
1 2 5 6

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Cyrus Kriticos

2007-09-28, 7:21 pm

Cyrus Kriticos wrote:

> [bash]
>
> $ paste <(head -n 1 paste_try) <(tail -n 1 paste_try)
> 1 2 5 6


or perhaps something like this:

$ tac paste_try | paste paste_try -
1 2 5 6
3 4 3 4
5 6 1 2

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Bill Marcum

2007-09-28, 7:21 pm

On 2007-09-28, Seb <spluque@gmail.com> wrote:
> Hi,
>
> I have a hard time understanding how 'xargs' works, but it seems to be the
> tool necessary for having 'paste' take two args from stdin. The idea is
> as:
>
>
> echo -e "1 2\n3 4\n5 6" > paste_try
> paste $(head -n 1 paste_try) $(tail -n 1 paste_try)
>
>
> which of course fails, but hope you can see what I mean. Would 'xargs'
> work here? I'd appreciate any pointers. Thanks.
>
>

That fails because the results of "head -n 1 paste_try" and
"tail -n 1 paste_try" aren't filenames. Well, they could be, but I don't
think that's what you want.
What might work is
awk 'NR==1{x=$0} END{print x,$0}'
Seb

2007-09-28, 7:21 pm

On Fri, 28 Sep 2007 14:22:19 -0400,
Bill Marcum <marcumbill@bellsouth.net> wrote:

[...]

> That fails because the results of "head -n 1 paste_try" and "tail -n 1
> paste_try" aren't filenames. Well, they could be, but I don't think
> that's what you want. What might work is
> awk 'NR==1{x=$0} END{print x,$0}'


Nice, I should have thought of awk for this!

Thank you both (I didn't know about <( in bash either)!


--
Seb
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com