Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This is probably easy and something is not clicking for me and my lack of coffee right now. I have a file with multiple lines that begin with a tab then the word GROUP something {

Some of these lines for whatever reason drop the curly bracket under some conditions. The quick fix is to use sed/awk to append the curly bracket to that line but not lines where the bracket already exist. I'm halfway there with but as you can see this will append the open curly bracket to every line that begins with a tab and GROUP.

sed '/[ \t]GROUP/ s/$/ {/' scst.conf.test > greg.scst.out
share|improve this question

3 Answers 3

up vote 4 down vote accepted

You can try this sed command:

$ sed '/[ \t]GROUP/ s/{*$/ {/' scst.conf.test > greg.scst.out
share|improve this answer
    
These all worked but this one made the most sense to me. Thanks. –  egorgry Mar 8 '12 at 15:54

Checking GROUP something without { and back referencing it then replacing with \1 {.

sed 's|\([ \t]GROUP.*\)[^{]$|\1 {|' yourfile
share|improve this answer

With the following test file as test.txt:

    GROUP blah {
    GROUP blah {
    GROUP blah
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {

The following sed script does the business:

sed -r 's/\tGROUP(.*)[^{]$/&{/' test.txt

With the output looking like this:

    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {
    GROUP blah {

Is that correct? What it does is look for lines which have tab->GROUP any characters but don't have { at the end. Replaces it with the matched line (that is what the & does) and appends a { afterwards.

share|improve this answer

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.