Using sed between specific lines only

后端 未结 3 1833
野的像风
野的像风 2020-12-10 11:41

I have this sed command for removing the spaces after commas.

 sed -e \'s/,\\s\\+/,/g\' example.txt

How can i change it that, it will make

相关标签:
3条回答
  • 2020-12-10 12:09

    Since OSX (BSD sed) has some syntax differences to linux (GNU) sed, thought I'd add the following from some hard-won notes of mine:

    OSX (BSD) SED find/replace within (address) block (start and end point patterns(/../) or line #s) in same file (via & via & via & section 4.20 here):

    Syntax:

    $ sed '/start_pattern/,/end_pattern/ [operations]' [target filename]
    

    Standard find/replace examples:

    $ sed -i '' '2,3 s/,\s\+/,/g' example.txt
    $ sed -i '' '/DOCTYPE/,/body/ s/,\s\+/,/g' example.txt
    

    Find/replace example with complex operator and grouping (cannot operate without grouping syntax due to stream use of standard input). All statements in grouping must be on separate lines, or separated w/ semi-colons:

    Complex Operator Example (will delete entire line containing a match):

    $ sed -i '' '2,3 {/pattern/d;}' example.txt
    

    Multi-file find + sed:

    $ find ./ -type f -name '*.html' | xargs sed -i '' '/<head>/,/<\/head>/ {/pattern/d; /pattern2/d;}'
    

    Hope this helps someone!

    0 讨论(0)
  • 2020-12-10 12:13
    sed -e '2,3!b;s/,\s\+/,/g' example.txt
    

    This version can be useful if you later want to add more commands to process the desired lines.

    0 讨论(0)
  • 2020-12-10 12:22

    Use:

    sed '2,3s/,\s\+/,/g' example.txt
    
    0 讨论(0)
提交回复
热议问题