Linux sed (stream editor) part 3

continue from part 2

sed commands:

  • y
    The command is doing substitution of letter by letter. We give it a series of letters to replace, and then in the same order a series of letters that replacing the first series. It will replace the first letter from the first series with the first letter from the second series, the second letter from the first series with the second letter from the second series and so.
    In the example below, we print the content of the bower.json file and then use the y to replace a with e

    
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '' bower.json 
    {
      "name": "angularjs",
      "version": "0.0.0",
      "authors": [
        "Oren Tal <oren.tal2@gmail.com>"
      ],
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
        "angularjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "angular": "^1.5.0"
      }
    }
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed 'y/a/e/' bower.json 
    {
      "neme": "engulerjs",
      "version": "0.0.0",
      "euthors": [
        "Oren Tel <oren.tel2@gmeil.com>"
      ],
      "description": "enguler prectising",
      "mein": "index.html",
      "keywords": [
        "engulerjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "enguler": "^1.5.0"
      }
    }
    
    
  • – write to file
    The w command, will write to a file, we should give it a file name
    w <fileName>
    In the example below we use w to write all line that contain ‘:’ to the file test

    
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '/:/ w test' bower.json 
    {
      "name": "angularjs",
      "version": "0.0.0",
      "authors": [
        "Oren Tal <oren.tal2@gmail.com>"
      ],
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
        "angularjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "angular": "^1.5.0"
      }
    }  
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '' test 
      "name": "angularjs",
      "version": "0.0.0",
      "authors": [
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
      "license": "MIT",
      "ignore": [
      "dependencies": {
        "angular": "^1.5.0"
    
    
    
  • – read file
    The r command will read the content of the file to the current location of the sed execution. The structure is
    r <fileName>
    In the example below we read the content of the test file in the end of the file bower.json

    
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '$ r test' bower.json 
    {
      "name": "angularjs",
      "version": "0.0.0",
      "authors": [
        "Oren Tal <oren.tal2@gmail.com>"
      ],
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
        "angularjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "angular": "^1.5.0"
      }
    }
      "name": "angularjs",
      "version": "0.0.0",
      "authors": [
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
      "license": "MIT",
      "ignore": [
      "dependencies": {
        "angular": "^1.5.0"
    
    
    
  • – append line, insert text in the line below the current line. The structure:
    a "<textToAppendBelow>"
    In the example below we append the text “line 2” to line 2.

    
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '2a"line 2"' bower.json 
    {
      "name": "angularjs",
    "line 2"
      "version": "0.0.0",
      "authors": [
        "Oren Tal <oren.tal2@gmail.com>"
      ],
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
        "angularjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "angular": "^1.5.0"
      }
    }
    
    
  • – insert line, will insert line above the current line.
    The structure is:
    i "<textToInsertAbove>"
    In the example below, we will insert the text “line 2” above line 2.

    
    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '2i"line 2"' bower.json 
    {
    "line 2"
      "name": "angularjs",
      "version": "0.0.0",
      "authors": [
        "Oren Tal <oren.tal2@gmail.com>"
      ],
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
        "angularjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "angular": "^1.5.0"
      }
    }
    
    
  • – change line, will replace the current line, with the new text we give it:
    c "<textToReplaceTheCurrentLine>"
    In the example below we will replace line 2 with the text “line 2”

    oren@oren-computer:~/workspace/angularjs  (component) 
    $ sed '2c"line 2"' bower.json 
    {
    "line 2"
      "version": "0.0.0",
      "authors": [
        "Oren Tal <oren.tal2@gmail.com>"
      ],
      "description": "angular practising",
      "main": "index.html",
      "keywords": [
        "angularjs"
      ],
      "license": "MIT",
      "ignore": [
        "**/.*",
        "node_modules",
        "bower_components",
        "test",
        "tests"
      ],
      "dependencies": {
        "angular": "^1.5.0"
      }
    }
    
    
Advertisements

One thought on “Linux sed (stream editor) part 3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s