Personal computing discussed

Moderators: renee, SecretSquirrel, notfred

 
ciro25
Gerbil In Training
Topic Author
Posts: 3
Joined: Wed Jun 27, 2007 10:58 am

shell script

Wed Jun 27, 2007 11:28 am

i ve got a file with 100 of text lines and many occurrences.
How can i search and display them all nummbered?

E,g:

1) bla bla bla
2) fortij nfhru
3)fhdgfhdiwe

P.s i do not want to get rid of them just output them and redirect them in another file.

There must be a way to do it without using sed or awk isn it?

Thnx
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Wed Jun 27, 2007 2:18 pm

Do you just need the count of how many times each line appears? In that case I think you want to do
uniq -c infile outfile
 
Corith
Gerbil
Posts: 69
Joined: Fri Feb 09, 2007 11:32 am

Wed Jun 27, 2007 2:50 pm

It sounds like you just want to print the file with line numbers? In which case I believe cat will do that.

cat -n filename


I could be wrong. I don't have access to a linux terminal right now to check.
 
SecretSquirrel
Minister of Gerbil Affairs
Posts: 2726
Joined: Tue Jan 01, 2002 7:00 pm
Location: North DFW suburb...
Contact:

Wed Jun 27, 2007 6:31 pm

If you want to find each string in the file and list them with line numbers then...

sort -u <filename> | cat -n

BTW, uniq only works on successive duplicate lines. In other words, uniq would not work for

foo
bar
foo
bar


But it would work for
foo
foo
bar
bar


Using sort with the -u flag will sort the file first so that it is in the correct order, like the second example above, then run it through unique.

You can go one step further with this.

sort <file> | uniq -c | cat -n

This would print the line number, followed by the number of occurences in the files of a particular string, followed by the string itself.

--SS
 
ciro25
Gerbil In Training
Topic Author
Posts: 3
Joined: Wed Jun 27, 2007 10:58 am

Thu Jun 28, 2007 4:28 am

Thnx guys very helpful!!!!!

Look at this please!!!

[ciro.ruggiero@unix ~]$ grep $USER xx | cat -n
1 USER=ciro.ruggiero
2 MAIL=/var/spool/mail/ciro.ruggiero
3 PATH=/u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin
4 PWD=/home/ciro.ruggiero
5 HOME=/home/ciro.ruggiero
6 LOGNAME=ciro.ruggiero

This is almost what i wanted...how do i put a bracket after the numbers?
eg:
1)
2)

also...if i want to get rid of line 3 completaly, how doi do it?
 
SecretSquirrel
Minister of Gerbil Affairs
Posts: 2726
Joined: Tue Jan 01, 2002 7:00 pm
Location: North DFW suburb...
Contact:

Thu Jun 28, 2007 7:48 am

ciro25 wrote:
Thnx guys very helpful!!!!!

Look at this please!!!

[ciro.ruggiero@unix ~]$ grep $USER xx | cat -n
1 USER=ciro.ruggiero
2 MAIL=/var/spool/mail/ciro.ruggiero
3 PATH=/u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin
4 PWD=/home/ciro.ruggiero
5 HOME=/home/ciro.ruggiero
6 LOGNAME=ciro.ruggiero

This is almost what i wanted...how do i put a bracket after the numbers?
eg:
1)
2)

also...if i want to get rid of line 3 completaly, how doi do it?


grep $USER xx | cat -n | sed "s/\(\s*[0-9]*\)\(\s*\)/\1)\2/"

As for getting ride of line three, do you want to always get rid of the third line or always get rid of PATH, which in this example happens to be line 3? Look into "grep -v".

--SS

Who is online

Users browsing this forum: No registered users and 17 guests
GZIP: On