|
Home > Archive > Unix Programming > September 2006 > How to join lines using Unix script
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 |
How to join lines using Unix script
|
|
| kitcha 2006-09-20, 7:34 am |
| I have a file of the format.
a
b
c
d
I want to join these lines i.e the output should be
a b c d
This is equivalent to opening the file and doing %j in the file, I
would like to know if theres an option of doing it using a Shell Script
| |
| Maurizio Loreti 2006-09-20, 7:34 am |
| "kitcha" <kitcha315@gmail.com> writes:
> I have a file of the format.
>
> a
> b
> c
> d
>
> I want to join these lines i.e the output should be
>
> a b c d
>
> This is equivalent to opening the file and doing %j in the file, I
> would like to know if theres an option of doing it using a Shell Script
usr tr to convert newlines to spaces, except for the last one.
--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
| |
| Hubble 2006-09-20, 7:34 am |
|
kitcha wrote:
> I have a file of the format.
>
> a
> b
> c
> d
>
> I want to join these lines i.e the output should be
>
> a b c d
>
> This is equivalent to opening the file and doing %j in the file, I
> would like to know if theres an option of doing it using a Shell Script
Backticks also do what you want, so
echo `cat file`
will do. (but beware that there are argument length limits). You can
also slurp
the words into a variable
V=`cat file`
for i in $V ; do ....
Hubble
Hubble.
| |
| noogie.brown@gmail.com 2006-09-20, 1:29 pm |
|
Hubble wrote:
> kitcha wrote:
>
> Backticks also do what you want, so
>
> echo `cat file`
>
> will do. (but beware that there are argument length limits). You can
> also slurp
> the words into a variable
>
> V=`cat file`
> for i in $V ; do ....
>
> Hubble
>
> Hubble.
Just a note that these are deprecated in bash i think.
your meant to use $() now
eg
V=$(cat file)
fir i in $V ; do ...
| |
| Hubble 2006-09-20, 1:29 pm |
| ....backticks
> Just a note that these are deprecated in bash i think.
>
> your meant to use $() now
>
Backticks are supported by almost all shells, not just bash, and even
by programs like perl. If you want to make your scripts incompatible,
you can declare backticks deprecated.
If you want to know how Unix commands behave and understand shell
scripts, you have to learn what they mean anyway.
Hubble.
| |
| Chris F.A. Johnson 2006-09-20, 1:29 pm |
| On 2006-09-20, kitcha wrote:
> I have a file of the format.
>
> a
> b
> c
> d
>
> I want to join these lines i.e the output should be
>
> a b c d
{ tr '\012' ' '; printf "\n"; } < FILE
Or:
awk '{ printf "%s ", $0 } END { print "" }' FILE
Or (limited to bash and ksh93):
set -f; echo $(< FILE)
Or (all Bourne-type shells):
set -f; echo $(cat FILE)
Or (use only with small files):
while read line
do
printf "%s " "$line"
done < FILE
printf "\n"
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Chris F.A. Johnson 2006-09-20, 1:29 pm |
| On 2006-09-20, Hubble wrote:
>
> kitcha wrote:
>
> Backticks also do what you want, so
>
> echo `cat file`
>
> will do. (but beware that there are argument length limits).
When using a shell builtin command (such as echo), the length is
limited only by available memory. External commands are limited by
the system.
> You can
> also slurp
> the words into a variable
>
> V=`cat file`
> for i in $V ; do ....
That's fine for a small file, but very slow for a long one.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Chris F.A. Johnson 2006-09-20, 1:29 pm |
| On 2006-09-20, Hubble wrote:
> ...backticks
>
>
>
> Backticks are supported by almost all shells, not just bash, and even
> by programs like perl. If you want to make your scripts incompatible,
> you can declare backticks deprecated.
The $() syntax is supported by all POSIX shells. Any *nix system
from the last 15 years will have a POSIX shell.
> If you want to know how Unix commands behave and understand shell
> scripts, you have to learn what they mean anyway.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Logan Shaw 2006-09-22, 1:42 am |
| kitcha wrote:
> I have a file of the format.
>
> a
> b
> c
> d
>
> I want to join these lines i.e the output should be
>
> a b c d
>
> This is equivalent to opening the file and doing %j in the file, I
> would like to know if theres an option of doing it using a Shell Script
Here are two quick and dirty ways that both have limitations on the
maximum length of line they will make (as well as limitations on the
strings they will accept):
Way #1:
xargs echo < myfile
Way #2:
fmt < myfile
Here's a way that, to my knowledge, has no such limitations:
perl -le '@x = <>; chomp for @x; print join " ", @x;' < myfile
It's not as concise as way #2, though...
- Logan
| |
| John W. Krahn 2006-09-22, 1:42 am |
| Logan Shaw wrote:
> kitcha wrote:
>
> Here are two quick and dirty ways that both have limitations on the
> maximum length of line they will make (as well as limitations on the
> strings they will accept):
>
> Way #1:
>
> xargs echo < myfile
>
> Way #2:
>
> fmt < myfile
>
> Here's a way that, to my knowledge, has no such limitations:
>
> PERL -le '@x = <>; chomp for @x; print join " ", @x;' < myfile
Or:
perl -le '@x = <>; chomp @x; print "@x"' myfile
Or even:
perl -le 'print "@{[ map /(.*)/, <> ]}"' myfile
And you can do the same thing without slurping the whole file:
perl -pe 'eof||s/\n/ /' myfile
John
--
use Perl;
program
fulfillment
|
|
|
|
|