Counting rows line by line from a specific column using Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting rows line by line from a specific column using Awk
# 1  
Old 05-29-2011
Data Counting rows line by line from a specific column using Awk

Dear UNIX community,

I would like to to count characters from a specific row and have them displayed line-by-line.

I have a file called testAwk2.csv which contain the following data:

Code:
rabbit penguin goat 
giraffe emu ostrich

I would like to count in the middle row individually line by line and append the result to the end of each line so the final result appears as follows:

Code:
rabbit penguin goat 7
 giraffe emu ostrich 3

I have tried the following:

Code:
awk '{print $2, wc -m}' testAwk2.csv

Result:
Code:
penguin 0
emu 0

Code:
awk '{print $2 | "wc -m"}' testAwk2.csv

Result:

Code:
12

I thought the above code would produce something like:

Code:
penguin 7
emu 3

But it didn'tSmilie

As you can see from the above code that I have not attempted to append a character count to the end of the line and instead have tried to concentrate in displaying the count for the individual rows.

Any suggestions or tips would be received gladly.

Many thanks,

vnayak
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-29-2011 at 06:53 PM.. Reason: code tags, please!
# 2  
Old 05-29-2011
Code:
awk '{print $0, length($2)}'  testAwk2.csv

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 05-29-2011
Code:
awk '$0=$0 FS length($2)' testAwk2.csv

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 05-30-2011
Hi,

Many thanks for your code. That helped a lot. I was literally barking up the wrong tree until I tried out your code.

Thanks once again. I really appreciated the quick response!

Vnayak
# 5  
Old 05-31-2011
Same thing in perl..

Code:
 
perl -lane 'print $_ ." ". length $F[1]'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

2. Shell Programming and Scripting

Split certain strings in a line for a specific column.

Hi, i need help to extract certain strings/words from lines with different length. I have 3 columns separated by tab delimiter. like below Probable arabinan endo-1,5-alpha-L-arabinosidase A (EC 3.2.1.99) (Endo-1,5-alpha-L-arabinanase A) (ABN A) abnA Ady3G14620 Probable arabinan... (5 Replies)
Discussion started by: redse171
5 Replies

3. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

How to read the value from a specific line and column BASH

Hi All, I have the same problem as the one posted in https://www.unix.com/shell-programming-scripting/96097-how-read-value-specific-line-column-csh-variable.html but I'm using bash. Can anyone tell me how I have to modify the code to make it bash compatible? eval `awk 'NR==3{print "set... (5 Replies)
Discussion started by: f_o_555
5 Replies

8. Shell Programming and Scripting

column data to rows every n line

Hi every one, I am trying to organise an input text file like: input 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 into an output as following: output file 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 (5 Replies)
Discussion started by: nxp
5 Replies

9. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

10. Shell Programming and Scripting

subtitute specific column in line

Hi All, I have problem to solve aaaa,aaaaa,aaa,aaaa,aaa,aa ,aa bbbb,bbbbbbbbb,bbbb,bbbbb ,bb to aaaa;aaaaa,aaa ;aaaa;aaa,aa ;aa bbbb;bbbbbbbbb;bbbb;bbbbb ;bb i try use sed to find and replace, but dont know how to replace specific column position. can u help me?? thx for the... (11 Replies)
Discussion started by: MomoChan
11 Replies
Login or Register to Ask a Question