【问题标题】:remove spaces between fields of lines in Unix [duplicate]删除Unix中行之间的空格[重复]
【发布时间】:2023-03-11 10:16:01
【问题描述】:

当我在 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

我怎样才能实现我想要的?

【问题讨论】:

标签: linux bash unix


【解决方案1】:

试试:

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

[[:space:]]+ 匹配一个或多个任意类型的空白(制表符、空白或其他包括任何 unicode 定义的空白)。 s/[[:space:]]+/ / 用一个空格替换了该序列的空白。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多