sed

Shell编程之sed工具

核能气质少年 提交于 2020-02-26 01:22:31
一、sed介绍 1.sed用来做啥? 2.sed如何处理文件? 二、sed使用方法介绍 1.命令行格式 1)语法格式 2)举例说明 2.脚本格式 1)用法 2)注意事项 3)举例说明 3.补充扩展总结 三、课堂练习 四、课后实战 一、sed介绍 1. sed用来做啥? sed是Stream Editor(流编辑器)的缩写,简称流编辑器;用来==处理文件==的。 2. sed如何处理文件? sed是==一行一行读取==文件内容并==按照要求==进行==处理==,把处理后的结果==输出到屏幕==。 首先sed读取文件中的一行内容,把其保存在一个==临时缓存区中==(也称为模式空间) 然后==根据需求==处理临时缓冲区中的行,完成后把该行==发送到屏幕上== 总结: 由于sed把每一行都存在临时缓冲区中,对这个 副本 进行编辑,所以==不会直接修改原文件== Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作,对文件进行过滤和转换操作 二、sed使用方法介绍 sed常见的语法格式有两种,一种叫==命令行==模式,另一种叫==脚本==模式。 1. 命令行格式 1)语法格式 sed [options] == ' ==处理动作 =='== 文件名 常用选项 选项 说明 备注 -e 进行多项(多次)编辑 ==-n== 取消默认输出 不自动打印模式空间 ==-r== 使用扩展==正则表达式

文本处理工具sed常用命令

末鹿安然 提交于 2020-02-25 23:45:08
sed 是一个文本处理工具,可以将数据进行替换、新增、选取等特定工作 格式 sed 选项 动作 文件名 使用sed替换文件中某个域的字段 比如写的一个yaml文件中间有个字段需要修改,可以使用sed在外面命令行进行替换 格式:sed 's/原字符/新的字符/‘ 文件名 [root@zhaocheng ~]# sed 's/systemctl/service/' filetest root:$1$dDTFylQ3$.vTZKpm7mrra9WMsxvBfW.:18241:0:99999:7 bin:*:17834:0:99999:7:dad lp:*:17834:0:99999:7ada sync:*:17834:0:99999:7:::gg shutdown:*:17834:0:99999:7::da halt:*:17834:0:99999:7::fsda nginx:!!:18289::::::daaf rabbitmq:!!:18297:::::dada service start mysqld dadad:nginx:sdada 上面输出的只会输出到屏幕上,并不会修改文件,如果直接替换可以添加-i [root@zhaocheng ~]# sed -i 's/systemctl/service/' filetest [root@zhaocheng ~]# cat

match everything up to first '/' after a specific pattern

家住魔仙堡 提交于 2020-02-25 04:46:29
问题 Trying to do this within a bash script using sed regex on macos. I have a file with directory listings of an external drive. /Volumes/WD/A/Some Learning/Learning Is Great/ /Volumes/WD/A/Some Deeper/Learning Is Great/Another Learning Opportunity/ /Volumes/WD/B/More Things Here/Great Learning/ /Volumes/WD/B/More Things/ I want to search and return the top-most directory matching various patterns. If you search for 'Learning' the output should be: /Volumes/WD/A/Some Learning/ /Volumes/WD/A/Some

linux下正则表达式

有些话、适合烂在心里 提交于 2020-02-23 16:29:12
正则表达式 1. 用 du -sh /* 查看根下所有目录占用的内存是多少 2.lsof -i :22 查看 22 端口 3. 清空文件, cat /dev/null>access_log 清空一个文件 4. 如果在 oldboy/ett 目录下在创建一个目录,那么 /oldbot/ett 的硬链接数是多少,为什么? 2 个,因为子目录下的 .. 是父目录的硬链接 5 检测一个命令是否是内置命令: type 命令名称 Linux 的多用户多任务介绍: 1. 用户的身份是用 UID ( useridentify )和 GID(groupidentify) 来识别的 . 2. 超级用户的 UID=0 , GID=0 ,再生产环境中一般禁止用 root 用户直接 ssh 连接服务器。其他的用户都是虚拟用户,特点是不可以登录 0 代表超级用户, 1-499 代表虚拟用户, 500-65535 代表普通用户 4. 服务运行时需要用户角色的,可以不用登陆,因此,工作中我们要运行 mysql 数据库,可以创建如下用户 group -g 49 useradd -u 49 -s /sbin/nologin -g 49 -s 解释器命令改为不需要用户登录,直接可以进入 id mysql tail -1 /etc/passwd su - mysql chattr -i /etc/passwd /etc

passing bash arguments into sed to delete lines

£可爱£侵袭症+ 提交于 2020-02-22 15:33:13
问题 I'm trying to write a bash function which will take all the arguments sent to it and push them through sed to delete lines in a file that match the arguments. A use case example: to_delete get the trash I can get it to "almost" work like this: function to_delete() { sed -i -e "/$@/d" /tmp/testfile; } The problem with this is it will only work if I send a single command line argument: to_delete get If I send more than one it returns this error: sed: 1: "/get": unterminated regular expression

using sed, remove everything before the first occurence of a character

。_饼干妹妹 提交于 2020-02-21 13:48:12
问题 Let's say I have a line looking like this Hello my first name is =Bart and my second is =Homer How can I do if I want to get everything after the first = or : using sed ? In this example, I would like to get the result Bart and my second is =Homer I am using sed 's/.*[=:]//' right now but I get Homer as result (everything after the last = or : ) and I would like to get everything after the first, and not the last = or : 回答1: Normally, quantifiers in sed are greedy, which is why you will

using sed, remove everything before the first occurence of a character

寵の児 提交于 2020-02-21 13:45:12
问题 Let's say I have a line looking like this Hello my first name is =Bart and my second is =Homer How can I do if I want to get everything after the first = or : using sed ? In this example, I would like to get the result Bart and my second is =Homer I am using sed 's/.*[=:]//' right now but I get Homer as result (everything after the last = or : ) and I would like to get everything after the first, and not the last = or : 回答1: Normally, quantifiers in sed are greedy, which is why you will

Bash: how to find and break up long lines by inserting continuation character and newline?

与世无争的帅哥 提交于 2020-02-21 13:02:54
问题 I know how to find long lines in a file, using awk or sed: $ awk 'length<=5' foo.txt will print only lines of length <= 5. sed -i '/^.\{5,\}$/d' FILE would delete all lines with more than 5 characters. But how to find long lines and then break them up by inserting the continuation character ('&' in my case) and a newline? Background: I have some fortran code that is generated automatically. Unfortunately, some lines exceed the limit of 132 characters. I want to find them and break them up

sed (in bash) works with [ \t] but not with \s?

孤街浪徒 提交于 2020-02-21 13:01:34
问题 I want to search-replace something containing whitespace on a bash command line, and I assumed sed would be the easiest way to go. Using [ \t] denoting either tab or space, to match the whitespace, works as intended: echo "abc xyz" | sed "s/[ \t]xyz/123/" abc123 But using \s instead of [ \t] does not, to my surprise: echo "abc xyz" | sed "s/\sxyz/123/" abc xyz I'm fairly new to bash so I might be missing something trivial, but no matter what I do, I can't get this to work. Using \\s instead

sed (in bash) works with [ \t] but not with \s?

橙三吉。 提交于 2020-02-21 12:58:46
问题 I want to search-replace something containing whitespace on a bash command line, and I assumed sed would be the easiest way to go. Using [ \t] denoting either tab or space, to match the whitespace, works as intended: echo "abc xyz" | sed "s/[ \t]xyz/123/" abc123 But using \s instead of [ \t] does not, to my surprise: echo "abc xyz" | sed "s/\sxyz/123/" abc xyz I'm fairly new to bash so I might be missing something trivial, but no matter what I do, I can't get this to work. Using \\s instead