Put numbers after word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put numbers after word
# 1  
Old 07-07-2014
Put numbers after word

Hello
I have an file with this content
--------------------------------------------
Code:
timer
one
timer
two
timer
three
timer
four
timer
five
timer
six
timer
seven

-------------------------------------------
And I want the following output.
-------------------------------------------------
Code:
timer 1
one
timer 2
two
timer 3
three
timer 4
four
timer 5
five
timer 6
six
timer 7
seven

-----------------------------------------
So I want after the word timer(this is always the same word) a number
start with one and go up until the end of the file.
I have this code:
Code:
awk '/^timer/{$2+=1}1' file.txt > file1.txt

but this put after every timer the number 1.
Maybe someone know what I have to put inside this code?
Anyway thanks.

Last edited by Scrutinizer; 07-07-2014 at 01:45 AM.. Reason: code tags
# 2  
Old 07-07-2014
Try replacing $2+=1 with $2=++i
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-07-2014
Code:
awk '/^timer/{$(NF+1)=++n}1' file.txt > file1.txt

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 07-08-2014
Both solutions work.
Thanks
# 5  
Old 07-08-2014
Code:
awk '/^test/{$0=$0" " ++i}1' file1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match word and put data before it

Hi all I have 7 words ina file called "lookupfile" CAD CD HT RA T1D T2D BD in other file I have data like this in which columns are seaprated by comma but the names among above seven names are in one column menas comma between these seven words doesnt mean that they are separated by... (9 Replies)
Discussion started by: manigrover
9 Replies

2. Shell Programming and Scripting

help needed to put instance numbers

Hi All I need help am having a source file as below emp dept class subclass region country division first i need to get line count and i need to divide by 3 it is an parameter passing value number of lines 7 (8 Replies)
Discussion started by: ragu.selvaraj
8 Replies

3. Shell Programming and Scripting

Put double quotes around numbers

Hi, consider a file which has data such as "random text",912345,"54","finish" "random text",9991236745,"9954","finish" I want to replace the numbers that don't have double quotes around them with ones that do; so the output should be "random text","912345","54","finish" "random... (4 Replies)
Discussion started by: Storms
4 Replies

4. Shell Programming and Scripting

Put numbers against the words

Hi All, I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files. I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using sort -u from the actual .dat files.... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

5. Shell Programming and Scripting

Summing numbers after specific word

Hi all, Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated. cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf... (7 Replies)
Discussion started by: cgol
7 Replies

6. Shell Programming and Scripting

find the last word with all numbers?

Hi, I was trying to extract the last word with all numbers using awk. like the below example. I am looking for a pattern using awk. desired result: (13 Replies)
Discussion started by: hitmansilentass
13 Replies

7. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

8. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

9. Shell Programming and Scripting

extract numbers from a word

Hi ppl, I am a bit lost on this...can some one assist. I know this can be down with awk or sed, but i cant get the exact syntax right. I need to only extract the numbers from a signle word ( eg abcd.123.xyz ) How can i extract 123 only ? Thanks (14 Replies)
Discussion started by: systemali
14 Replies

10. Shell Programming and Scripting

put each word in new line - sed or tr

Hello ! I have a result of ls command in a file: file1 file2 file3.out file4.pdf file5 they all are separated by space. I need to put them on a separate line example: file1 file2 file3.out file4.pdf fil35 i tried sed 's/ /\n/g' inputfile > outputfile but did not help (3 Replies)
Discussion started by: hemangjani
3 Replies
Login or Register to Ask a Question