awk command to find total number of Special character in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to find total number of Special character in a column
# 1  
Old 11-18-2014
awk command to find total number of Special character in a column

How to find total number of special character in a column?
I am using
Code:
 awk -f "," '$col_number "[^0-9]*$" {print $col_number}' file.csv|wc -l

but its not giving correct output. It's giving output as 1 even though i give no special character?




Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 11-18-2014 at 12:14 PM..
# 2  
Old 11-18-2014
What is a "special character"?

What is $col_number?
# 3  
Old 11-18-2014
Hi,
$col_number can be anything say 11 or 13. I need the total count of all the special character that can appear in a string. Say, for example in Column 12 i have entries 123.345%65_34,0.345-987,567$9098. I know i can use file separator (awk -F "|" $12(col_number) "[^0-9]*$" {print $col_number}' file.csv|wc -l) any other special character appeneded to file separator. My requirement is to get total number of special character appearing in $col_number say column 12. How can i do it using awk command.
Also, how to get total count alpha numeric in a column say 0.3456wef54,983.rty,trew34.456.

Thanks,
Ajit
# 4  
Old 11-18-2014
So, $col_number is a shell variable? That won't work in awk, awk is not shell. None of your awk code appears like it would work at all.

And a "special" character is any character that's not a number? Okay.

wc -l does not give you the count of characters, it gives you the count of lines. We don't need it here in any case. Strip all non 0-9 characters out of the column with gsub, then print the length of the column. To get alphanumeric, strip out all non-alphanumeric and get the length of that. etc.

Code:
awk -F, '$(COL) ~ /[^0-9]/ { gsub(/[0-9]/, "", $(COL));  print length($(COL)) }' COL=12 filename

or perhaps

Code:
awk -F, '$(COL) ~ /[^0-9]/ { gsub(/[0-9]/, "", $(COL));  print length($(COL)) }' COL="$col_number" filename


Last edited by Corona688; 11-18-2014 at 01:46 PM..
# 5  
Old 11-18-2014
The more painstakingness you apply phrasing your request, the better the answers/solutions you get. It always helps to supply sample date, both in and out.
Me, I am not sure I understood what you really need. You did not, for example, tell us what you consider "special characters", although asked to do so.

Quote:
Also, how to get total count alpha numeric in a column say 0.3456wef54,983.rty,trew34.456.
Try
Code:
awk '{print gsub(/[0-9a-zA-Z]/, "&"), length}' file
25 32

This is for $0, the entire line. Of its 32 chars, 25 are alphanumeric. Apply it to one single field if you want to narrow it down.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or other way to find out number of occurrence of 7th character

Hi all, I am looking for to filter out based on 7th character and list the number of occurrence based on the 7th character if p , d , o or m 1. if 7th character is p , Output should be: p_hosts = N 2. if 7th character is d , Output should be: d_hosts = N 3. if 7th character is o , Output... (10 Replies)
Discussion started by: rveri
10 Replies

2. Shell Programming and Scripting

Text to column starting/ending with special character in each row

Hello, Here is my text data excerpted from the webpage: input My target is to get: What i tried is: sed 's/.*\(connector\)/1/' input > output but all characters coming before the word "connector" are deleted which is not good for me. My question: (9 Replies)
Discussion started by: baris35
9 Replies

3. UNIX for Dummies Questions & Answers

How to find count total number of pattern in a file …?

How to find count total number of pattern in a file … File contains : a.txt ------------- aaa bbb nnn ccc aaa bbb aaa ddd aaa aaa aaa aaa grep -c aaa a.txt Op: 4 ( But my requirement is should count the total no of patterns as 7 ) (4 Replies)
Discussion started by: Jitten
4 Replies

4. Shell Programming and Scripting

awk - special character not working

Hi, I am trying to use forward slash in awk. While running the command through console, its working fine but not working while running it through shell script. find . -name "*" -ctime -6 | xargs cat | grep -E -v ^fileName\|^\(\) | awk -v DATE="${CURR_DATE}" -v DATE_LOG="$DATE_SYS" ' ... (4 Replies)
Discussion started by: abhi1988sri
4 Replies

5. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

6. UNIX for Advanced & Expert Users

awk command in special character

Hi, I want to add below line after end of a file (i.e file1) &&echo "copy done" >> out.txt cat file1 scp user1@server1:/tmp/dir /tmp/dir1 my requirment is cat file1 scp user1@server1:/tmp/dir /tmp/dir1 &&echo "copy done" >> out.txt could any one please help me (7 Replies)
Discussion started by: anshu ranjan
7 Replies

7. Shell Programming and Scripting

remove special character from a specific column

Hello , i have a text file like this : A123 c12AB c32DD aaaa B123 23DS 12QW bbbb C123 2GR 3RG cccccc i want to remove the numbers from second and third column only. i tried this : perl -pe 's///g' file.txt > newfile.txt but it will remove the number from... (7 Replies)
Discussion started by: shelladdict
7 Replies

8. UNIX for Dummies Questions & Answers

AWK Command to find text in specific column

I'm new to scripting and would appreciate any help. I have a list of over 20 words in File1 that I need to find in columns 10-15 of File2. I need the entire row of File2 that the File1 list matches. I originally used a grep command which works, but provides File1 results that can be found... (3 Replies)
Discussion started by: Chillin
3 Replies

9. Ubuntu

Inserting a header with column number to a 1.6 GB file with special spacing

Hi; I've been searching posts to find a solution to what I'm trying to do, but I've have NOT found anything yet. I have a file (file1) with 300K columns and 1411 rows, the columns don't have a column no. header (No header at all) and I'm trying to fetch the information from specific columns.... (3 Replies)
Discussion started by: sogi
3 Replies

10. Shell Programming and Scripting

Finding the total of a column using awk

Here is my file name countries USSR 8650 262 Asia Canada 3852 24 North America China 3692 866 Asia USA 3615 219 North America Brazil 3286 116 South America India 1269 637 Asia Argentina 1072 ... (8 Replies)
Discussion started by: ironhead3fan
8 Replies
Login or Register to Ask a Question