0

I have to format the output of the awk command which gets the input from a file

Sample Input File

123 ABC 12 23 34 45 56 67 78 13
234 DEF 23 34 45 56 67 78 89
345 GHI 34 45 56 67 78 90 10 01 99
456 JKL 45 56 67 78 90 12 34

Required Output

user | 123 ABC 12 | 23 34 45 56 67 78 13
user | 234 DEF 23 | 34 45 56 67 78 89
user | 345 GHI 34 | 45 56 67 78 90 10 01 99
user | 456 JKL 45 | 56 67 78 90 12 34

I tried the below command which gave me some error

awk -v user=$user '{ printf user "|" $1" "$2" "$3 "|" "$1=$2=$3=""" $0;printf("\n"); }' sampleinputfile

Error Message

user | 123 ABC 12 | 23 34 45 56 67 78 13
user | 234 DEF 23 | 34 45 56 67 78 89
user | 345 GHI 34 | 45 56 67 78 90 10 01 99
user | 456 JKL 45 | 56 67 78 90 12 34
awk: (FILENAME=sampleinputfile FNR=4) fatal: not enough arguments to satisfy format string
        `user | 456 JKL 45|$1=$2=$3=456 JKL 45 56 67 78 90 12 34
                                                      ^ ran out for this one

1 Answer 1

0

One way:

awk '{$3=$3" | ";print "user | "$0;}' file
1
  • Thanks Guru!! I tweaked little to suit my requirement awk -v user=$user '{$3=$3" | ";print user " | "$0;}' file
    – ramp
    Commented Jan 19, 2016 at 2:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .