skip to Main Content

Previously I was using kali linux to code my bash file so it has no issue with the command, but after since I use ubuntu I realized it does not work on ubuntu and can’t find any solution to this problem yet.

Ori Code

tr ':' 't' < venue.txt | tr ' ' '_____" | awk -F "t '$1=Name {print $2,$3,$4,$5,$6}' Name=$Name | column -t --table-columns "Number:", "Type:", "Capacity:" , "Status:" | tr '_____' ' '

venue.txt

B:B01:Random:6:This is a long message
C:C01:Lab:5:This is message

I wanted the output be something like this, without the use of column –table-columns since somehow ubuntu does not support it, best would be awk

(When name==b, output be like)
Number     Type      Capacity      Status
 B01       Random        6         This is a long message

2

Answers


  1. Chosen as BEST ANSWER

    Somehow managed doing it after using this

    tr ':' 't' < venue.txt | tr ' ' '_____" | awk -F "t" 'BEGIN {printf("%-4s %-1s %-3s %s %sn" "Number:", "Type:", "Capacity:" , "Status:")}; %$1=Name {print $2,$3,$4,$5}' Name=$Name | column -t | tr '_____' ' '
    

  2. Your code:

    tr ':' 't' < venue.txt |
    tr ' ' '_____" |
    awk -F "t '$1=Name {print $2,$3,$4,$5,$6}' Name=$Name |
    column -t --table-columns "Number:", "Type:", "Capacity:" , "Status:" |
    tr '_____' ' '
    
    • tr is not needed; awk has -F
    • the second tr command has a double-quote (") where there should be a single-quote, so the entire command meaning changes (the awk command becomes extra arguments to tr – which would give a syntax error). Was this a typo for tr ' ' '_____' ?
    • tr 'x' 'yyyyy' == tr 'x' 'y'
    • Was awk -F "t '... a typo for awk -F 't' '... ?
    • $1=Name assigns the value of variable Name to the first field. Perhaps you intended to compare (==) ?
    • $6 will always be empty since venue.txt has only 5 fields
    • tr 'xxxxx' 'y' == tr 'xxxxx' 'yyyyy' == tr 'x' 'y'

    If you are trying to print the record in venue.txt whose first column equals the value of bash variable Name, without printing the name itself, and converting the : delimiter to a tab and adding a header line to the output, you can do everything with a single awk command:

    awk -F: -v OFS='t' '
        BEGIN { print "Number","Type","Capacity","Status" }
        $1==Name { print $2,$3,$4,$5 }
    ' Name="$Name" venue.txt
    

    It would not be hard to use awk to align the columns but it needs two passes and is more effort than using column:

    awk -F: -v OFS=: '
        BEGIN { print "Number","Type","Capacity","Status" }
        $1==Name { print $2,$3,$4,$5 }
    ' Name="$Name" venue.txt | column -t -s:
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search