awk line instance counter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk line instance counter
# 1  
Old 06-17-2015
awk line instance counter

I Have a text file with several thousand lines of text.
Occasionally there will be a "sysAlive" line of text (every so often)
What would be an awk command to print every line of text, and to put in incrementing counter ONLY on the "sysAlive" lines

For example:
Code:
>cat file.txt
lineAAA a b c d x
lineBBB CCC DDD
lineEEE XYZ
this line contains the word sysALive
lineFFF aaa bbb ccc
lineGGG nnn nnn jjj
this line contains the word sysALive
lineHHH III J k L M n
line OOOPPPOOO
this line contains the word sysAlive
line thats enuff

I want this to look like:
Code:
>cat file2.txt
lineAAA a b c d x
lineBBB CCC DDD
lineEEE XYZ
this line contains the word sysALive 1
lineFFF aaa bbb ccc
lineGGG nnn nnn jjj
this line contains the word sysALive 2
lineHHH III J k L M n
line OOOPPPOOO
this line contains the word sysAlive 3
line thats enuff


The keyword (sysAlive) is always in the same field.
something like
Code:
awk print$0, and if $6="sysAlive" print $0(i) and increment i+1

Sorry for the crude example.
Any help is greately appreciated,
take care
# 2  
Old 06-17-2015
Hello ajp7701,

I can see you said you are looking for exact string sysALive, but you showed in output as 3 when it comes sysAlive, so keeping this in mind and considering that it is not a typo. Following may help you then.
Code:
 awk '($0 ~ /sysA[lL]ive/){A++;print $0 OFS A;next} 1' Input_file

Output will be as follows.
Code:
lineAAA a b c d x
lineBBB CCC DDD
lineEEE XYZ
this line contains the word sysALive 1
lineFFF aaa bbb ccc
lineGGG nnn nnn jjj
this line contains the word sysALive 2
lineHHH III J k L M n
line OOOPPPOOO
this line contains the word sysAlive 3
line thats enuff

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-17-2015
yes that is 3. I cut and pasted, I dont know how that happened. ?? SRRY Smilie
# 4  
Old 06-17-2015
Since you want to count both sysAlive and sysALive, I assume you want a case insensitive match on the entire word. The following will count that string no matter which field it is in as long as it is a standalone word:
Code:
awk '/(^|[[:space:]])[sS][yY][sS][aA][lL][iI][vV][eE]([[:space:]]|$)/{print $0,++cnt;next}1' file.txt > file2.txt

If you just want the 6th field or just want the last field, you can simplify it a little bit, but you can still use the same general structure.
# 5  
Old 06-18-2015
Code:
awk '{printf("%s%s\n",$0,tolower($NF)=="sysalive"?" "++c:"")}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on Adding one counter loop at the end of each line in a file

Hello All, I have file a.txt I want to add a counter loop at the end of each line in a file ill explain: i have a site h**p://test.test=Elite#1 i want to add a a counter to the number at the end of the file, that it will be like this urlLink//test.test=Elite#1 urlLink//test.test=Elite#2... (3 Replies)
Discussion started by: nexsus
3 Replies

2. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

3. UNIX for Dummies Questions & Answers

awk repeats counter

if I wanted to know if the word DOG(followed by several random numbers) appears in col 1, how many times will that same word DOG* appeared in col 2? This is a very large file Thanks! (7 Replies)
Discussion started by: verse123
7 Replies

4. Shell Programming and Scripting

AWK counter problem

Hi I have a file like below ############################################ # ParentFolder Flag SubFolders Colateral 1 Source1/Checksum CVA 1 Source1/Checksum Flexing 1 VaR/Checksum Flexing 1 SVaR/Checksum FX 1 ... (5 Replies)
Discussion started by: manas_ranjan
5 Replies

5. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

6. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

7. Shell Programming and Scripting

Append instance count to each line

Hello forum, I need help with a script for displaying the number of instances/times a particular line appears in a tab-delimited file and append that number to the end of the line. Example text file: aaa bbb ccc ddd ggg hhh kkk nnn aaa bbb ccc ddd aaa bbb ccc ddd ppp qqq nnn sss ggg... (1 Reply)
Discussion started by: jaysean
1 Replies

8. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

Say I have (in psuedocode) For i=1 to 10 tar cvfb /... 5*i /junk(i) end What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Discussion started by: jeriryan87
2 Replies

9. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

10. Programming

WRT counter show me that line from a txt file

Hello Experts, I am beginner to C language.. I know I am making a simple mistake but I dont know what the problem here #include <stdlib.h> #include <stdio.h> int main(void) { FILE* fh; char s; int i; int count =10; double a,b; printf("COUNT=... (6 Replies)
Discussion started by: user_prady
6 Replies
Login or Register to Ask a Question