如何删除第一个n列,其中包含文本文件中的空白linux shell脚本

发布于 2025-02-01 10:39:55 字数 1140 浏览 4 评论 0 原文

我想删除包含此文本文件空白的前6列 sample.txt

      2022-05-26 Mary  Jane
                 foo   bar
      2022-05-27 Tom   Powels
                 lorem ipsum
                 bar   foo
      2022-05-28 Honky Tonk
      2022-05-28 Hill  Billy
      ...

by Linux shell脚本,例如,使用 sed> sed awk 和/或剪切

因此,我在SE中搜索了预期输出

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

,但仅找到了在每行开头删除所有空白的解决方案,例如

$ sed 's/^ *//' sample.txt > output.txt

该文件中导致该文件中

2022-05-26 Mary  Jane
foo   bar
2022-05-27 Tom   Powels
lorem ipsum
bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

在丢失列格式的

。不幸的是, sed 的呼叫

$ sed 's/^ {6}//' sample.txt > output.txt

不起作用。

因此,如何删除包含Linux shell脚本空白的前6列?

I want to remove the first 6 columns containing blanks of this text file sample.txt

      2022-05-26 Mary  Jane
                 foo   bar
      2022-05-27 Tom   Powels
                 lorem ipsum
                 bar   foo
      2022-05-28 Honky Tonk
      2022-05-28 Hill  Billy
      ...

by linux shell scripting, e.g. by using sed, awk and/or cut.

Hence the expected output is

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

I've searched in SE, but only found solutions to remove all blanks at the beginning of each line, e.g.

$ sed 's/^ *//' sample.txt > output.txt

which results in this file

2022-05-26 Mary  Jane
foo   bar
2022-05-27 Tom   Powels
lorem ipsum
bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

where the formatting of the columns is lost.

Unfortunately this call of sed

$ sed 's/^ {6}//' sample.txt > output.txt

doesn't work.

Hence how could I remove the first 6 columns containing blanks by linux shell scripting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所的.畏惧 2025-02-08 10:39:55

从文本文件中删除任意列可以通过Linux Shell上的 COLRM 来完成。
IBM的此命令行工具记录在在这里

因此,可以从 sample.txt 中删除前6列,可以通过

$ colrm 1 6 < sample.txt > output.txt

产生所需的输出来完成

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

Removing arbitrary columns from a text file could be done by colrm on linux shell.
This command line tool from IBM is documented here.

Hence removing the first 6 columns from sample.txt could be done by

$ colrm 1 6 < sample.txt > output.txt

resulting in the desired output

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...
温柔少女心 2025-02-08 10:39:55
 sed -E 's/^ {6}//' sample.txt > output.txt
 awk '{gsub(/^ {6}/,""); print > "output.txt"}' sample.txt
 
 sed -E 's/^ {6}//' sample.txt > output.txt
 awk '{gsub(/^ {6}/,""); print > "output.txt"}' sample.txt
 
情徒 2025-02-08 10:39:55

如果您需要从每行中删除 n 第一个字符,则gnu awk substr 函数很方便,让 file> file> file> file.txt content the

  2022-05-26 Mary  Jane
             foo   bar
  2022-05-27 Tom   Powels
             lorem ipsum
             bar   foo
  2022-05-28 Honky Tonk
  2022-05-28 Hill  Billy
  ...

Outter

awk '{print substr($0,7)}' file.txt

输出

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

说明:打印当前行的一部分( $ 0 )从 7 th字符开始。

(在GAWK 4.2.1中测试)

If you need to remove n first characters from each line, then GNU AWK substr function is handy, let file.txt content be

  2022-05-26 Mary  Jane
             foo   bar
  2022-05-27 Tom   Powels
             lorem ipsum
             bar   foo
  2022-05-28 Honky Tonk
  2022-05-28 Hill  Billy
  ...

then

awk '{print substr($0,7)}' file.txt

output

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
...

Explanation: print part of current line ($0) starting at 7th character.

(tested in gawk 4.2.1)

软糯酥胸 2025-02-08 10:39:55

这是蛮力,但完成了工作:

mawk{1,2} NF++ FS='^      ' OFS=
{n,g}awk  NF++ FS='^ {6}'   OFS=

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy

it's brute force but gets the job done :

mawk{1,2} NF++ FS='^      ' OFS=
{n,g}awk  NF++ FS='^ {6}'   OFS=

~

2022-05-26 Mary  Jane
           foo   bar
2022-05-27 Tom   Powels
           lorem ipsum
           bar   foo
2022-05-28 Honky Tonk
2022-05-28 Hill  Billy
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文