Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripts for listing all the distinct characters in a text file

Tags:

bash

sed

awk

E.g.

Given a file input.txt, which has the following content:

He likes cats, really?

the output would be like:

H
e

l
i
k
s
c
a
t
,
r
l
y
?

Note the order of characters in output does not matter.

like image 650
JackWM Avatar asked Mar 20 '13 10:03

JackWM


2 Answers

How about:

echo "He likes cats, really?" | fold -w1 | sort -u
like image 51
cnicutar Avatar answered Oct 14 '22 19:10

cnicutar


One way using grep -o . to put each character on a newline and sort -u to remove duplicates:

$ grep -o . file | sort -u 

Or a solution that doesn't required sort -u or multiple commands written purely in awk:

$ awk '{for(i=1;i<=NF;i++)if(!a[$i]++)print $i}' FS="" file
like image 23
Chris Seymour Avatar answered Oct 14 '22 19:10

Chris Seymour