Home > Enterprise >  is there any way in shell scripting by using awk/sed to make regular spacing between words in file i
is there any way in shell scripting by using awk/sed to make regular spacing between words in file i

Time:09-09

Suppose i have output file of my scipt which is printed using echo like,

     ip           hostname    username   file
192.168.1.111     localhost1    abc      file1
192.168.2.22    localhost1    abc1     file2
192.168.3.3    localhost1    abc11    file3
192.168.4.44     localhost1   abc111   file4

Expected Output:-

  ip             hostname    username    file
192.168.1.111   localhost1    abc        file1
192.168.2.22    localhost1    abc1       file2
192.168.3.3     localhost1    abc11      file3
192.168.4.44    localhost1    abc111     file4

tried:-

awk '{ printf "%-20s %-20s %-20s %-20s \n", $1 , $2 , $3 , $4 }' file_name

but it's not working is there any way to make file appropriate spacing and readable

CodePudding user response:

Using column with --table option

column --table  < input.txt 
ip             hostname    username  file
192.168.1.111  localhost1  abc       file1
192.168.2.22   localhost1  abc1      file2
192.168.3.3    localhost1  abc11     file3
192.168.4.44   localhost1  abc111    file4

CodePudding user response:

With your shown samples please try following. Use combination of awk column to get the expected results. Pass your Input_file to awk command where setting OFS to \t and then printing the current line doing $1=$1 to apply TABs as separators. Then passing its output to column as standard input and printing it properly using TAB as delmiter.

awk 'BEGIN{OFS="\t"} {print $1,$2,$3,$4}' Input_file | column -t -s $'\t' 
  • Related