how to count string length?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to count string length?
# 1  
Old 05-03-2012
how to count string length?

Hi, Gurus,

I have a requirement which need count string length.
eg.
Code:
abc
bcde
defge

want to get following:
Code:
abc    3
bcde   4 
defge  5

Smilie

thanks in advance!
ken002
# 2  
Old 05-03-2012
Code:
awk '{ $(NF+1)=length($1) } 1' inputfile > outputfile

# 3  
Old 05-03-2012
another way
Code:
while read line
 do 
   printf "$line ${#line} \n"
 done < infile

# 4  
Old 05-03-2012
Code:
awk '$2=gsub(/./,"&")' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-03-2012
Quote:
Originally Posted by Corona688
Code:
awk '{ $(NF+1)=length($1) } 1' inputfile > outputfile

Hi it works, But I don't understand Smilie

1)I don't see the print
2) what does't men for the last "1"

can you explain to me


Thanks
# 6  
Old 05-04-2012
Hi, yang lei, everything in awk has the form condition{action} . If the condition evaluates to 1 then the action is performed. If the condition is omitted then the default condition is 1, so the action is always performed. If the action is omitted then the default action is performed, which is {print $0} . In this case the condition is "1" so that evaluates to 1 and the action is omitted, therefore {print $0} is performed, which is "print the entire record".
# 7  
Old 05-04-2012
Using expr.... :-)

Code:
while read line
do
 echo "$line" "`expr "$line" : '.*'`"
done < inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to count the length of fasta sequences?

I could calculate the length of entire fasta sequences by following command, awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length($0)}END{print l}' unique.fasta But, I need to calculate the length of a particular fasta sequence specified/listed in another txt file. The results to to be... (14 Replies)
Discussion started by: dineshkumarsrk
14 Replies

2. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

3. UNIX for Dummies Questions & Answers

Count on grep for more than two values in a row of fixed length file

I want to get count on number of records in a few folders by running grep command for more than two columns in a row of fixed length file. suppose if i have a fixed length file has 5 columns and I want to see the record counts for country =can and province = bc and time stamp <= 12 feb 2013... (14 Replies)
Discussion started by: princetd001
14 Replies

4. Shell Programming and Scripting

awk length count

Morning, every one. I have a file like this: AAEQGAGNQPQH 27 AAGETQY 51 AAGGSSYNEQF 12 AAGGYEQY 72 AAGLEAKNIQY 159 AAGPYEQY 26 AAGQDYNSPLH 45 AAGQGGEQF 1587 AAGREGGNTEAF 4 AAGSPQH 3 AAGSYEQY 45 AAGTGAYEQY 19 AAGTSGNNEQF 79 AAGWNTEAF 37 I want to count the string length of the... (2 Replies)
Discussion started by: xshang
2 Replies

5. Shell Programming and Scripting

Count line Length

I am trying to write a shell scrip that can give me the line length of a record that was in EBDIC and then converted to ASCII. Everything I try reports 1 yet the length is 2000+. I have tried echo "Line length : ${#FILE}" echo "FILE" |awk -F, '{print NF}' awk '{lenth(file)}' All I can... (11 Replies)
Discussion started by: wbshrk
11 Replies

6. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

7. UNIX for Dummies Questions & Answers

length of string

Hi lets say i have a variable output="string" how can you find the length of the string contained in this variable? i guess "wc" cannot be used. its only for files. (8 Replies)
Discussion started by: silas.john
8 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. UNIX for Dummies Questions & Answers

How to count the string length

Please can anyone tell me, how to count the string length (2 Replies)
Discussion started by: Anshu
2 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question