awk - count character count of fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - count character count of fields
# 1  
Old 11-28-2012
Linux awk - count character count of fields

Hello All,

I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form

Code:
1|121|asda|434|thesi|2012|05|24|
1|343|unit|09|best|2012|11|5|

I was put into a scenario where I need the field count in all the lines in that file. It was simply achieved by
Code:
 awk -F"|" '{print NF}' file

Now the case is, I need the count of characters in a field. For example,

Code:
 head -2 file | awk -F"|" '{do something}'

Output should be,
Code:
1-3-4-3-5-4-2-2
1-3-4-2-4-4-2-1

Thanks Smilie

Moderator's Comments:
Mod Comment Thank you for changing the subject title.
Google search:
"awk - count character count of fields" - Top 5
"awk - need help" - nowhere to be seen

Last edited by PikK45; 11-28-2012 at 05:00 AM.. Reason: Inappropriate Title
# 2  
Old 11-28-2012
Try:
Code:
awk -F"|" '{for (i=1;i<NF;i++) printf length($i)"-";printf length($NF)"\n"}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-28-2012
Quote:
Originally Posted by bartus11
Try:
Code:
awk -F"|" '{for (i=1;i<NF;i++) printf length($i)"-";printf length($NF)"\n"}' file

As per OP's input. it should be NF-1

Code:
awk -F"|" '{for (i=1;i<(NF-1);i++) printf length($i)"-";printf length($(NF-1))"\n"}' file

OR

Code:
awk -F "|" '{for(i=1;i<NF;i++){s=s?s"-"length($i):length($i)}print s;s=""}' file


Last edited by pamu; 11-28-2012 at 05:44 AM.. Reason: correction..
# 4  
Old 11-28-2012
I guess printf length($NF)"\n" is used to make the jump line, but i do not understand its usage.
Can you detail it please ?

Thank You
# 5  
Old 11-28-2012
Quote:
Originally Posted by Fundix
I guess printf length($NF)"\n" is used to make the jump line, but i do not understand its usage.
Can you detail it please ?

Thank You
It is just attaching newline character to the end of the last field's length.
This User Gave Thanks to bartus11 For This Post:
# 6  
Old 11-28-2012
Thank you guys Smilie

Especially, bartus11 & pamu. Actually I thought of using sizeof() as in C. Smilie
# 7  
Old 11-28-2012
Alternatively:
Code:
awk '{for(i=1;i<NF;i++)$i=length($i)}NF--' FS=\| OFS=- file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you :). file X 5811530 . G C NLGN4X 17 10544696 . GA G MYH3 9 96439004 . C ... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

awk to output match and mismatch with count using specific fields

In the below awk I am trying output to one file those lines that match between $2,$3,$4 of file1 and file2 with the count in (). I am also trying to output those lines that are missing between $2,$3,$4 of file1 and file2 with the count of in () each. Both input files are tab-delimited, but the... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

awk to count and rename based on fields

In the below awk using the tab-delimited input, I am trying count the - symbol in $5 and output the count as well as the renamed condition ins. I am also count the - symbol in $6 and output the count as well as the renamed condition del. I am also count the tomes that in $5 and $6 there are... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

6. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

7. Shell Programming and Scripting

awk count fields not working

Hi, i am trying to count the fields in a file. Input: 100,1000,,2000,3000,10/26/2012 12:12:30 200,3000,,1000,01/28/2012 17:12:30 300,5000,,5000,7000,09/06/2012 16:12:30 output: Cout of the fileds for each row 6 5 6 awk -F"," '{print $NF}' file1.txt When i try with above awk... (3 Replies)
Discussion started by: onesuri
3 Replies

8. Shell Programming and Scripting

count and number instances of a character in sed or awk

I currently use LaTeX together with a sed script to set cloze test papers for my students. I currently pepend and equals sign to the front of the words I want to leave out in the finished test, =perpendicular, for example. I am able to number the blanks using a variable in LaTeX. I would like to... (8 Replies)
Discussion started by: maouinin
8 Replies

9. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

10. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies
Login or Register to Ask a Question

Featured Tech Videos