Count lines separated by new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count lines separated by new line
# 8  
Old 04-06-2013
Quote:
Originally Posted by Yoda
It will produce an output as per your requirement.

Input file:
Code:
$ cat file
emcpower28a
pci@3,03 (disk physical name)
pci@3,04

emcpower9a
pci@1,03
pci@2,03
pci@3,01
pci@4,03

Output:
Code:
$ awk '/^emc/{p=$0}!/^emc/&&NF{A[p]++}END{for(c in A) print c,A[c]}' OFS=" - " file
emcpower9a - 4
emcpower28a - 2

Well, it didn't give expected output.

Let me show you what I did.

File has records like this

Code:
Pseudo name=emcpower0a
3076 pci@2,600000/SUNW,emlxs@0/fp@0,0 c1t5Fd94s0 FA 13cA   active  alive      0      0
3075 pci@12,600000/SUNW,emlxs@0/fp@0,0 c3t5Dd94s0 FA  4cA   active  alive      0      0

Pseudo name=emcpower8a
3076 pci@2,600000/SUNW,emlxs@0/fp@0,0 c1t5Cd95s0 FA 13cA   active  alive      0      0
3075 pci@12,600000/SUNW,emlxs@0/fp@0,0 c3t53d95s0 FA  4cA   active  alive      0      0
3073 pci@12,600000/SUNW,emlxs@0/fp@0,0 c3t54d95s0 FA  4cA   active  alive      0      0

Pseudo name=emcpower15a
3076 pci@2,600000/SUNW,emlxs@0/fp@0,0 c1t57d165s0 FA 13cA   active  alive      0      0
3075 pci@12,600000/SUNW,emlxs@0/fp@0,0 c3t52d165s0 FA  4cA   active  alive      0      0

I fired below awk command and got some error
Code:
awk '/^emc/{p=$0}!/^emc/&&NF{A[p]++}END{for(c in A) print c,A[c]}' OFS=" - " /tmp/dev1
awk: syntax error near line 1
awk: bailing out near line 1

So I used nawk which ran successfully however didn't produce expected output.

Code:
nawk '/^emc/{p=$0}!/^emc/&&NF{A[p]++}END{for(c in A) print c,A[c]}' OFS=" - " /tmp/dev1
 - 696

---------- Post updated at 08:43 AM ---------- Previous update was at 08:40 AM ----------

Based on above reply I want output something like

Code:
emcpower0a - 2
emcpower8a - 3
emcpower15a - 2

# 9  
Old 04-06-2013
Quote:
Originally Posted by prashant2507198
Well, it didn't give expected output.
Do you expect this code to work when you change the file format?

Read it carefully and you will understand why it didn't work.

Here is something that will work for your current input:
Code:
nawk -F'=' '
                /emcpower/ {
                                d = $2
                }
                !/emcpower/ && NF {
                                A[d]++
                }
                END {
                        for ( c in A )
                                print c, A[c]
                }
' OFS=' - ' file

Please make sure that you post representative samples that are similar to your original input file data in future, otherwise it is a waste of time for you and people who are trying to help...
# 10  
Old 04-06-2013
Quote:
Originally Posted by Yoda
Do you expect this code to work when you change the file format?

Read it carefully and you will understand why it didn't work.

Here is something that will work for your current input:
Code:
nawk -F'=' '
                /emcpower/ {
                                d = $2
                }
                !/emcpower/ && NF {
                                A[d]++
                }
                END {
                        for ( c in A )
                                print c, A[c]
                }
' OFS=' - ' file

Please make sure that you post representative samples that are similar to your original input file data in future, otherwise it is a waste of time for you and people who are trying to help...
WOW. It worked like butter. Can you please help me in understanding components of this command or any documentation about nawk?
# 11  
Old 04-06-2013
Here is what the code does:
Code:
awk -F'=' '                                     # Set = as field separator
                /emcpower/ {                    # Search for pattern: emcpower
                                d = $2          # If found, assign d to 2nd field (disk name)
                }
                !/emcpower/ && NF {             # Search for not pattern: emcpower and NF>=1
                                A[d]++          # Create and increment array indexed by disk name
                }
                END {                           # END block
                        for ( c in A )          # For each element in array
                                print c, A[c]   # Print disk name and count
                }
' OFS=' - ' file                                # Set ' - ' as output field separator

Read the nawk manual for further reference: man nawk
# 12  
Old 04-06-2013
Quote:
Originally Posted by Yoda
Here is what the code does:
Code:
awk -F'=' '                                     # Set = as field separator
                /emcpower/ {                    # Search for pattern: emcpower
                                d = $2          # If found, assign d to 2nd field (disk name)
                }
                !/emcpower/ && NF {             # Search for not pattern: emcpower and NF>=1
                                A[d]++          # Create and increment array indexed by disk name
                }
                END {                           # END block
                        for ( c in A )          # For each element in array
                                print c, A[c]   # Print disk name and count
                }
' OFS=' - ' file                                # Set ' - ' as output field separator

Read the nawk manual for further reference: man nawk
AWESOME. I have found a book on awk which I will read later but for now I needed an urgent solution so I had posted here.

Can you please help me with this too? It would be really great great help for me.

File like this

Code:
        /dev/rdsk/c20t638d0s2
                Total Path Count: 6
                Operational Path Count: 6
       /dev/rdsk/c20t630d0s2
                Total Path Count: 6
                Operational Path Count: 6
        /dev/rdsk/c20t639d0s2
                Total Path Count: 6
                Operational Path Count: 6

I want output only

Code:
/dev/rdsk/c20t639d0s2 - 6 (total path count numbers)

# 13  
Old 04-06-2013
Given your file above, and hoping you're NOT changing the file format, this should do the job for you:
Code:
$ awk '/\/dev\// {TMP=$0; getline; print TMP " - " $NF}' file
        /dev/rdsk/c20t638d0s2 - 6
       /dev/rdsk/c20t630d0s2 - 6
        /dev/rdsk/c20t639d0s2 - 6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

2. Shell Programming and Scripting

Want to count the number of lines after the first line

hi, How can i count the number of lines after the first line in a flat file in unix? Say i have a flat file with a header like: Student Name Student ID .... Tnx (7 Replies)
Discussion started by: reignangel2003
7 Replies

3. Shell Programming and Scripting

Need to concatenate spuriously separated lines

Given the pattern below: 3113296571|NULL|NULL|NULL||N| 1| 0| 926667| 1001036| 0| 3076120438|NULL|NULL|NULL|NULL|DUE FOR NEW CONSENT!|N|NULL| 10198318|2011-07-25-12.34.02.786000|NULL|NULL|NULL| 0 3113336478|NULL|NULL|NULL||N| 1| ... (16 Replies)
Discussion started by: lemele
16 Replies

4. Shell Programming and Scripting

Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4... (6 Replies)
Discussion started by: darbs121
6 Replies

5. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

6. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

7. Shell Programming and Scripting

use shellscript to find the count of a line in a set of lines

I have a file a.xml some portion of the file is given below.But the file format is same. CTYPE available_templates SYSTEM './available_templates.dtd'> <available_templates> <template_file name="Approve External" path="core/approve/bin" <command_list> <command... (1 Reply)
Discussion started by: millan
1 Replies

8. Shell Programming and Scripting

Separate lines in a single '|' separated line

Hi I have a file with contents like china india france japan italy germany . . . . etc.... I want the output as china|india|france|japan|italy|germany|.|.|. (3 Replies)
Discussion started by: hidnana
3 Replies

9. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies

10. Shell Programming and Scripting

two lines into one colon separated line...

Does anyone know how to get these two output lines into one colon ':' separated line with some unix command? Maybe nawk. I've tried to read the nawk and awk man pages but I don't get it right. Are these commands the one to use? Output from find command: # /sw/tools/matlab/7.0.1/man... (2 Replies)
Discussion started by: tonlu
2 Replies
Login or Register to Ask a Question