|
Home > Archive > Unix questions > October 2007 > Sed line 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 |
Sed line transpose
|
|
| vjp2.at@at.BioStrategist.dot.dot.com 2007-10-10, 7:35 am |
| 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
<xpoe.sed>
x
n
<xpoe.txt>
m1
f1
m2
f2
m3
f3
m4
f4
<xpoe.out>
f1
m1
f2
m2
f3
m3
f4
{m4 doesn't print}
| |
| Chris F.A. Johnson 2007-10-10, 7:35 am |
| 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
>
><xpoe.sed>
> x
> n
><xpoe.txt>
> m1
> f1
> m2
> f2
> m3
> f3
> m4
> f4
><xpoe.out>
> 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 | <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
| |
| Michael Tosch 2007-10-10, 7:35 am |
| 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
>
> <xpoe.sed>
> x
> n
> <xpoe.txt>
> m1
> f1
> m2
> f2
> m3
> f3
> m4
> f4
> <xpoe.out>
> 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
| |
| Ed Morton 2007-10-10, 1:27 pm |
| 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
>
> <xpoe.sed>
> x
> n
> <xpoe.txt>
> m1
> f1
> m2
> f2
> m3
> f3
> m4
> f4
> <xpoe.out>
> 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.
| |
| Michael Tosch 2007-10-10, 7:23 pm |
| Michael Tosch wrote:
> vjp2.at@at.BioStrategist.dot.dot.com wrote:
>
> 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
|
|
|
|
|