Sed line transpose - Unix

This is a discussion on Sed line transpose - Unix ; I want to transpose every two lines, replacing the odd witht he even and vice versa. But the last line doesn't get printed. Why? sed -f xpoe.sed xpoe.txt > xpoe.out x n m1 f1 m2 f2 m3 f3 m4 f4 ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Sed line transpose

  1. Sed line transpose

    I want to transpose every two lines, replacing the odd witht he even
    and vice versa. But the last line doesn't get printed. Why?

    sed -f xpoe.sed xpoe.txt > xpoe.out


    x
    n

    m1
    f1
    m2
    f2
    m3
    f3
    m4
    f4

    f1
    m1
    f2
    m2
    f3
    m3
    f4
    {m4 doesn't print}

  2. Re: Sed line transpose

    On 2007-10-10, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    >
    > I want to transpose every two lines, replacing the odd witht he even
    > and vice versa. But the last line doesn't get printed. Why?
    >
    > sed -f xpoe.sed xpoe.txt > xpoe.out
    >
    >
    > x
    > n
    >
    > m1
    > f1
    > m2
    > f2
    > m3
    > f3
    > m4
    > f4
    >
    > f1
    > m1
    > f2
    > m2
    > f3
    > m3
    > f4
    > {m4 doesn't print}


    I'd use awk, not sed:

    awk '
    NR % 2 == 0 { print; print last }
    { last = $0 }
    END { if ( NR % 2 == 1 ) print }
    ' "$FILE"

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  3. Re: Sed line transpose

    vjp2.at@at.BioStrategist.dot.dot.com wrote:
    > I want to transpose every two lines, replacing the odd witht he even
    > and vice versa. But the last line doesn't get printed. Why?
    >
    > sed -f xpoe.sed xpoe.txt > xpoe.out
    >
    >
    > x
    > n
    >
    > m1
    > f1
    > m2
    > f2
    > m3
    > f3
    > m4
    > f4
    >
    > f1
    > m1
    > f2
    > m2
    > f3
    > m3
    > f4
    > {m4 doesn't print}


    The 1st line is empty, because the buffer is empty.
    The last line is missing, because it is only moved to the buffer.
    To add the last line, append
    $p
    $x
    The $ addresses the last line - we need an explicit print then
    exchange with the buffer, then sed does the implicit print.


    --
    Michael Tosch @ hp : com

  4. Re: Sed line transpose

    vjp2.at@at.BioStrategist.dot.dot.com wrote:

    > I want to transpose every two lines, replacing the odd witht he even
    > and vice versa. But the last line doesn't get printed. Why?
    >
    > sed -f xpoe.sed xpoe.txt > xpoe.out
    >
    >
    > x
    > n
    >
    > m1
    > f1
    > m2
    > f2
    > m3
    > f3
    > m4
    > f4
    >
    > f1
    > m1
    > f2
    > m2
    > f3
    > m3
    > f4
    > {m4 doesn't print}


    awk 'NR%2{s=$0;next}{print $0"\n"s}END{if (NR%2) print}' file

    Ed.

  5. Re: Sed line transpose

    Michael Tosch wrote:
    > vjp2.at@at.BioStrategist.dot.dot.com wrote:
    >> I want to transpose every two lines, replacing the odd witht he even
    >> and vice versa. But the last line doesn't get printed. Why?
    >>
    >> sed -f xpoe.sed xpoe.txt > xpoe.out
    >>
    >>
    >> x
    >> n
    >>
    >> m1
    >> f1
    >> m2
    >> f2
    >> m3
    >> f3
    >> m4
    >> f4
    >>
    >> f1
    >> m1
    >> f2
    >> m2
    >> f3
    >> m3
    >> f4
    >> {m4 doesn't print}

    >
    > The 1st line is empty, because the buffer is empty.
    > The last line is missing, because it is only moved to the buffer.
    > To add the last line, append
    > $p
    > $x
    > The $ addresses the last line - we need an explicit print then
    > exchange with the buffer, then sed does the implicit print.
    >
    >


    I think that sed -n works better in this case,
    because the n operation does not print.

    sed -n 'h;n;G;p' xpoe.txt

    not only prints the last line without an extra effort,
    but also does not produce an empty 1st line.

    --
    Michael Tosch @ hp : com

+ Reply to Thread