Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

character count 1

Status
Not open for further replies.

demis001

Programmer
Aug 18, 2008
94
US
Is there any simple single line command to count character in awk? I want some thing like =len(A1) in excel.

input data:
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA


I need output like this
AAAAGCTGGGTTGAGAGGGCGA 18
AAAAGCTGGGTTGAGAGGGCGA 18
so on....

Then I want to print $1 if $2>15

Thanks as usual,
Dereje
 
awk 'length($0)>15' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Before that how should I count the character?

What I have is this data:
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA
AAAAGCTGGGTTGAGAGGGCGA

I want first count and put total count of $1 in $2
Like this

AAAAGCTGGGTTGAGAGGGCGA 18
AAAAGCTGGGTTGAGAGGGCGA 18

Dereje
 
The following didn't work:
awk '{print $0}{print "\t"}{print length($0)}'
 
awk '{printf "%s\t%d",$0,length($0)}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It didn't work

The output looks like this
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M
^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M 2A^M

Don't know what it is

Command used:
awk '{printf "%s\t%d",$0,length($0)}' test.txt

Thanks
 
OOps, sorry for the typo:
awk '{printf "%s\t%d\n",$0,length($0)}' /path/to/input

another way:
awk '{print $0"\t"length($0)}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
when I print the result to the shell. It will not output indvidual line. Rather, it print ontop of it

Result looks like this
A.......42AAAAGCTGGGTTGAGTGCTCGGTTCGCATAACGTGTCTTGC
 
Did you try my suggestions stamped 4 Sep 08 11:20 ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It is really wierd to see that. For shorter line it works fine for longer it both gives the result looks like this

AAAAGCTG23TTGAGAGGGCGA

The max lenght of the line is 42

I have observed the presence of ^M character at the end of each line and handle it as follows
sed 's/^M//g' test_sorted.txt | awk '{print $0"\t"length($0)}'

Thanks PHV
 
The sed part also didn't work: The output looks like
A......................^M 24
A......................^M 24
A......................^M 24
A......................^M 24
A......................^M 24
A......................^M 24
A......................^M 24
AAAAGAAA42AATCATTAGATTATTAATTTTTATTAAAAAT
AAAAGAAA42AGAAAAAAAAAAAAAAAAAAAAAGAAGAAAA
AAAAGAAA42AGAAAAAAAAAAAAAAAAAAGAAAAAAAAAA
AAAAGAAA42AGAAAAAAAAAAAAAAAAAGAAAAAAAAAAA
AAAAGAAA42AGAAAAAAAAAAAAAAAAGAAAAAAAAAAAA
AAAAGAAA42AGAAAAAAAAAAAAAAAAGAAGACATAAGAA
AAAAGAAA42AGAAAAAAAAAAAGAAAAAAAGAAAAAAGAA
AAAAGAAA42AGAAAAAGAAAAAAAAAAAAAAAAAGAAAAA
Dereje
 
Perhaps this ?
awk '{gsub(/\r/,"");printf "%s\t%d\n",$0,length($0)}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you for introducing me to gsub!! It do the job!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top