Counting lines in each block file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting lines in each block file
# 1  
Old 12-07-2009
Counting lines in each block file

hello
im new here so i want to say hi everybody Smilie

i have to write a script and im newbie :/ i hope that in this forum are many ppl who knows subject Smilie

i have hundrets folders. in each folder is a file name trace.txt. each trace.txt has a lot of tracert's results separates with "-----" it lookes like that :
Code:
tracert results
-----
tracert results
-----
tracert results
-----

i have to count lines in each block between "-----". by this i mean counting how many points are between machines. results should be writen into file call for example count.txt with one number per line. for example
Code:
10
10
10
11
13
14
10

does anyone have idea how to do this? i think i should use awk, but awk is hard to learn and im getting lost in this subject :/ i was able to make a part of this script, but the most important part i cannot wrie. i made something like this :
Code:
#!/bin/bash
 
for node in /home/michale/results/results_10h/*
  do 
    if [ -f $node/trace.txt ]
    then
        MISSING PART :)
    else 
        echo " there is no file to analyze! "
  done

sorry for my english, im not using it every day.
# 2  
Old 12-07-2009
Try tu put this in the place of MISSING PART Smilie
Code:
while read LINE
do
    I=0
    LINE=""
    until [ "$LINE" = "-----" ]
    do
        (( I++ ))
        read LINE || break
    done
    echo $I
done < $node/trace.txt # > $outputfile # Remove first comment to output to a file

# 3  
Old 12-07-2009
Code:
 awk 'NF{c++}/^--/{print --c;c=x}' infile > outfile

# 4  
Old 12-07-2009
thank you very much for interesting !
it works great. i used danmero's solution.

Last edited by michael8484; 12-13-2009 at 07:02 PM..
# 5  
Old 12-07-2009
Quote:
Originally Posted by danmero
Code:
 awk 'NF{c++}/^--/{print --c;c=x}' infile > outfile

Danmero, I remove NF from your code, and get same output.

Code:
 awk '{c++}/^--/{print --c;c=0}'  infile > outfile

So what's the purpose to put NF in there ?
# 6  
Old 12-07-2009
Quote:
Originally Posted by rdcwayx
So what's the purpose to put NF in there ?
Don't count empty lines/records (just for safety Smilie )
# 7  
Old 12-07-2009
An alternative solution in Perl:

Code:
$
$ cat -n f4
     1  line 1
     2  line 2
     3
     4  line 3
     5  line 4
     6  ----------
     7  line 1
     8  line 2
     9  line 3
    10  ----------
    11  line 1
    12
    13
    14  line 2
    15  line 3
    16  ----------
    17
    18  line 1
    19  ----------
$
$ perl -lne 'if (/^--/){print $x; $x=0} elsif(/./){$x++}' f4
4
3
3
1
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting lines in a file using awk

I want to count lines of a file using AWK (only) and not in the END part like this awk 'END{print FNR}' because I want to use it. Does anyone know of a way? Thanks a lot. (7 Replies)
Discussion started by: guitarist684
7 Replies

2. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

3. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

4. UNIX for Dummies Questions & Answers

Counting # of lines

Counting number of lines: sp I am trying to figure out a script to count the number of text files in cywig and have it give me a number (as the answer) any help would be appreciated. I am new here, so be gentle :D (3 Replies)
Discussion started by: unicksjp
3 Replies

5. UNIX for Advanced & Expert Users

Delete # of Lines after counting

I'm trying to write a script to clean up log file automatically when it reaches 1000 lines. I can't get this to work..can some help me please :) Server: SunOS 5.8 Generic_117350-53 sun4u sparc SUNW,Netra-T12 #!/bin/ksh #file reference file=`echo "$HOME/support/moe/b.tc"` #linecount... (7 Replies)
Discussion started by: moe458
7 Replies

6. UNIX for Dummies Questions & Answers

Counting no. of lines and printing it at the start of the file

Dear users, I need to count the number of lines in a simple text file and print the number on the first line of that same file. I know I can count the lines using wc -l file.txt this gives for example 100 file.txt I need the number 100 to be printed at the very top of file.txt... (2 Replies)
Discussion started by: jenjen_mt
2 Replies

7. Shell Programming and Scripting

Counting similar lines from file UNIX

I have a file which contains data as below: nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1wqe... (6 Replies)
Discussion started by: mohsin.quazi
6 Replies

8. UNIX for Dummies Questions & Answers

Counting no of lines between two strings in a file

Hi all, I'm very very new to UNIX and AWK world.Please help me in finding a solution for my problem. I'm having a file like this ----------------------------------------------------------------- ~Version Information VERS. 2.0: CWLS log ASCII Standard -VERSION 2.0 WRAP. ... (4 Replies)
Discussion started by: santyshyam
4 Replies

9. Shell Programming and Scripting

displaying/ counting lines

I have a file called xx with the env redirected into it 5 times: env >> xx env >> xx env >> xx env >> xx env >> xx I have to read an input file (here: xx) and look for occurrences of the current user who is executing this script. Once finding an occurrence of the username I have to take that... (2 Replies)
Discussion started by: aga
2 Replies

10. UNIX for Dummies Questions & Answers

Counting The Number Of Duplicate Lines In a File

Hello. First time poster here. I have a huge file of IP numbers. I am trying to output only the class b of the IPs and rank them by most common and output the total # of duplicate class b's before the class b. An example is below: 12.107.1.1 12.107.9.54 12.108.3.89 12.109.109.4 12.109.6.3 ... (2 Replies)
Discussion started by: crunchtime
2 Replies
Login or Register to Ask a Question