举个例子:
$ du -h file_size.txt 112K file_size.txt
我将如何从du -h的输出中删除文件名
我试图用sed来search一个string(文件名),并用什么都replace,但它没有奏效(下面的命令)
du -h file_size.txt | sed 's/ 'file_size.txt'//'
有人能指出为什么这不行,或者更好的办法吗?
问候
保罗
在这个命令行中有一些不好的引用。 最简单的可能是:
du -h file_size.txt | cut -f -1
修复你的命令行:
du -h file_size.txt | sed 's/file_size.txt//'
由于你的文章有一个awk
标签:
du -h file_size.txt | awk '{ print $1 }'
du -h file_size.txt | cut -f1
简单输出第一列。 这是awk解决方案
du -h test.txt | awk '{ print $1 }'
这有点冗长,但无论如何,这是你想要的另一种方式:
du -h file_size.txt |while read x junk; do echo $x; done
@OP如果你的数据具有不同的字段,例如空格/制表符,那么它更容易使用的工具,如awk / cut,通过分隔符将它们分割在这些字段上。 使用正则表达式(例如sed)是没有必要的。 更好的是使用shell。
各种方式
$ du -h file | awk '{print $1}' 4.0K $ du -h file | cut -f1 4.0K $ set -- $(du -h file) #using shell only $ echo $1 4.0K
你原来的命令会起作用,只是你在sed regexp中包含一个空格,而du输出标签。
du -h
的输出是[tab]分隔的。
du -h file_size.txt |sed 's/[\t].*//g'