0

When I do a desc hive table like below on a Linux terminal CLI.

hive -e "desc hive_db.hive_table"

I get the following output

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

Now I want to redirect the output to a file before doing it I want to remove the spaces between each fields and have just one space between them.

The expected output is below

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

I have tried like below

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

The output I get is

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

Th output I get is a close one but I have one space and one tab between each fields in each line

How can I achieve what I want?

nmr
  • 605
  • 6
  • 20
  • 4
    Possible duplicate of [How to replace multiple spaces with a single space using Bash?](https://stackoverflow.com/q/50259869/608639), [sed command to replace multiple spaces into single spaces](https://stackoverflow.com/q/22939323/608639), [How to remove extra spaces in bash?](https://stackoverflow.com/q/13092360/608639), etc. – jww May 26 '19 at 22:41

1 Answers1

2

Try:

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

[[:space:]]+ matches one or more of any kind of white space (tab, blank, or other including any unicode-defined white space). s/[[:space:]]+/ / replaces that sequence of white space with a single blank.

John1024
  • 109,961
  • 14
  • 137
  • 171