How to do a count within a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to do a count within a file
# 1  
Old 01-12-2011
How to do a count within a file

Hi

I have a file that contains something similar to this:

Code:
 
cell id = 1A
cell id = 1B
cell id = 1C
cell id = 1D
cell id = 1E
cell NUM = 2A
cell NUM = 2B
cell NUM = 2C
cell NUM = 2D
cell id = 1A
cell id = 1B
cell id = 1C
cell id = 1D
cell id = 1E

1. Can you show me how to read through the file
2. Then search for the the word "cell id"
3. Look at the unique id next to it and add it to a counter (unique id is 1A, 1B..etc)
4. The counter will hold the amount of id's in this file which are identified by the word "cell id" preceding the id

For the above file, the counter should display 5

Thanks
# 2  
Old 01-12-2011
Code:
 
awk '/^cell id/{if(!a[$NF]) cnt++;a[$NF]++;next}END{print cnt}' inputFile


Last edited by anurag.singh; 01-12-2011 at 11:01 AM..
# 3  
Old 01-12-2011
Thanks for the prompt reply.

This is my exact script

Code:
 
#!/bin/bash
 
awk '/^cell id/{if(!a[$NF]) cnt++;a[$NF]++;next}END{print cnt}' gen_cell_orig_001.dsx
 



My actual file is this

Code:
 
<?xml version="1.0" encoding="UTF-8"?>
<!--=====================================================================================-->
<!--Datasafe Import XML -->
<!--Generated by csvdsx -->
<!--=====================================================================================-->
<Datasafe version="2.2" environment="Planned">
<Items>
<Additions>
<Cell id="1A"/>
<Cell id="1B"/>
<Cell id="1C"/>
</Additions>
<Deletions/>
</Items>
<Classifications>
<Additions>
<BTS id="1">
<Classification name="Cells">
<Cell id="1A" ref="ds:/BTS:1/Cells/Cell:1A"/>
<Cell id="1B" ref="ds:/BTS:1/Cells/Cell:1B"/>
<Cell id="1C" ref="ds:/BTS:1/Cells/Cell:1C"/>
</Classification>
</BTS>
<Additions>
<Deletions/>
</Classifications>
<Settings>
<Additions>
<Cell id="1A" ref="ds:/BTS:1/Cells/Cell:1A">
<Parameter name="Access Grant Blocks Reserved" setting="5"/>
</Cell>
<Cell id="1B" ref="ds:/BTS:1/Cells/Cell:1B">
<Parameter name="Access Grant Blocks Reserved" setting="2"/>
</Cell>
<Cell id="1C" ref="ds:/BTS:1/Cells/Cell:1C">
<Parameter name="Access Grant Blocks Reserved" setting="3"/>
</Cell>
</Additions>
<Deletions/>
</Settings>
</Datasafe>

The initial script doesn't output anything to screen

Please can you have a look for me.

Thank you

Last edited by ladyAnne; 01-12-2011 at 01:54 PM..
# 4  
Old 01-12-2011
Your initial input is different than this one.
Following shouild work:
Code:
awk -F\" '/Cell id/{if(!a[$2]) cnt++;a[$2]++;next}END{print cnt}' gen_cell_orig_001.dsx

This User Gave Thanks to anurag.singh For This Post:
# 5  
Old 01-13-2011
Thank you. That helped me big time. If I may ask please.. can you please explain to me how this code works. I appreciate all your help.
Thanks
# 6  
Old 01-13-2011
1. Set field separator as double quote i.e. " (So that $2 will be values 1A, 1B etc)
2. Check if current line has "Cell id" in it, If yes, then do following:
2.1 If $2 (id value) is not in array a (i.e. id value encountered 1st time),
increment cnt value.
2.2 Store $2 in array a (So that, cnt should not be incremented again for same id value)
3. When complete file is processed, print the cntt value.
# 7  
Old 01-19-2011
Thanks Anurag

You were a big help.

One last question - At which part are you storing $2 in the array and what does a[$2]++ do ?

Thanks Alot Anurag
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

2. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

3. UNIX for Dummies Questions & Answers

Taking word count from file and printing in file

hi, i am having a file which contains the below content, i need to take the word count of if and print the file name also inputfile.txt file_name1.txt,type_name1.txt file_name2.txt,type_name2.txt i would need the word count of the files like this if file_name*.txt then wc -l... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

4. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

5. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

6. Shell Programming and Scripting

Parse file from remote server to calculate count of string existence in that file

Hi I need to parse the file of same name which exist on different servers and calculate the count of string existed in both files. Say a file abc.log exist on 2 servers. I want to search for string "test" on both files and calculate the total count of search string's existence. For... (6 Replies)
Discussion started by: poweroflinux
6 Replies

7. Shell Programming and Scripting

appending the count of line in each file at head of each file

hello everybody, I have some files in directory.each file contain some data. my requirement is add the count of each line of file in head of each file. any advice !!!!!!!! (4 Replies)
Discussion started by: abhigrkist
4 Replies

8. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

9. UNIX for Dummies Questions & Answers

how to get a file name & record count of csv file

HI , I am new to shell scripting , I have a requirement that I send a file for data quality ( original.csv) & i will be getting 4 files daily into a particular directory in return with cleansed data . the files may be clean.csv, unclean.csv , ... (2 Replies)
Discussion started by: sirik
2 Replies

10. UNIX for Dummies Questions & Answers

How to count the record count in an EBCDIC file.

How do I get the record count in an EBCDIC file on a Linux Box. :confused: (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question