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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or other way to find out number of occurrence of 7th character
# 1  
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 should be: o_hosts = N
4. if 7th character is m, Output should be: m_hosts = N

Code:
prm1hcppb240
prm1hcppb220
prm1hcppb212
prm1hcppb211
prm1hcppb410
cmi1hcmpb282
cmi1hcmpb033
cmi1hcmpb230
prm1hcppb022
prm1hcpmp203
prm1hcppb303
prm1hcppb290
prm1hcppb250
prm1hcppb241
prm1hcppb352
cmi1hcopb450
cmi1hcmmp002
prm1hcpbk302
prm1hcpbk001
cmi1hcmbk203
cmi1hcpbk201
cmi1hvmmp011
cmi1hvmmp101
cmi1hvppb318
cmi1hcmpb502
brc1scpdb0112
brc1scddb0122


Desired output should be:
Code:
p_hosts = N
d_hosts = N
o_hosts = N
m_hosts = N

Thanks in advance.,

---------- Post updated at 10:56 AM ---------- Previous update was at 10:51 AM ----------

What I have tried :
Code:
substr($1,7,1)

but could not filter out..,
# 2  
I'm pretty sure that's only a snippet from your attempt. Post it in its entirety.

What if that char is NOT p , d , o , or m ?
This User Gave Thanks to RudiC For This Post:
# 3  
Maybe something like:
Code:
awk '
{	h[substr($1, 7, 1)]++
}
END {	for(i in h)
		printf("%s_hosts = %d\n", i, h[i])
}' file

producing the output:
Code:
d_hosts = 1
m_hosts = 8
o_hosts = 1
p_hosts = 17

with your sample input (although the order of the output lines may vary depending on the version of awk you use).

If you are trying this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Code:
awk '
{$0 ~ /^......[dpom]/ ? a[substr($0, 7, 1)]++ : x++}
END {for (i in a) print i "_hosts = " a[i]; print "Non_p_d_o_m_hosts = " x++} ' infile


Last edited by rdrtx1; 02-03-2017 at 05:12 PM..
# 5  
Code:
awk ' /^......[pdmo]/ {a[substr($1,7,1)]++} END {for (i in a) print i "_hosts = " a[i]}' infile

# 6  
Moderator's Comments:
Mod Comment Please note that rveri only supplied a partial answer to the question about what code had been attempted by the submitter. Until that question is answered in full, please refrain from posting solutions. Once rveri does respond, suggestions posted by Don Cragun and rdrtx1 will be unhidden.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Similar to what has been posted so far but allowing to count occurrences of undesired characters:
Code:
awk '
        {TMP = substr ($0, 7, 1)
         if (TMP ~ "[" SRCH "]") C[TMP"_hosts"]++
         else                    C["None   "]++
        }
END     {for (c in C) print c, "=", C[c]
        }
' SRCH="pdo" file

d_hosts = 1
None    = 8
p_hosts = 17
o_hosts = 1


Last edited by RudiC; 02-03-2017 at 05:02 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #368
Difficulty: Medium
The Open Group released the Single UNIX Specification Version 2 in 1987.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

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 awk -f "," '$col_number "*$" {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? Please use code tags next time for your code and... (4 Replies)
Discussion started by: AjitKumar
4 Replies

3. 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

4. Shell Programming and Scripting

awk to find the number of occurrence

My file contains like this on 10 th line NM1*IL*1* awk '/NM1/{print NR}' *.dat output is 10 awk '/NM1*IL*1*/{print NR}' *.dat output is Nothing but im expecting 10 on second code as well . (4 Replies)
Discussion started by: Rajesh_us
4 Replies

5. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

6. UNIX for Dummies Questions & Answers

Find certain number of character in vi

Hello, Experts, I have a file with the first and second column connected together, and i want to use vi to seperate them (put a space in between). Is there any command in vi would put a space after the 7th letter? Thanks! example: 0.981101.517 2.944101.517 4.907101.517 (10 Replies)
Discussion started by: wingsy1212
10 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

9. Shell Programming and Scripting

Get rid of the 7th character of each line if this is a space

I have a text file like this ... B 16 1.340E+05 A 18 3.083E+02 Wu123 1.365E+02 ... I would like to get rid of the 7th character of each line if this is a space character. Thank you, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

10. UNIX for Dummies Questions & Answers

optimizing - to find the number of occurrence

Hi, I need to find the number of occurrence of string in a file, for ex: >cat filename abc abc def ghi ghi ghi ghi abc abc >output would be abc 4 def 1 (10 Replies)
Discussion started by: matrixmadhan
10 Replies

Featured Tech Videos