Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to replace a fixed pattern with another pattern. The pattern which needs to be replaced is:

{
        "field_name": "genDtl.schmNature",
        "data_type": "tbaFlagType","fld_size": "0","bspc": "",
            "type": "hardcoded",
            "value": "N",
            "neg_value": "",
            "desc": "",
            "is_fetch_input": "N",
        },

The resulting pattern should be:

{
        "field_name": "genDtl.schmNature",
        "data_type": "tbaFlagType","fld_size": "0","bspc": "",
            "type": "hardcoded",
            "value": "N",
            "neg_value": "",
            "desc": "",
            "is_fetch_input": "N",
        },
        {
        "field_name": "genDtl.autoClsOfZeroBalAcctFlg",
        "data_type": "tbaFlagType","fld_size": "0","bspc": "",
            "type": "hardcoded",
            "value": "Y",
            "neg_value": "",
            "desc": "",
            "is_fetch_input": "N",
        },

How can I use the sed command to achieve this?

share|improve this question
add comment

1 Answer

sed 's/your-long-pattern/&the-additional-text/' inputfile

The ampersand repeats the found string.

You might also be able to use:

sed 's/\({ "field_name": "\)\(genDtl.schmNature\)\(", "data_type": "tbaFlagType","fld_size": "0","bspc": "", "type": "hardcoded", "value": "\)\(N\)\(", "neg_value": "", "desc": "", "is_fetch_input": "N", },\)/\1\2\3\4\5\1genDtl.autoClsOfZeroBalAcctFlg\3Y\5/' inputfile

(untested)

share|improve this answer
    
For large substitutions like this, I suggest using an external sed script file with the -e flag, e.g. sed -e foo.sed <infile > outfile. Registers are fine or you can apply multiple "s" commands in the sed file to each line. –  kmarsh Jul 22 '10 at 17:46
    
@kmarsh: -e is for an in-line script. I think you mean -f which takes a filename. By "registers" do you mean capture groups/back references? –  Dennis Williamson Jul 22 '10 at 20:42
    
Yes, I meant -f. The term "Registers" can be found in some families of man pages, on GNU systems do "info regex" and search for registers, in section 7.1.8 on my Linux system. –  kmarsh Aug 9 '10 at 17:09
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.