I over-documented my meta statements in my scripts. There are many. They all start with something like this:
#!/bin/bash
# Title and other notes information
## I may have other lines here, or not
ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=( ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=( ) # Corresponding datatypes for secondary options
sourced=( ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call
...
# Script continues
outsidethewire=( "key 971" ) # Leave this comment here
somearray=( "sliced apples" "pie dough" ) # Mother's secret recipe
...I need it to become this...
#!/bin/bash
# Title and other notes information
## I may have other lines here, or not
ruler=( monarch )
kingdom=( "Island" )
zipcode=( 90210 )
datatype=( 0-9- )
favoritepie=( "Cherry" )
aoptions=( "Home address" "Work address" "Mobile" )
boptions=( )
aopttypes=( string string phonenum )
bopttypes=( )
sourced=( )
subscripts=( installusr )
...
# Script continues
outsidethewire=( "key 971" ) # Leave this comment here
somearray=( "sliced apples" "pie dough" ) # Mother's secret recipe
) #
...I need to remove the comments after these array statements.
I can run this...
sed 's/ ) # .*/ )/' *
But, I want to limit that to only the first 11 occurrences per file.
From this answer I get a pattern to match the first single match, giving me this...
sed '0,/ ) # .*/s// )/' *
...but that only works for the first occurrence.
I could put it into a loop:
#!/bin/bash
counter=1
while [ "$counter" -le "11" ]; do
sed '0,/ ) # .*/s// )/' *
counter=$(expr $counter + 1)
done
This loop makes the assumption that all files will match evenly, running blankly for all files. If possible, I'd like the loop to run for each file, not for all files based on a counter. But, I'm not sure how to do that.
Is that the best way to do this? Or, is there a more "proper", fail-safe way using other Linux tools?
If I understand this correctly, you want to match comments after a closing bracket )
and delete the first 11 occurances in the file, regardless of if similar matches after a closing bracket occur.
Assume the contents of your file are;
$ cat input_file
#!/bin/bash
# Title and other notes information
## I may have other lines here, or not
ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=( ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=( ) # Corresponding datatypes for secondary options
sourced=( ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call
# Title and other notes information
## I may have other lines here, or not
ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=( ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=( ) # Corresponding datatypes for secondary options
sourced=( ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call
...
# Script continues
Using sed
, match the lines of interest then, carry out the action on the first 11 lines that matched the condition.
$ sed '/) #/{1,+10s/#.*//}' input_file
#!/bin/bash
# Title and other notes information
## I may have other lines here, or not
ruler=( monarch )
kingdom=( "Island" )
zipcode=( 90210 )
datatype=( 0-9- )
favoritepie=( "Cherry" )
aoptions=( "Home address" "Work address" "Mobile" )
boptions=( )
aopttypes=( string string phonenum )
bopttypes=( )
sourced=( )
subscripts=( installusr )
# Title and other notes information
## I may have other lines here, or not
ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=( ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=( ) # Corresponding datatypes for secondary options
sourced=( ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call
...
# Script continues