Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用shell格式化文本文件_Linux_Shell_Concatenation - Fatal编程技术网

Linux 使用shell格式化文本文件

Linux 使用shell格式化文本文件,linux,shell,concatenation,Linux,Shell,Concatenation,我想使用shell命令格式化文本文件 文件内容如下: first_name:last_name:some_random_characters Jack:Brown:!@#xyz Mike:Spencer:234QWE Robert:Junior:^#2dsa ... 我希望输出像: JBrown:!@#xyz MSpencer:234QWE RJunior:^#2dsa ... 我尝试使用cat-d':'-f1 file.txt和cat-d':'-f2-3 file.txt, 但是我不知道

我想使用shell命令格式化文本文件

文件内容如下:

first_name:last_name:some_random_characters

Jack:Brown:!@#xyz
Mike:Spencer:234QWE
Robert:Junior:^#2dsa
...
我希望输出像:

JBrown:!@#xyz
MSpencer:234QWE
RJunior:^#2dsa
...
我尝试使用
cat-d':'-f1 file.txt
cat-d':'-f2-3 file.txt
, 但是我不知道如何将这些命令组合起来。

与awk:

awk 'BEGIN{OFS=FS=":"}{print substr($1,1,1)$2, $3}' file.txt > outputfile.txt
上面说:

  • 用冒号分隔每行(并将OFS输出字段分隔符设置为相同字符)
    BEGIN{OFS=FS=“:”}
  • 现在打印出第一列的第一个字符和第二列的所有字符、分隔符和第三列的所有字符
    {print substr($1,1,1)$2,$3}
  • 我认为这是你试图做的,但没有效率

    $ paste -d'\0' <(cut -c1 file) <(cut -d: -f2- file)
    

    $paste-d'\0'我想你的意思是
    cut
    ,而不是
    cat
    。你可以使用管道来组合命令——它们让你“缝合”上一个命令的输出,成为下一个命令的输入。例如,file1.txt | |。。。公元infinitum@MorganMLG:要删除前两行,我建议使用
    tail
    。对于重新组合行的字段,也许
    awk
    sed
    将是一个不错的选择。
    $ paste -d'\0' <(cut -c1 file) <(cut -d: -f2- file)