Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I find and many words of a file with the Linux shell?

I have

text
example

text
example

text
example

text
example

And I need

text
example1

text
Example2

text
example3

text
example4

That is, the word is located and then numbers.

share|improve this question
5  
I read this quickly, posted, and then deleted my answer. Now that I'm carefully reading I don't think I understand what you are asking for. Can you clarify? –  unclejamil Sep 17 '11 at 6:14
    
I edited the question, and found that the in the original question the words text and example* where on two different line, shown on a single line because of poor use of markdown. –  enzotib Sep 17 '11 at 7:51
    
Even after your edits, it's really hard to understand exactly what you need. Maybe you could add a mock up of the output you're hoping to get? –  Tim Post Sep 17 '11 at 13:20
add comment

2 Answers

up vote 5 down vote accepted

I don't completely understand your question. However provided your input example and your output example you can do something like this (just an orientation):

Your input file named "example" is as follows:

text
example

text
example

text
example

text
example

So provided that "text" can be any thing in a line, and "example" is just "example" (edit my code to fit your needs); you can match your output requirements with the following statement (not too much exhaustive however):

hmontoliu@ulises:/tmp$ awk 'BEGIN {c=0} {if ( $0 ~ /example/ ) {c+=1; print $0c} else print $0}'   example 
text
example1

text
example2

text
example3

text
example4

HTH

share|improve this answer
    
Thank you guys. –  Regis da Silva Sep 18 '11 at 3:17
add comment

As I understand from example you want to print lines and after that line number.

For printing line number and then lines you can use cat or nl:

$ cat -n test.txt 
     1  text example
     2  text example
     3  text example
     4  text example

$ nl test.txt 
     1  text example
     2  text example
     3  text example
     4  text example

But there is no possibility to print numbers after lines in cat and nl so you can use awk:

$ awk '{print $0 "\t" NR}' test.txt 
text example    1
text example    2
text example    3
text example    4
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.