The t command in ed transfers/copies a line from the addressed line (here $ for the last line) to after the line addressed by its argument (here 0, i.e. insert before the first line). The ,p command prints the contents of the editing buffer (change this to w to write the result back into the file).
H appends the current line to the hold space. Unfortunally this will start the hold space with a newline, so 1h for the first line, the hold space gets overwritte with that line without preceeding newline
$!d means if this is not (!) the last line ($), delete the line, because we don't want to print it yet
G is only executed for the last line, because for other lines the d stopped processing. Now all lines (first to last) we collected in the hold space get appended to the current (last) line with the G command and everything gets printed at the end of the script.
With
ed
:The
t
command ined
transfers/copies a line from the addressed line (here$
for the last line) to after the line addressed by its argument (here0
, i.e. insert before the first line). The,p
command prints the contents of the editing buffer (change this tow
to write the result back into the file).或者只是像这样,没有 sed 或其他
纯
sed
:所有行都收集在保持空间中,然后附加到最后一行:
H
appends the current line to the hold space. Unfortunally this will start the hold space with a newline, so1h
for the first line, the hold space gets overwritte with that line without preceeding newline$!d
means if this is not (!
) the last line ($
),d
elete the line, because we don't want to print it yetG
is only executed for the last line, because for other lines thed
stopped processing. Now all lines (first to last) we collected in the hold space get appended to the current (last) line with theG
command and everything gets printed at the end of the script.另一种
sed
选择是:删除除最后一个,下一个读取和输出整个文件之外的所有行。
GNU awk:
Portable awk:
Reverse the file, remember the first line and print it after the last line, then re-reverse the file:
Tried with below sed command and it worked fine too
command
output
尝试这个,
tail -n1
将打印文件的最后一行xargs -I
将尾部输出传递给 sedsed '1i
将在第一行插入上下文如果输出看起来是文件,则
-i
向 sed 添加选项以进行内联编辑