Operating System - HP-UX
1752544 Members
5149 Online
108788 Solutions
New Discussion юеВ

Text filtering and formatting .....

 
SOLVED
Go to solution
Karthik_2
Regular Advisor

Text filtering and formatting .....

1.How do I remove duplicate lines in a
text file after sorting .
ex:
1111
2222
2222
3333
3333
I need to have only
1111
2222
3333
2.How do I sort lines in a file based on the
length of the line.
ex:
234
eqwr
45678
dfghhjk

cheers
Karthik ...
Its ALL in the MATRIX
4 REPLIES 4
Herve BRANGIER
Respected Contributor

Re: Text filtering and formatting .....

Hi

For you first question use "uniq" command
(man uniq). For example :

uniq foo.txt | sort

HTH

Herv?

Andreas Voss
Honored Contributor
Solution

Re: Text filtering and formatting .....

Hi,

with 'sort -u' you can do sort and uniq in one command.

For sorting length you can use this

cat | awk '{printf("%9d %s\n",length($0),$0);}' | sort -n | cut -c11-

Regards
Herve BRANGIER
Respected Contributor

Re: Text filtering and formatting .....


Another way for the first question :

sort -u foo.txt

(-u : like uniq command)

For the second question try something like :

line_size.sh :

#!/bin/sh
for line in `sort -u $1`
do
echo `echo $line | wc -c` $line
done

And after use :

line_size.sh foo.txt | sort -k 1,1n | cut -f2 -d' '

HTH

Herv?





Sridhar Bhaskarla
Honored Contributor

Re: Text filtering and formatting .....

This is the way I handle this.

Cat text1 > text
cat text1 >> text
sort text |uniq -d

Try this cheap method out.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try