Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the data in my file as

START-OF-FIELDS
ID
NAME
DEPT
END-OF-FIELDS

START-OF-DATA
1|joy|cs
2|sam|ec
END-OF-DATA

START-OF-FIELDS
ID 
NAME
DOB
DEPT
ADDRESS
END-OF-FIELDS

START-OF-DATA
5|joe|13/2/2001/|ee|street1
3|gwen|4/08/1999|cs|street2
END-OF-DATA

EXPECTED OUTPUT:-

ID
NAME
DEPT

Count for data set 1 is: 3

ID 
NAME
DOB
DEPT
ADDRESS

Count for data set 2 is: 5 and so on for dynamic no. of data sets.

I want count of data between both START-OF-FIELDS & END-OF-FIELDS & for START-OF-DATA & END-OF-DATA. can any body give me exact code to do this. I have used

sed -n '/^START-OF-FIELDS/,/END-OF-FIELDS/{s/^START-OF-FIELDS//;/^END-OF-FIELDS/d;p;}' Sourcefile.txt > START_OF_FIELDS.TXT

wc -l START_OF_FIELDS.TXT

But I am not getting desired rsult.

share|improve this question
1  
Please also add what you want in output.. –  Rahul Patil Mar 19 '13 at 9:43
add comment

3 Answers

Your problem needs to be better defined, but looking at your sample input I am making a guess as to what you want to do. Use a small AWK script like this:

#!/bin/bash

awk '! /START-OF-/ {
  if (MODE=="DEFINE") { HEADER=HEADER $0 "|" }
  else if ((MODE=="DATA") && (NF>0)) { print }
}
 /START-OF-FIELDS/ { MODE="DEFINE"; HEADER="" }
 /START-OF-DATA/ {
      print HEADER
      MODE="DATA"
      HEADER=""
     }' | sed 's/|$//'

This produces:

ID|NAME|DEPT
1|joy|cs
2|sam|ec
ID |NAME|DOB|DEPT|ADDRESS
5|joe|13/2/2001/|ee|street1
3|gwen|4/08/1999|cs|street2

Note: I am lazy, so I add a vertical-bar after every field in the header. Then I use the SED command to remove the trailing V-bars afterwards.

share|improve this answer
add comment
sed -n '/^START-OF-DATA$/,/^$/{/^START-OF-DATA$/d;/^$/d;p;}' filename

That is:

  • Collect the lines between START-OF-DATA and the next empty line - /^START-OF-DATA$/,/^$/. With those lines:
    • Delete the delimiter lines - /^START-OF-DATA$/d and /^$/d
    • Print the rest - p
share|improve this answer
add comment

Given the new better problem description, this is actually easier

#!/bin/bash
awk 'BEGIN {
            SETNR=0; 
            MODE="Non-Fields"}
(MODE=="Fields") && (! /START-OF-/ ) && (! /END-OF/ ) {
            print; 
            COUNT++
           }
/START-OF-FIELDS/ {
            COUNT=0; 
            SETNR++; 
            MODE="Fields"
           }
/END-OF/ {
            if (MODE=="Fields") {
                 printf ("Count for data set %d is: %d\n", SETNR, COUNT)
                };
            MODE="Non-Fields"
         }' $*
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.