如何删除第一个n列,其中包含文本文件中的空白linux shell脚本
我想删除包含此文本文件空白的前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列?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从文本文件中删除任意列可以通过Linux Shell上的
COLRM
来完成。IBM的此命令行工具记录在在这里。
因此,可以从
sample.txt
中删除前6列,可以通过产生所需的输出来完成
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 byresulting in the desired output
如果您需要从每行中删除 n 第一个字符,则gnu
awk
substr
函数很方便,让file> file> file> file.txt
content theOutter
输出
说明:
打印
当前行的一部分($ 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, letfile.txt
content bethen
output
Explanation:
print
part of current line ($0
) starting at7
th character.(tested in gawk 4.2.1)
这是蛮力,但完成了工作:
〜
it's brute force but gets the job done :
~