交换两个文件的内容


13

这是一个代码问题。您需要获取两个单独文件(i.txt和o.txt)的内容并交换它们。您可以创建第三个文件来帮助您完成该过程,但是在交换文件内容之后,让程序将其删除。现在再次允许重命名文件。

程序必须适用于任何文件内容。

最短的代码胜出,所有合格的提交将被投票。


2
重命名文件。无需“取走内容”并交换它们即可获得结果。
达伦·斯通

@Darren Stone我认为这应该是非法的。感谢您在提交答案之前发现问题。

似乎是一个不好的约束,重命名是执行此操作的最佳方法
hdante

1
好了,我可以删除此约束。

6
@hdante但“重命名文件”未交换其内容。在一个系统,采用inode这儿的目标将是i.txtinode包含从数据o.txtinode,反之亦然,所以,如果有硬链接到那些inodeš其他地方,它们的内容将出现换为好。重命名无法实现这一点。
AJMansfield

Answers:


9

zsh,20岁

<i*>t;<o*>i*;mv t o*

1
不过,我更喜欢没有临时文件的文件:i=`<i.txt`;<o*>i*;<<<$i>o*。太短了,必须缩短。
Ry-

1
规则已更改,允许重命名文件。您可以根据需要更新代码。

替换为<t>o*;rm t,您可以节省3个字符mv t o*
F.豪里2014年

@ F.Hauri:啊,谢谢!(有规则禁止在某一时刻重命名。)
Ry

该代码不正确。它仅在特殊情况下有效,即i *和o *分别仅匹配i.txt和o.txt,即,没有其他文件名以“ i”或“ o”开头的文件。
Erwan Legrand

3

Python,77

import os;t='.txt';r,i,o,x=[os.rename,'i'+t,'o'+t,'1'+t];r(i,x);r(o,i);r(x,o)

Python,65岁

import os;t='.txt'
for a,b in zip('iox','xio'):os.rename(a+t,b+t)

Python,63岁

import os;t='.txt'
for a,b in 'ix','oi','xo':os.rename(a+t,b+t)

PHP,68

<?$r=rename;$t='.txt';$r("i$t","x");$r("o$t","i$t");$r("x","o$t");?>

Windows批处理文件,42

move i.txt x&move o.txt i.txt&move x o.txt

Windows批处理文件(参数),30

move %1 x&move %2 %1&move x %2

我不知道您可以用来$r=rename为PHP函数创建别名。谢谢!
AL 2014年

2

两个基于的答案;52和62个字符

壳:差异+补丁(+三通+ sed ...)52

也许不是更短,但是我发现这很有趣(并且没有使用临时文件):

diff -u [io]*|tee >(patch -r -)|sed 1s/i/o/|patch -R

交换内容和修改文件的位置

样品运行

swapContent() { diff -u $1 $2|tee >(patch -r -)|sed 1s/$1/$2/|patch -R ;}

while read page file ;do man -Pcol\ -b $page >$file.txt;done <<<$'man i\nbash o'
printf "%s %8d  %s\n" $(join -j 2 <(stat -c '%s %n' [io]*) <(md5sum [io]*))
swapContent [io]*
printf "%s %8d  %s\n" $(join -j 2 <(stat -c '%s %n' [io]*) <(md5sum [io]*))

可能会产生类似:

i.txt    46007  1da1f7533e0eab1e97cce97bb7ca1d3b
o.txt   321071  7dcd230890faf4ef848d4745eda14407
patching file o.txt
i.txt   321071  7dcd230890faf4ef848d4745eda14407
o.txt    46007  1da1f7533e0eab1e97cce97bb7ca1d3b

使用xargs 简化 mv请求

没那么有趣,但还是不错。

set -- {i,o}.txt t&&eval 'xargs -n2 mv<<<"' \${1,3,2,1,3,2} \"

注意:由于第一个修补程序输出未重定向,因此在第二个修补程序命令上它们将变为垃圾。这是因为只有一个patching file o.txt出现。
F.豪里2014年

2

PHP,89

我以为我会试一试。

<?php $f1='f1.txt';$f2='f2.txt';$ft='ft.txt';copy($f1,$ft);copy($f2,$f1);rename($ft,$f2);

非高尔夫版本:

<?php
$f1 = 'f1.txt';
$f2 = 'f2.txt';
$ft = 'ft.txt';

copy($f1, $ft);
copy($f2, $f1);
rename($ft, $f2);

显然我在这里得到了2个答案,并将它们组合在一起..哦,很好。


2

Ruby,72个字节

哇!Ruby代码高尔夫!我认为以前从未尝试过!

但是,在所有现实中,这都需要一些不错的Ruby快捷方式和一些我发现的模式。这是我有史以来的第一次高尔夫,做起来很有趣。事不宜迟,以下是高尔夫球代码:

3.times{|x|x*=2;t=".txt";a=([?i,?a,?o]*2);File.rename(a[x]+t,a[x+1]+t)}

和非高尔夫版本

3.times do |x|
    x = x * 2
    t=".txt"
    a=([?i,?a,?o]*2)
    File.rename(a[x]+t, a[x+1]+t)}
end

关键因素在于,传递给的参数File.rename正是这样的:

File.rename "i.txt", "a.txt"
File.rename "o.txt", "i.txt"
File.rename "a.txt", "o.txt"

希望这(没有)有道理!


2

Powershell,44个 49字节

$t='.txt'
ren i$t a$t -fo
ren o$t i$t
ren a$t o$t

其中renRename-Item的别名。该脚本使用并删除第三个文件a.txt


1
某些编码可能会失败。因为gc默认值为UTF8NoBOM,但sc默认值为ASCII。尝试一些UTF-8文件或二进制文件,您将看到它如何失败。
AdmBorkBork

确实。谢谢。它需要-e by -n参数来确保文件不变。-readCount 0向性能添加参数会很好。的脚本rename较短:)已修复。
mazzy

1

红宝石

i1="i.txt"
i2="o.txt"
c1=IO.readlines(i2)
c2=IO.readlines(i1)
File.open(i1){|x|x.puts(c1)}
File.open(i2){|x|x.puts(c2)}

缩短版本:

a=["i.txt","o.txt"]
(0..1).each{|x|b[x]=IO.readlines(a[x])*"\n"}
a.reverse!
(0..1).each{|x|IO.write(a[x],b[x])}

不是最短的,但非常简单易读。另外,没有中间文件,只有RAM。


1
鉴于这是一个代码问题,您至少可以取出空格并尝试一些最短的空格。
Ry-

完全没有意识到这是代码高尔夫,对不起

1

的PHP

$f1 = 'file1.txt';
$f2 = 'file2.txt';

$f1contents = file_get_contents ($f1);
$f2contents = file_get_contents ($f2);

file_put_contents($f1, $f2contents);
file_put_contents($f2, $f1contents);

10
那不是真正的高尔夫运动:(
Jakob Bowyer

1

Shell脚本,24

可在Bash和大多数外壳中使用。传递两个文件名作为参数。

mv $1 ੴ;mv $2 $1;mv ੴ $2

如果您想要固定的文件名,则可以这样做,但要收取12个字符的罚款:

mv i.txt ੴ;mv o.txt i.txt;mv ੴ o.txt

@minitech,是的。“ੴ”变为“ o.txt”。这些是mv操作,不是cp
达伦·斯通

到目前为止,第二好的答案!减少两个字符,您就赢了!我不在乎固定的文件名。

1

蟒蛇:

import os
l,e='i_o'*2,'.txt'
for x,y in zip(l,l[1:])[::2]:
 os.rename(x+e,y+e)

1

Windows批处理文件(48)

type i.txt>a&type o.txt>i.txt&type a>o.txt&del a

我写这篇文章时忘记了move命令...


您刚刚重写了我的a文件!
Danny

@Danny问题指出您可以创建另一个文件,但是必须将其删除。
kitcar2000

1

C 162

打高尔夫球:使用t.txt作为tmp文件并交换名称,然后删除t.txt。

#include <stdio.h>
#define R(x,y) rename(x,y)
#define X(x) remove(x)
int main(){char *i="i.txt",*o="o.txt",*t="t.txt";R(i,t);X(i);R(o,i);R(t,o);X(t);return 0;}

编辑:删除了2个空格


1

PHP -172

@EisaAdil的答案的高尔夫球版本

$f1='file1.txt';$f2='file2.txt';$f1contents=file_get_contents($f1);$f2contents=file_get_contents($f2);file_put_contents($f1,$f2contents);file_put_contents($f2,$f1contents);

1

Rebol-46(重命名文件)或55(r / w内容)

重命名文件(t用作临时文件):

r: :rename 
r i: %i.txt %t
r o: %o.txt i
r %t o

读然后写出文件内容:

a: read i: %i.txt
b: read o: %o.txt
write o a 
write i b

1

PHP,45

<?php
copy('i','t');copy('o','i');rename('t','o');

到目前为止还不是最简单,但是最短的PHP。


文件扩展名丢失。
泰特斯

1

Groovy-99个字符

这是我在Groovy 2.2.1中的尝试。我尝试不重命名就这样做:

f={new File(it+".txt")}
w={x,y->x.withWriter{it.write y}}
i=f "i"
o=f "o"
t=i.text
w i,o.text
w o,t

取消高尔夫:

file = { new File(it+".txt") }
writeTextToFile = { x,y -> x.withWriter{it.write y} }

iFile = file("i")
oFile = file("o")

iText = iFile.text
writeTextToFile (iFile,oFile.text)
writeTextToFile (oFile,iText)

1

VBA(148 ... 132)和(126 ... 110)

在c:\驱动器中使用临时文件t重命名。也是第一次尝试高尔夫:S

Sub s():Set f=CreateObject("Scripting.FileSystemObject"):i="c:\i.txt":t="c:\t":f.MoveFile i,t:f.MoveFile "c:\o.txt",i:Kill t:End Sub

如果已经引用了scrrun.dll,则可以将其减少到126 ... 110。

Sub s():Set f=new FileSystemObject:i="c:\i.txt":t="c:\t":f.MoveFile i,t:f.MoveFile "c:\o.txt",i:Kill t:End Sub

看起来您的代码有很多空白。您确定需要它们吗?如果将其删除,则得分会更高。
user12205 2014年

我认为编辑器不需要将它扔进去。从必要的地方除去:)
jaybee3 2014年

1

C:65个字符

#define r(a,b)rename(#a".txt",#b".txt");
main(){r(i,)r(o,i)r(,o)}

用C语言完成的一个非常简单的解决方案。.txt在为文件之一使用适当的新名称之前,它会使用一个临时名称()。

取消关注(请注意语法高亮显示在定义中如何失败,发现了一个错误!):

#include <stdio.h>

#define r(a, b) rename(#a ".txt", #b ".txt");

int main()
{
    r(i,  ) // rename("i.txt",  ".txt");
    r(o, i) // rename("o.txt", "i.txt");
    r( , o) // rename( ".txt", "o.txt");

    return 0;
}

1

Perl,120字节(内容交换而不重命名文件)

use open IO,':bytes';undef$/;open I,"<i.txt";$I=<I>;open I,"<o.txt";open O,">i.txt";print O<I>;open O,">o.txt";print O$I

文件内容被存入内存并写回到另一个文件。因此i.txto.txt必须适合内存。

由于实际上交换了文件内容,因此硬链接会自动更新,请参见AJManfield的注释

取消高尔夫:

use open IO => ':bytes'; # binmode, not needed for Unix, but needed for Windows
undef $/;                # read whole file instead of lines
open I, "<i.txt";        # open i.txt for reading
$I = <I>;                # read i.txt
open I, "<o.txt";        # open o.txt for reading
open O, ">i.txt";        # open i.txt for writing
print O <I>;             # read o.txt and put the contents in i.txt
open O, ">o.txt";        # open o.txt for writing
print O $I;              # write o.txt with contents of old i.txt


1

Lua中71 70字节

_ENV=os t='.txt'i,o,r='i'..t,'o'..t,rename r(i,t)r(o,i)r(t,o)remove(t)

在线尝试!

os将操作系统库定义为全局表,以便我们可以编写renameremove代替os.renameand os.remove。(顺便说一句,这也意味着一个字母的变量实际上是os表中的字段。)为os.rename节省一些空间而使用短别名。设置文件名变量,'.txt'用作临时文件。重新命名和删除。



1

Tcl,122字节

set A [read [open i.txt]]
puts [set i [open i.txt w]] [read [open o.txt]]
puts [set o [open o.txt w]] $A
close $i
close $o

在线尝试!


Tcl,169字节

set A [read [set i [open i.txt r]]]
set B [read [set o [open o.txt r]]]
close $i
close $o
puts [set i [open i.txt w]] $B
puts [set o [open o.txt w]] $A
close $i
close $o

在线尝试!


0

SmileBASIC,36 35字节

@L
RENAME@OI[2-I],@IO[I]I=I+1GOTO@L
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.