ohnycany
ohnycany

Reputation: 67

AWK script to print the number of characters in a line

I want to output the number of characters in each line in the txt file after printing the line. The awk script I am using is:

awk 'length($0)>19 END {print length($0)}' marks.txt,

but it doesn't print the number of characters in each line. There is a 0 after printing the file content in the output, but this is not what I need. Could anyone point out what I did wrong? Thanks!

The content of the marks.txt file is:

1) Amit         Physics  80
2) Rahul        Maths    90
3) Shyam        Biology  87
4) Kedar        English  85
5) Hari         History  89

The output is

1) Amit         Physics  80
2) Rahul        Maths    90
3) Shyam        Biology  87
4) Kedar        English  85
5) Hari         History  89
0

Upvotes: 3

Views: 1411

Answers (1)

Dudi Boy
Dudi Boy

Reputation: 4865

Using awk

awk '{print $0, length}' input.txt

output

1) Amit         Physics  80 27
2) Rahul        Maths    90 27
3) Shyam        Biology  87 27
4) Kedar        English  85 27
5) Hari         History  89 27

Upvotes: 2

Related Questions