1

Could anyone explain me this below sed code

sed -n '
/Policy Name:/! d
    s/.*:\s\+//
    h
    :1
    n
    /Active:\s*no/d
    /HW\//!b1
    :2
    s/.*\s\(\S*\)\s*/\1/
    G
    s/\n/\t/p
    n
    /^\s*$\|Include:/! b2
    '

I want to edit to add "Policy Type:" information, when I replace it with "Policy Name:" it works fine, However when i add a section like below it doesn't work obviously because I am trying it without understanding.

sed -n '
/Policy Name:/! d
    s/.*:\s\+//
    h
    :1
    n
/Policy Type:/! d
    s/.*:\s\+//
    h
    :1
    n
    /Active:\s*no/d
    /HW\//!b1
    :2
    s/.*\s\(\S*\)\s*/\1/
    G
    s/\n/\t/p
    n
    /^\s*$\|Include:/! b2
    '

Also I have a solution for AIX equivalent code below from same forum same I need to understand it to edit it to add Policy Type.

# define constants
SPC=`echo x | tr x '\040'`
TAB=`echo x | tr x '\011'`
 NL=

# custom regex for...
s="[$SPC$TAB]";   # horizontal whitespace
S="[^$SPC$TAB]";  # non-whitespace

# POSIX compliant sed code...
sed -ne "
   /Policy Name:/!d

   s/.*:$s\{1,\}//
   h

   :1
      n
      /Active:$s*no/d
   /HW\//!b1

   :2
      s/.*$s\($S*\)$s*/\1/
      G
      s/\n/$TAB/p
      n
      /^$s*\$/d
      /Include:/d
   b2
"  yourfile

Input file

Policy Name:       Today

  Policy Type:       Standard
  Active:              yes
  Effective date:      01/24/2014 11:17:05
  Client Encrypt:      no
  LC/CY/Custmr:  EU         NY  Cindy
                 BU         CA  Victor
                 GU         MI  Bob
  Include:
Policy Name:       Tomorrow

  Policy Type:       Oracle
  Active:              yes
  Effective date:      01/26/2014 11:17:05
  Client Encrypt:      no
  LC/CY/Custmr:  MU         LA  Martha
                 EU         CA  Sam
  Include:
Policy Name:       Yesterday

  Policy Type:       Oracle
  Active:              no
  Effective date:      01/21/2014 11:17:05
  Client Encrypt:      no
  LC/CY/Custmr:  NV         IL  Joe

  Include:`

Desired Output

Cindy    Today     Standard
Victor   Today     Standard
Bob      Today     Standard
Martha   Tomorrow  Oracle
Sam      Tomorrow  Oracle
3

Okay, let's do it step by step:

sed -n '

The -n option makes sed output nothing unless told so

/Policy Name:/! d

All lines that do not contain Policy name: get deleted. The rest of the script is only processed in the following loops.

s/.*:\s\+//
h

This removes everything until the : and trailing spaces and places the rest in the hold buffer for later use.

:1
n

This is the beginning of a loop reading new lines

/Active:\s*no/d

Lines with that pattern are removed, so obviously there is no interest in inactives

/HW\//!b1

And now, we loop to :1 if the line contains no HW/

:2
s/.*\s\(\S*\)\s*/\1/

Beginning of the next loop, remove everything but the last sequence of non-blanks.

G
s/\n/\t/p

Then append the policy name kept in hold buffer, separated by a tab and print that line

n
/^\s*$\|Include:/! b2
'

and this gets repeated with the next lines until we reach the given pattern.

You should note that this is highly non-portable code that won't work on many sed versions.

Edit: To add the Policy Type as a third column you should add this line to the script before or after the Active: check:

/Policy Type:/{s/.*:\s*//;H;}

That is: If the line contains the said string, execute the commands between {}. Those commands remove the part up to the : and trailing blanks and append the rest of the line (which is supposed to contain the policy type) to the hold buffer. Thus, the hold buffer contains policy name and type, separated by a newline. So when we append this with G, there will be two newlines to be replaced, so the replacement command needs to get the g flag to replace all occurrences:

s/\n/\t/gp

The AIX script is mainly the same, but avoiding GNU extensions to regular expressions. Mainly the use variables to match white spaces or tabs, as \t will not work in all sed flavors, as well as + for "one or more" needs to be replaced by \{1,\}

  • Great explanation @Philippos, However /Policy [TN][ya][pm]e:/! d didn't work, previous output use to give me like below. HW Policy Name I wanted new code to give me HW Policy Name Policy Type – Sid Jun 2 '17 at 11:54
  • Then I misunderstood what you tried. Please add the relevant part of the input file to your question. – Philippos Jun 2 '17 at 11:58
  • added it in my question. – Sid Jun 2 '17 at 15:40
  • Any suggestions, anyone? – Sid Jun 4 '17 at 7:08
  • Anyone anything, i tried couple of more things still doesn't work – Sid Jun 5 '17 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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