我有一个带有制表符分隔数据(150行)的文本文件,我希望将其转换为以空格分隔或固定长度的列。我尝试过使用Excel的.prn
格式导出文件,但是当在MS记事本中打开时,它会丢失所有格式。
让我们将该文件考虑为:
Product Name Product Key
Autodesk 3ds Max 2019 128K1
Autodesk 3ds Max 2019 with Softimage 978K1
Autodesk Advance Steel 2019 959K1
Autodesk Alias AutoStudio 2019 966K1
Autodesk Alias Concept 2019 A63K1
Autodesk Alias Design 2019 712K1
Autodesk Alias SpeedForm 2019 A62K1
Autodesk Alias Surface 2019 736K1
Autodesk AutoCAD 2019 001K1
现在想要的是:
Product Name Product Key
Autodesk 3ds Max 2019 128K1
Autodesk 3ds Max 2019 with Softimage 978K1
Autodesk Advance Steel 2019 959K1
Autodesk Alias AutoStudio 2019 966K1
Autodesk Alias Concept 2019 A63K1
Autodesk Alias Design 2019 712K1
Autodesk Alias SpeedForm 2019 A62K1
Autodesk Alias Surface 2019 736K1
Autodesk AutoCAD 2019 001K1
我已经使用了这工具,它做了这项工作,但再次当保存在MS记事本,列是错误的,我希望数据只在记事本.
无论如何,Notepad..
EXCEL
**.**CMD
**.**SHELL
.中的任何工具都可以使用P.S.来完成这一任务。我喜欢终点站。
提前谢谢.!
发布于 2019-11-24 07:16:09
你能试一下吗。
awk '
FNR==NR{
len=length($0)>len?length($0):len
next
}
{
val=$NF
$NF=""
$1=$1
printf("%-"len"s%s\n",$0,val)
}
' Input_file Input_file
一种线性溶液形式:
awk 'FNR==NR{len=length($0)>len?length($0):len;next} {val=$NF;$NF="";$1=$1;printf("%-"len"s%s\n",$0,val)}' Input_file Input_file
解释:添加对上述代码的解释。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when first time Input_file is being read.
len=length($0)>len?length($0):len ##Creating variable len whose value is either length of current line or len value whichever is having higher value.
next ##next will skip all further statements from here.
} ##Closing BLOCK for FNR==NR condition here.
{ ##Starting BLOCK which will be executed when 2nd time Input_file is being read.
val=$NF ##Creating variable val whose value is $NF(last field of current line).
$NF="" ##Nullifying last field of current line.
$1=$1 ##re-assigning value of $1 to itself to adjust $0.
printf("%-"len"s %s\n",$0,val) ##Printing current line with mentioning %- with variable len to add spaces at last of current line and then printing last field with new line.
} ##Closing BLOCK for which was opened for 2nd time Input_file is being read.
' Input_file Input_file ##Mentioning Input_file names here.
输出如下。
Product Name Product Key
Autodesk 3ds Max 2019 128K1
Autodesk 3ds Max 2019 with Softimage 978K1
Autodesk Advance Steel 2019 959K1
Autodesk Alias AutoStudio 2019 966K1
Autodesk Alias Concept 2019 A63K1
Autodesk Alias Design 2019 712K1
Autodesk Alias SpeedForm 2019 A62K1
Autodesk Alias Surface 2019 736K1
Autodesk AutoCAD 2019 001K1
面向Windows用户的:
如果已经安装了Windows Subsystem for Linux
,就可以像上面在bash命令行中描述的那样直接执行awk脚本。
如果您已经(或将要安装) gawk
作为一个独立的应用程序软件,下面的指导将有帮助:
Gawk for Windows
)下载sourceforge。有两种类型的安装:有安装程序或没有安装程序。选择由你来决定。下面的描述是基于没有安装程序的情况。awk
可执行文件并不关心脚本文件的扩展名,所以可以将其与".txt“相关联以关联文本编辑器,也可以将其更改为".awk”以进行规范。
FNR==NR{ len=length($0)>len?length($0):len next }{ val=$NF $NF=“$1=$1 printf”(%-“len”%s\n,$0,val) }cmd
终端并将chdir
打开到上面创建的工作文件夹。gawk
,或者将-path- to -gawk-可执行文件附加到环境变量PATH
中,则只需键入以下内容:
gawk.exe -f script.txt Input_file.txt Input_file.txt > Output_file.txtOutput_file.txt
中找到结果。请确保使用单空格字体显示垂直排列的列。虽然awk
或gawk
的诞生已经过去了很长时间,但它仍然没有过时。为了提高工作效率和生产效率,请享受黑客服务。
发布于 2019-11-24 09:24:44
perl版本(因为听起来您在使用Windows,如果您还没有Perl,请安装Strawberry perl ):
#!/usr/bin/env perl
# Save in a file instead of trying to use as a one-liner
use warnings;
use strict;
use autodie;
use List::Util qw/max/;
use Fcntl qw/:seek/;
my $file = shift;
open my $INFILE, "<", $file;
my @lens;
while (<$INFILE>) {
chomp;
my @F = split /\t/;
for my $col (0 .. $#F) {
$lens[$col] = max(length $F[$col], $lens[$col]//0);
}
}
seek $INFILE, 0, SEEK_SET;
while (<$INFILE>) {
chomp;
my @F = split /\t/;
for my $col (0 .. $#F) {
printf "%-*s ", $lens[$col], $F[$col];
}
print "\n";
}
示例:
$ perl widify input.tsv
Product Name Product Key
Autodesk 3ds Max 2019 128K1
Autodesk 3ds Max 2019 with Softimage 978K1
Autodesk Advance Steel 2019 959K1
Autodesk Alias AutoStudio 2019 966K1
Autodesk Alias Concept 2019 A63K1
Autodesk Alias Design 2019 712K1
Autodesk Alias SpeedForm 2019 A62K1
Autodesk Alias Surface 2019 736K1
Autodesk AutoCAD 2019 001K1
发布于 2019-11-24 11:46:47
这可能对您有用(GNU sed):
sed -E '1{s/\S+ \S+/ & /;b};:a;/^.{39,} \S+$/!s/^(.*) /\1 /;ta' file
标题放在列的上方,其余的行用空格在右边填充第一个字段,使其设置为40个字符的宽度。
https://stackoverflow.com/questions/59015387
复制相似问题