Sed-Delete one or more lines from a file

Source: Internet
Author: User

Sed-Delete one or more lines from a file
Syntax: C code sed '{[/] <n> | <string> | <regex> [/]} d' <fileName> sed' {[/] <adr1> [, <adr2>] [/] D' <fileName> /... /= delimitersn = line numberstring = string found in lineregex = regular expression corresponding to the searched patternaddr = address of a line (number or pattern) d = delete Examples Remove the 3rd line: C code sed '3d 'fileName.txt Remove the line containing the string "awk": C code sed'/awk/d' fi Lename.txt Remove the last line: C code sed '$ d' filename.txt Remove all empty lines: C code sed'/^ $/d 'filename.txt sed '/./! D 'filename.txt Remove the line matching by a regular expression (by eliminating one containing digital characters, at least 1 digit, located at the end of the line ): C code sed '/[0-9/] [0-9] * $/d' filename.txt Remove the interval between lines 7 and 9: C code sed '7, 9d 'filename.txt The same operation as abve but replacing the address with parameters: C code sed '/-Start/,/-End/d' filename.txt The above examp Les are only changed at the display of the file (stdout1 = screen ). for permanent changes to the old versions (<4) use a temporary file for GNU sed using the "-I [suffix]": C code sed-I ". bak "'3d 'filename.txt Task: Remove blank lines using sedType the following command: C code $ sed'/^ $/d' input.txt> output.txt Task: remove blank lines using grepC code $ grep-v '^ $ 'input.txt> output.txt Both grep and se D use special pattern ^ $ that matchs the blank lines. grep-v option means print all lines except T blank line. let us say directory/home/me/data /*. txt has all text file. use following for loop (shell script) to remove all blank lines from all files stored in/home/me/data directory: C code #! /Bin/sh files = "/home/me/data /*. txt "for I in $ files do sed '/^ $/d' $ I> $ I. out mv $ I. out $ I done Updated for accuracy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.