Home > Archive > AWK > April 2007 > is ORS the best way to transpose?
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 |
is ORS the best way to transpose?
|
|
|
| Hi,
With simple files of the form:
line1
line2
line3
Is modifying ORS the recommended way of transposing the lines?, i.e.:
line1 line2 line3
naively with:
awk 'BEGIN {ORS=" "}; {print $1}
Cheers,
--
Seb
| |
| Janis Papanagnou 2007-04-26, 6:56 pm |
| Seb wrote:
> Hi,
>
> With simple files of the form:
>
>
> line1
> line2
> line3
>
>
> Is modifying ORS the recommended way of transposing the lines?, i.e.:
>
> line1 line2 line3
>
>
> naively with:
>
> awk 'BEGIN {ORS=" "}; {print $1}
Or even...
awk 'BEGIN{ORS=" "}1'
Yes, that's a possible way if you don't mind the missing \n at the end
of the line.
Janis
>
>
> Cheers,
>
| |
|
| On Thu, 26 Apr 2007 22:01:59 +0200,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
[...]
> Or even...
> awk 'BEGIN{ORS=" "}1'
> Yes, that's a possible way if you don't mind the missing \n at the end
> of the line.
Great, thanks.
--
Seb
| |
| Kenny McCormack 2007-04-26, 6:56 pm |
| In article <873b2mncr2.fsf@patagonia.sebmags.homelinux.org>,
Seb <spluque@gmail.com> wrote:
>Hi,
>
>With simple files of the form:
>
>
>line1
>line2
>line3
>
>
>Is modifying ORS the recommended way of transposing the lines?, i.e.:
>
>line1 line2 line3
>
>
>naively with:
>
>awk 'BEGIN {ORS=" "}; {print $1}
The people do:
ORS=NR%3?" ":"\n"
and let it go at that.
| |
| Janis Papanagnou 2007-04-26, 6:56 pm |
| Kenny McCormack wrote:
> In article <873b2mncr2.fsf@patagonia.sebmags.homelinux.org>,
> Seb <spluque@gmail.com> wrote:
>
>
>
> The people do:
>
> ORS=NR%3?" ":"\n"
>
> and let it go at that.
>
That's really neat (as long as we know the number of lines in advance).
Janis
| |
| Kenny McCormack 2007-04-26, 6:56 pm |
| In article <f0r5i8$t17$1@online.de>,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>Kenny McCormack wrote:
>
>That's really neat (as long as we know the number of lines in advance).
>
>Janis
I'm kind of assuming that the goal really is to reformat an arbitrary
file into lines of "n" original lines from the original file (where n is
known ahead of time).
| |
|
| On Thu, 26 Apr 2007 21:21:49 +0000 (UTC),
gazelle@xmission.xmission.com (Kenny McCormack) wrote:
[...]
> The people do:
> ORS=NR%3?" ":"\n"
> and let it go at that.
This works great, but I don't feel very without understanding what's
happening :-) Would you mind a quick walk through?
Cheers,
--
Seb
| |
| Janis Papanagnou 2007-04-26, 9:56 pm |
| Seb wrote:
> On Thu, 26 Apr 2007 21:21:49 +0000 (UTC),
> gazelle@xmission.xmission.com (Kenny McCormack) wrote:
>
>
>
>
> This works great, but I don't feel very without understanding what's
> happening :-) Would you mind a quick walk through?
You assign to the ORS (output record separator) either a space or a newline,
depending on the expression NR%3; for the values 1 and 2 it becomes "true"
(thus printing a blank) for value 3 it becomes "false" (printing the newline).
The ORS assignment will result in a true condition so that the default action
will take place, which is to print the whole line terminated by the currently
valid ORS (which may change from line to line). Alltogether the program will
take three consecutive lines and put them on a single line, continued until
the end of the file.
Janis
| |
| mik3l3374@gmail.com 2007-04-28, 9:57 pm |
| On Apr 27, 3:42 am, Seb <splu...@gmail.com> wrote:
> Hi,
>
> With simple files of the form:
>
> line1
> line2
> line3
>
> Is modifying ORS the recommended way of transposing the lines?, i.e.:
>
> line1 line2 line3
>
> naively with:
>
> awk 'BEGIN {ORS=" "}; {print $1}
>
> Cheers,
>
> --
> Seb
awk '{printf "%s ", $0} END{print}' file1
|
|
|
|
|