sed practice

Review
extension

  1. print line to line content http://ask.apelearn.com/question/559
    # sed -n '/特定字符/,/特定字符/'p test.txt
  2. sed convert case http://ask.apelearn.com/question/7758
    1. In sed, use \u for uppercase and \l for lowercase
    2. Capitalize the first lowercase letter of each word:
      # sed 's/\b[a-z]/\u&/g' filename
    3. Convert all lowercase to uppercase:
      # sed 's/[a-z]/\u&/g' filename
    4. Uppercase to lowercase:
      # sed 's/[A-Z]/\l&/g' filename
  3. sed adds a number at the end of a line http://ask.apelearn.com/question/288
    # sed 's/(^指定字符.*)/\1 数字/' test.txt
    # sed 's/(^指定字符.*)/& 数字/' test.txt
    # sed '/^指定字符/s/$/ 数字/' test.txt
  4. Delete a line to the last line http://ask.apelearn.com/question/213
    # sed '/指定字符/{p;:a;N;$!ba;d}' test.txt  
    定义一个标签a,匹配c,然后N把下一行加到模式空间里,匹配最后一行时,才退出标签循环,然后命令d,把这个模式空间里的内容全部清除。  
    
    if 匹配"c"  
    :a
    追加下一行  
    if 不匹配"$"  
    goto a  
    最后退出循环,d命令删除。
    
    Delete the matching line and the next line of the matching line
    sed -i '/sample/{N;d}' filename
    //sample is the matching character, N here is the next line, d is the delete
  5. Print 1 to 100 lines containing a string http://ask.apelearn.com/question/1048
    # sed -n '1,100{/sample/p}' 1.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167954&siteId=291194637
sed