▶ Bash Text manipulation: sed is better then a grepped cat !

When you like to learn about regex then sed is a good starting point.

Here is an example of some text manipulation.

See below snippets of a txt file which needs these adjustments

cat -n <file>.pp | grep notify
33  #                notify {“WARNING: numberoforasids:  $numberoforasids” : }
46                           notify => Service[‘postfix’],
71                          notify => Service[‘postfix’],
280                                   notify   => Exec[‘mkswap_on_lvswap’],
338                  notify {“current_pvs is $current_pvs” : }
355                      notify {“pvsarray is $pvsarray” : }
356                  notify {“number of pvs is $numberofpvs” : }
359                      notify {“vgpvs is $vgpvs” : }
360                  notify {“allpvs is $allpvs” : }
367                      notify {“current_pvs_string is $current_pvs_string” : }
421                      notify {‘setup /oracle, /oracle/client and /usr/sap/SMD’: }
562                      notify {‘setup /usr/sap/<SID>/global/trex/data’: }
640                          notify {‘setup /usr/sap/<SID>/global/trex/data/snapshots’: }
662                      notify {‘setup /home/<SID>adm and /inst<SID>’: }
701                      notify {‘setup /SAP/<SID> /sapmnt/<SID> /usr/sap/trans’: }
754                      notify {‘create user <sid2>adm and setup /usr/sap/<SID2> /home/<sid2>adm /sapmnt/<SID2>’: }
771                          notify {“SID2 settings: $current_sid2_small $current_sid2_big”: }
821                      notify {‘setup /usr/sap/<SID>’: }
1056                          notify {“sapdatafs_last $sapdatafs_last “: }
1057                          notify {“sapdatafs: $sapdatafs “: }
1058  #                        notify {“sapdatafs_testb: $sapdatafs_testb “: }
1059  #                        notify {“sapdatafs_testc: $sapdatafs_testc “: }
1060                          notify {“last_req_sapdatafs : $last_req_sapdatafs “: }

1. All uncommented notify statements have to be commented out.

sed -i ‘/^#/!s/notify/# notify/’ <file>.pp

2. From the result of step 1 , delete leading whitespace (spaces, tabs) from front of each line

 sed -i ‘/# notify/s/^[ \t]*//’ <file>.pp


How does it look now ?

33  #                notify {“WARNING: numberoforasids:  $numberoforasids” : }
46  # notify => Service[‘postfix’],
71  # notify => Service[‘postfix’],
280  # notify   => Exec[‘mkswap_on_lvswap’],
338  # notify {“current_pvs is $current_pvs” : }
355  # notify {“pvsarray is $pvsarray” : }
356  # notify {“number of pvs is $numberofpvs” : }
359  # notify {“vgpvs is $vgpvs” : }
360  # notify {“allpvs is $allpvs” : }
367  # notify {“current_pvs_string is $current_pvs_string” : }
421  # notify {‘setup /oracle, /oracle/client and /usr/sap/SMD’: }
562  # notify {‘setup /usr/sap/<SID>/global/trex/data’: }
640  # notify {‘setup /usr/sap/<SID>/global/trex/data/snapshots’: }
662  # notify {‘setup /home/<SID>adm and /inst<SID>’: }
701  # notify {‘setup /SAP/<SID> /sapmnt/<SID> /usr/sap/trans’: }
754  # notify {‘create user <sid2>adm and setup /usr/sap/<SID2> /home/<sid2>adm /sapmnt/<SID2>’: }
771  # notify {“SID2 settings: $current_sid2_small $current_sid2_big”: }
821  # notify {‘setup /usr/sap/<SID>’: }
1056  # notify {“sapdatafs_last $sapdatafs_last “: }
1057  # notify {“sapdatafs: $sapdatafs “: }
1058  #                        notify {“sapdatafs_testb: $sapdatafs_testb “: }
1059  #                        notify {“sapdatafs_testc: $sapdatafs_testc “: }
1060  # notify {“last_req_sapdatafs : $last_req_sapdatafs “: }

These 2 sed commands can be put in a 1-liner as well:
sed -i ‘/^#/!s/notify/# notify/;/# notify/s/^[ \t]*//’  <file>.pp



See also this very useful reference:  http://sed.sourceforge.net/sed1line.txt