同样,我有大约150个没有头的数据的文件
x1 y1 z1 x2 y2 z2 ..。 锌锌
分隔符恰好是选项卡键。如何对这150个文件使用sed和批处理来实现以下输出:
x1 x2 x3 ..。 xn y1 y2 y3 ..。 yn z1 z2 z3 。。 锌
任何想法都将不胜感激。
注:我以前发过类似的问题,而不是重复。请看这个link。
致以敬意,
伊克尔
发布于 2013-01-28 06:36:53
我不认为sed是这份工作的最佳工具。想到的最简单的解决方案只是三次使用cut:
cut -f1 file && cut -f2 file && cut -f3 filefile含量
x1 y1 z1
x2 y2 z2
x3 y3 z3
xn yn zn结果:
x1
x2
x3
xn
y1
y2
y3
yn
z1
z2
z3
zn对于批处理文件,假设您的当前工作目录中只有感兴趣的文件:
for i in *; do
cut -f1 "$i" >> "$i.bak"
cut -f2 "$i" >> "$i.bak"
cut -f3 "$i" >> "$i.bak"
mv "$i.bak" "$i"
done发布于 2013-01-28 06:35:19
我希望你不会对perl过敏..。
此解决方案适用于具有任意数量列的文件:
$ perl -ne 'BEGIN { @a = (); } $i = 0; foreach (split(/\s+/)) { $l = ($a[$i++] ||= []); push @$l, $_; }; END { print join("\n", @$_) . "\n" foreach (@a); }' << EOF
> x1 y1 z1
> x2 y2 z2
> x3 y3 z3
> x4 y4 z4
> EOF
x1
x2
x3
x4
y1
y2
y3
y4
z1
z2
z3
z4我要评论一下,因为这并不是很明显:
perl -n逐行读取(准确地说,它针对$/读取和拆分),-e执行脚本;BEGIN块,最后执行END块。解剖学:
BEGIN { @a = (); } # Creates an array named "a"
# Main scriptlet
$i = 0;
foreach (split(/\s+/)) { # Split an input line against one or more space chars
$l = # Set $l to...
($a[$i++] ||= []); # what is at index i of @a (increment i), but if not set,
# set to an (empty) array ref and return that
push @$l, $_; # Push token to the end of the array ref
}
END { # End block...
print join("\n", @$_) # Print the contents of array references, joined with \n,
. "\n" # then \n,
foreach (@a); # for each element of array a
} # DONEhttps://stackoverflow.com/questions/14556376
复制相似问题