如何将半行分成两列?

时间:2018-04-28 09:37:44

标签: vim awk

我想改变这篇文章,剪掉半条,粘贴成两个cloumns。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Xml;
using System.IO;

namespace ReadXML
{
    class Program
    {
        private static string XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + Environment.NewLine +
                                    "<log>" + Environment.NewLine +
                                    "    <logData id=\"Alpha\">"              + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>100</index>"          + Environment.NewLine +
                                    "            <value>150</value>"          + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>110</index>"          + Environment.NewLine +
                                    "            <value>750</value>"          + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>115</index>"          + Environment.NewLine +
                                    "            <value>150</value>"          + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>110</index>"          + Environment.NewLine +
                                    "            <value>750</value>"          + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>120</index>"          + Environment.NewLine +
                                    "            <value>750</value>"          + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>130</index>"          + Environment.NewLine +
                                    "            <value>150</value>"          + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "    </logData>"                          + Environment.NewLine +
                                    "    <logData id=\"Bravo\">"              + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>100</index>"          + Environment.NewLine +
                                    "            <value>25</value></data>"    + Environment.NewLine +
                                    "        <data>"                          + Environment.NewLine +
                                    "            <index>110</index>"          + Environment.NewLine +
                                    "            <value>11</value>"           + Environment.NewLine +
                                    "        </data>"                         + Environment.NewLine +
                                    "    </logData>"                          + Environment.NewLine +
                                    "</log>";
        }
        public static List<LogData> GetLogDatas(string xml)
        {
            List<LogData> logDatas = new List<LogData>();
            string wasteFile = "wasteFile.xml";
            File.WriteAllText(wasteFile, xml);
            using (XmlReader reader = XmlReader.Create(wasteFile))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        Debug.WriteLine($"LocalName            {reader.LocalName}");
                        if (reader.HasAttributes)
                            Debug.WriteLine($"Attribute:           {reader.GetAttribute("id")}");
                        //Debug.WriteLine($"Value1               {reader.Value}");  //this is empty
                        Debug.WriteLine($"Value2               {reader.ReadString()}");
                    }
                    else
                    {
                        //Debug.WriteLine("not IsStartElement");
                    }
                }
            }
            return logDatas;
        }
    }
    public class LogData
    {
        public string ID { get; }
        public List<LogPoint> LogPoints { get; } = new List<LogPoint>();
        public LogData (string id)
        {
            ID = id;
        }
    }
    public class LogPoint
    {
        public int Index { get; }
        public double Value { get; }
        public LogPoint ( int index, double value)
        {
            Index = index;
            Value = value;
        }
    }
}

Trget:

line1 Ipsum dignissimos doloribus
line2 Sit odio placeat
line3 Consectetur quia
line4 Elit adipisicing
line5 Adipisicing modi
line6 Adipisicing quae
line7 Consectetur tempore
line8 Adipisicing elit
line9 Dolor nam
line10 Dolor esse
line11 Elit amet
line12 Amet quos recusandae  

请用awk或vim简单介绍一下。

enter image description here

我尝试使用vim:

line1 Ipsum dignissimos doloribus     line7 Consectetur tempore
line2 Sit odio placeat                line8 Adipisicing elit 
line3 Consectetur quia                line9 Dolor nam 
line4 Elit adipisicing                line10 Dolor esse
line5 Adipisicing modi                line11 Elit amet 
line6 Adipisicing quae                line12 Amet quos recusandae 

失败,无法得到我想要的东西 enter image description here

8 个答案:

答案 0 :(得分:8)

pr是您要查找的命令。像这样使用它:

pr -t2 file.txt

-t省略了标题。 -2表示两列。

答案 1 :(得分:3)

@ hek2mgls pr解决方案显然是要走的路,但是如果你因为某些原因需要使用awk那么那就是:

$ awk '
    { a[NR]=$0; c=length($0); w=(c>w?c:w) }
    END { m=int((NR+1)/2); for (i=1;i<=m;i++) printf "%-*s\t%-s\n", w, a[i], a[m+i] }
' file
line1 Ipsum dignissimos doloribus       line7 Consectetur tempore
line2 Sit odio placeat                  line8 Adipisicing elit
line3 Consectetur quia                  line9 Dolor nam
line4 Elit adipisicing                  line10 Dolor esse
line5 Adipisicing modi                  line11 Elit amet
line6 Adipisicing quae                  line12 Amet quos recusandae

答案 2 :(得分:1)

您可以使用splitpaste,例如:

split -l 6 file.txt parts

这将创建2个文件,每个文件有6行

然后使用paste创建一个输出为split的新文件:

paste parts* > file.txt

根据您的示例,它将生成如下内容:

line1 Ipsum dignissimos doloribus       line7 Consectetur tempore
line2 Sit odio placeat  line8 Adipisicing elit
line3 Consectetur quia  line9 Dolor nam
line4 Elit adipisicing  line10 Dolor esse
line5 Adipisicing modi  line11 Elit amet
line6 Adipisicing quae  line12 Amet quos recusandae

为了在线之间建立空格,您可以使用expand例如:

paste parts* | expand -t 20

它将返回:

line1 Ipsum dignissimos doloribus       line7 Consectetur tempore
line2 Sit odio placeat                  line8 Adipisicing elit
line3 Consectetur quia                  line9 Dolor nam
line4 Elit adipisicing                  line10 Dolor esse
line5 Adipisicing modi                  line11 Elit amet
line6 Adipisicing quae                  line12 Amet quos recusandae

现在从vim使用此功能,您可以使用:

:!split -l 6 % /tmp/parts; paste /tmp/parts* | expand -t 20 > %; rm /tmp/parts*

答案 3 :(得分:1)

...或使用rs( — reshape a data array )

$ cat file | rs
line1 Ipsum dignissimos doloribus  line7 Consectetur tempore
line2 Sit odio placeat             line8 Adipisicing elit
line3 Consectetur quia             line9 Dolor nam
line4 Elit adipisicing             line10 Dolor esse
line5 Adipisicing modi             line11 Elit amet
line6 Adipisicing quae             line12 Amet quos recusandae  

答案 4 :(得分:1)

要在Vim中完成此操作,您可以使用尾随空格填充第一行,然后转到第7行并输入此键序列:

<c-v>05j$dgg$p

解释(<c-v>代表Ctrl-v):

<c-v>05j$dgg$p
<c-v>                      " Enter visual-block mode
     0                     " Move to beginning of line
      5j                   " Move 5 lines below
        $                  " Move to last character of line
         d                 " Delete visual selection
          gg               " Move to first line
            $p             " Paste text at the end of the line

另外在Vim的命令行中输入它(光标应该在第7行):

:exe "norm \<c-v>05j$dgg$p"

答案 5 :(得分:1)

准备:添加空格以使第一行在范围(1,6行)中最长。 在正常模式下,7gg

<c-v>6j$dgg$p
<c-v>                      " Enter visual-block mode
      6j                   " Move 5 lines below
        $                  " Move to last character of line
         d                 " Delete visual selection
          gg               " Move to first line
            $p             " Paste text at the end of the line

答案 6 :(得分:0)

如果你不是在寻找输出序列应该与Input_file的序列相同,那么下面的内容可以帮助你。

awk -v line=$(wc -l < Input_file) '{val=FNR==((line/2)+1)||FNR==1?1:++val;a[val]=a[val]?a[val] OFS $0:$0} END{for(i in a){print a[i]}}'  Input_file

编辑: 现在也添加一种非单一形式的解决方案。

awk -v line=$(wc -l < Input_file) '
{
  val=FNR==((line/2)+1)||FNR==1?1:++val;
  a[val]=a[val]?a[val] OFS $0:$0}
END{
  for(i in a){
    print a[i]}
}
'  Input_file

如果您想在Input_file中进行更改,请在上面的代码中追加以下> temp_file && mv temp_file Input_file

答案 7 :(得分:0)

我的解决方案涉及将寄存器从linewise更改为blockwise 挤压空间。

Sheet1

如果将此功能复制到剪贴板,可以这样使用:

fun! GiveItaNameYouWant()
    let @+=''
    7
    normal "+d}
    call setreg('+',@+,'b')
    normal gg
    execute "normal! A \<esc>"
    execute "normal! \"+p"
    %s/  \+/ /g
endfun

在我的情况下,如果你不想删除它,我会删除行:@+ Enter :call GiveItaNameYouWant() Enter 的额外空格。