{"id":914,"date":"2021-04-30T08:04:27","date_gmt":"2021-04-30T08:04:27","guid":{"rendered":"http:\/\/www.liutianfeng.com\/?p=914"},"modified":"2022-04-22T08:35:08","modified_gmt":"2022-04-22T08:35:08","slug":"%e3%80%90%e4%b8%89%e5%89%91%e5%ae%a2%e3%80%91%e4%b9%8bsed%e4%bd%bf%e7%94%a8%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/www.liutianfeng.com\/?p=914","title":{"rendered":"\u3010\u4e09\u5251\u5ba2\u3011\u4e4bsed\u4f7f\u7528\u603b\u7ed3"},"content":{"rendered":"<p>sed\u4f5c\u4e3aLinux\u4e09\u5251\u5ba2\u547d\u4ee4\u4e4b\u4e00\uff0c\u4f7f\u7528\u573a\u666f\u6781\u591a\uff0c\u603b\u7ed3\u5e38\u7528\u548c\u6bd4\u8f83\u9ad8\u7ea7\u7684\u7528\u6cd5\uff0c\u4f9b\u540e\u7eed\u53c2\u8003\u3002<\/p>\n<h2>sed\u7684\u9009\u9879<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">&gt; sed --help\r\nUsage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...\r\n\r\n  -n, --quiet, --silent\r\n                 suppress automatic printing of pattern space\r\n  -e script, --expression=script\r\n                 add the script to the commands to be executed\r\n  -f script-file, --file=script-file\r\n                 add the contents of script-file to the commands to be executed\r\n  --follow-symlinks\r\n                 follow symlinks when processing in place\r\n  -i[SUFFIX], --in-place[=SUFFIX]\r\n                 edit files in place (makes backup if SUFFIX supplied)\r\n  -c, --copy\r\n                 use copy instead of rename when shuffling files in -i mode\r\n  -b, --binary\r\n                 does nothing; for compatibility with WIN32\/CYGWIN\/MSDOS\/EMX (\r\n                 open files in binary mode (CR+LFs are not treated specially))\r\n  -l N, --line-length=N\r\n                 specify the desired line-wrap length for the `l' command\r\n  --posix\r\n                 disable all GNU extensions.\r\n  -r, --regexp-extended\r\n                 use extended regular expressions in the script.\r\n  -s, --separate\r\n                 consider files as separate rather than as a single continuous\r\n                 long stream.\r\n  -u, --unbuffered\r\n                 load minimal amounts of data from the input files and flush\r\n                 the output buffers more often\r\n  -z, --null-data\r\n                 separate lines by NUL characters\r\n  --help\r\n                 display this help and exit\r\n  --version\r\n                 output version information and exit<\/code><\/pre>\n<p>\u793a\u4f8b\uff1ased -i \u5b9e\u73b0\u6587\u4ef6\u5b57\u7b26\u4e32\u66ff\u6362<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">&gt; sed -i.bak 's\/j\/a\/g' a.txt   # -i\u53ef\u4ee5\u7528\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u7f16\u8f91\u6587\u4ef6\uff0c-i\u540e\u9762\u7d27\u8ddf\u4e00\u4e2a\u5b57\u7b26\u4e32\uff08\u4e0d\u80fd\u52a0\u7a7a\u683c\uff09\uff0c\u53ef\u4ee5\u5728\u66ff\u6362\u524d\u5c06\u6587\u4ef6\u505a\u4e00\u4e2a\u5907\u4efd\uff0c\u6bd4\u5982\u4f4d\u7f6e\u662f.bak\uff0c\u5907\u4efd\u6587\u4ef6\u5c31\u662ffile.bak\r\n&gt; ls -lhrt\r\ntotal 12K\r\n-rw-------. 1 root root 1.3K Apr 29 07:37 anaconda-ks.cfg\r\n-rw-r--r--  1 root root   25 Apr 30 03:51 a.txt.bak\r\n-rw-r--r--  1 root root   25 Apr 30 03:53 a.txt\r\n&gt; diff a.txt a.txt.bak \r\n1c1\r\n&lt; dlkdakakadfaadkfaakdafka\r\n---\r\n&gt; dlkdjkjkjdfjadkfjakdjfkj<\/code><\/pre>\n<h2><strong>\u5e38\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>1\u3001\u5339\u914d\u67d0\u4e2a\u5b57\u7b26\u4e32\u5f00\u5934\u5230\u6587\u4ef6\u672b\u5c3e\u5220\u9664<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">sed \"\/^# ================================\u6211\u662f\u7981\u6b62\/,\\$d\" data.sh\u00a0\u00a0#\u00a0\u8fd9\u91cc\u6ce8\u610f\uff0c\"\"\u662f\u6709\u53d8\u91cf\u66ff\u6362\u7684\uff0c\u56e0\u6b64\u9700\u8981\\\u8131\u4e49\uff0c\u5355\u5f15\u53f7\u5c31\u4e0d\u9700\u8981\r\nsed\u00a0'\/^# ================================\u6211\u662f\u7981\u6b62\/,$d' data.sh<\/code><\/pre>\n<p>2\u3001\u5339\u914d\u5b57\u7b26\u4e321\u5230\u5b57\u7b26\u4e322\u4e4b\u95f4\u7684\u5185\u5bb9\u5220\u9664<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">sed '\/10.3.52.40\/,\/10.3.52.41\/d' a.txt<\/code><\/pre>\n<p>3\u3001\u5339\u914d\u5b57\u7b26\u4e32\uff0c\u5728\u540e\u9762\u7684\u7b2c\u4e09\u884c\u65b0\u589e\u5185\u5bb9<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\"># cat a.txt\r\n2022-03-15 15:39:44\r\nDC_BAK_DIR=yb-ys\r\nSUB_ENV=test\r\nPOD_LOG_DIR=\/data\/log\/sql-pipeline\/yb-ys\/test\/hNstdltTbM8hPut2J1TPNRirCYcFfBVf\r\nRANDOM_DIR=hNstdltTbM8hPut2J1TPNRirCYcFfBVf\r\nNAME_SPACE=c87e2267-1001-4c70-bb2a-ab41f3b81aa3\r\nINSTANCE_ID=test-sql-pipeline-v2-6675df7596-g8jsl\r\nAPP_NAME=YWB-sql-pipeline-v2-\u8fed\u4ee3\u66f4\u65b0\u6d4b\u8bd5-\u5ffd\u7565\u544a\u8b66\r\n\r\n# sed '\/c87e2267-1001-4c70-bb2a-ab41f3b81aa3\/{n;n;s#$#\\nTest add things...#}' a.txt  # n;\u4ee3\u88681\u884c\r\n2022-03-15 15:39:44\r\nDC_BAK_DIR=yb-ys\r\nSUB_ENV=test\r\nPOD_LOG_DIR=\/data\/log\/sql-pipeline\/yb-ys\/test\/hNstdltTbM8hPut2J1TPNRirCYcFfBVf\r\nRANDOM_DIR=hNstdltTbM8hPut2J1TPNRirCYcFfBVf\r\nNAME_SPACE=c87e2267-1001-4c70-bb2a-ab41f3b81aa3\r\nINSTANCE_ID=test-sql-pipeline-v2-6675df7596-g8jsl\r\nAPP_NAME=YWB-sql-pipeline-v2-\u8fed\u4ee3\u66f4\u65b0\u6d4b\u8bd5-\u5ffd\u7565\u544a\u8b66\r\nTest add things...<\/code><\/pre>\n<p>4\u3001\u53bb\u6389\u6bcf\u884c\u6700\u540e\u7684\u4e00\u4e2a\u5b57\u7b26<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">sed 's\/.$\/\/'<\/code><\/pre>\n<p>5\u3001\u53bb\u6389\u989c\u8272<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">sed -r \"s\/\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]\/\/g\"<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.liutianfeng.com\">liutianfeng.com<\/a> &raquo; <a href=\"https:\/\/www.liutianfeng.com\/?p=914\">\u3010\u4e09\u5251\u5ba2\u3011\u4e4bsed\u4f7f\u7528\u603b\u7ed3<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>sed\u4f5c\u4e3aLinux\u4e09\u5251\u5ba2\u547d\u4ee4\u4e4b\u4e00\uff0c\u4f7f\u7528\u573a\u666f\u6781\u591a\uff0c\u603b\u7ed3\u5e38\u7528\u548c\u6bd4\u8f83\u9ad8\u7ea7\u7684\u7528\u6cd5\uff0c\u4f9b\u540e\u7eed\u53c2\u8003\u3002 sed\u7684\u9009\u9879 &#038;gt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[57],"tags":[],"_links":{"self":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/914"}],"collection":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=914"}],"version-history":[{"count":3,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/914\/revisions"}],"predecessor-version":[{"id":1234,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/914\/revisions\/1234"}],"wp:attachment":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}