在Unix中删除行字段之间的空格

时间:2019-05-26 22:05:58

标签: linux bash unix

当我在Linux终端CLI上执行如下所示的配置表时。

hive -e "desc hive_db.hive_table"

我得到以下输出

date                string  
entity              string  
key                 int 
id                  string  
direction           string  
indicator           string  
account_number      string  
document_date       string

现在,我想在将输出重定向到文件之前,先删除每个字段之间的空格,并且它们之间只有一个空格。

预期输出低于

date string 
entity string   
key int 
id string   
direction string    
indicator string    
account_number string   
document_date string

我尝试过以下方式

hive -e "desc hive_db.hive_table" | tr -s " " > abc.txt

我得到的输出是

date    string  
entity  string  
key     int 
id  string  
direction   string  
indicator   string  
account_number  string  
document_date   string

我得到的输出是接近的输出,但是每行中的每个字段之间都有one space and one tab

我如何实现自己想要的?

1 个答案:

答案 0 :(得分:2)

尝试:

hive -e "desc hive_db.hive_table" | sed -E 's/[[:space:]]+/ /' > abc.txt

[[:space:]]+匹配任何一种空白(选项卡,空白或其他,包括任何Unicode定义的空白)。 s/[[:space:]]+/ /用单个空格替换该空白序列。