首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将文本转换为表(空格分隔或固定长度)

将文本转换为表(空格分隔或固定长度)
EN

Stack Overflow用户
提问于 2019-11-24 07:00:44
回答 4查看 365关注 0票数 0

我有一个带有制表符分隔数据(150行)的文本文件,我希望将其转换为以空格分隔或固定长度的列。我尝试过使用Excel的.prn格式导出文件,但是当在MS记事本中打开时,它会丢失所有格式。

让我们将该文件考虑为:

代码语言:javascript
运行
复制
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

现在想要的是:

代码语言:javascript
运行
复制
       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.来完成这一任务。我喜欢终点站。

提前谢谢.!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-11-24 07:16:09

你能试一下吗。

代码语言:javascript
运行
复制
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

一种线性溶液形式:

代码语言:javascript
运行
复制
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

解释:添加对上述代码的解释。

代码语言:javascript
运行
复制
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.

输出如下。

代码语言:javascript
运行
复制
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。有两种类型的安装:有安装程序或没有安装程序。选择由你来决定。下面的描述是基于没有安装程序的情况。
  • 解压缩下载的文件以提取任意位置的二进制文件和模块。(下载文件夹、桌面或其他地方)。
  • 在桌面或任何方便的地方创建一个具有任意名称(例如"myawk")的工作文件夹。
  • 将下面的脚本复制到具有任意名称的文件(如"script.txt")。 由于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打开到上面创建的工作文件夹。
  • 然后按以下方式在终端上键入: C:\your\path\to\gawk.exe -f script.txt Input_file.txt Input_file.txt > Output_file.txt 请根据您的系统修改字符串"C:\yout\path\to\gawk.exe“。 如果您已经在安装程序中安装了gawk,或者将-path- to -gawk-可执行文件附加到环境变量PATH中,则只需键入以下内容: gawk.exe -f script.txt Input_file.txt Input_file.txt > Output_file.txt
  • 您可以在Output_file.txt中找到结果。请确保使用单空格字体显示垂直排列的列。

虽然awkgawk的诞生已经过去了很长时间,但它仍然没有过时。为了提高工作效率和生产效率,请享受黑客服务。

票数 4
EN

Stack Overflow用户

发布于 2019-11-24 09:24:44

perl版本(因为听起来您在使用Windows,如果您还没有Perl,请安装Strawberry perl ):

代码语言:javascript
运行
复制
#!/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";
}

示例:

代码语言:javascript
运行
复制
$ 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
票数 1
EN

Stack Overflow用户

发布于 2019-11-24 11:46:47

这可能对您有用(GNU sed):

代码语言:javascript
运行
复制
sed -E '1{s/\S+ \S+/       &              /;b};:a;/^.{39,} \S+$/!s/^(.*) /\1  /;ta' file

标题放在列的上方,其余的行用空格在右边填充第一个字段,使其设置为40个字符的宽度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59015387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档