Jonhnny Weslley
Jonhnny Weslley

Reputation: 1080

Using printf awk function to output nth arguments

Given the following file's content:

alias command with whitespace-separated argument list
anotheralias othercommand and its arguments

how i can to print something like:

       alias = command with whitespace-separated argument list
anotheralias = othercommand and its arguments

Currently I'am using the below command, but it's wrong.

cat aliases | awk '{printf "%20s = %s\n", $1, $0}'

Upvotes: 0

Views: 620

Answers (4)

Vaughn Cato
Vaughn Cato

Reputation: 64308

cat aliases | awk '{$1=sprintf("%20s =",$1);print}'

Upvotes: 3

glenn jackman
glenn jackman

Reputation: 247042

Just use the shell:

while read line; do 
  set -- $line
  cmd=$1
  shift
  printf "%20s = %s\n" "$cmd" "$*"
done < filename

Upvotes: 0

Kent
Kent

Reputation: 195209

another way:

sed 's/ /=/1' yourFile|awk -F= '{printf ("%20s = %s\n",$1,$2)}'

Upvotes: 0

Michael J. Barber
Michael J. Barber

Reputation: 25052

cat aliases | awk '{ printf("%20s =", $1); $1=""; printf("%s\n", $0) }'

Upvotes: 1

Related Questions