简体   繁体   中英

grep command to find out how many times any character is followed by '.'

I have to find out how often any character is followed by a period (.) with the help of grep. After finding how many times character is followed by period and then I have to sort the result in ascending order.

For example in this string: "Find my input. Output should be obtained. You need to find output."

The output should be something like this:

d 1 
t 2

What I have done so far :

cat filename | grep -o "*." | sort -u 

But it is not working as intended.

Any ideas how to solve this? I have to perform this operation on huge library of books in .txt files.

An iterative approach with GNU grep:

grep -o '.\.' filename | sort | uniq -c

Output:

1 d.
          2 t.

grep -Po '.(?=\.)' filename | sort | uniq -c

Output:

1 d
          2 t

grep -Po '.(?=\.)' filename | sort | uniq -c | awk '{print $2,$1}'

Output:

d 1
t 2

With single GNU awk process:

awk -v FPAT='.[.]' 'BEGIN{ PROCINFO["sorted_in"]="@ind_str_asc" }
       { for(i=1;i<=NF;i++) a[substr($i,1,1)]++ }
       END{ for(i in a) print i,a[i] }' filename

The output:

d 1
t 2

这个也好

echo "Find my input. Output should be obtained. You need to find output."| grep -o ".\." | sort | uniq -c | rev | tr -d .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM