search
HomeDatabaseMysql Tutorialsed常见用法总结

sed常见用法总结

Jun 07, 2016 pm 04:01 PM
sedXYZcommonSummarizetextusageedit

编辑文本 sed -i 1i xyz test.txt 在第一行之前sed -i 1a xyz test.txt 在第一行之后插入sed -i 1c xyz test.txt 把第一行数据替换成xyzsed /^bb/i\kjdlfkjdslkf temp.txt //在匹配的行之前加入新一行sed /^bb/a\kjdlfkjdslkf temp.txt //在匹配的行之后加入

编辑文本 
sed -i '1i xyz' test.txt 在第一行之前
sed -i '1a xyz' test.txt  在第一行之后插入
sed -i '1c xyz' test.txt  把第一行数据替换成xyz

sed '/^bb/i\kjdlfkjdslkf' temp.txt  //在匹配的行之前加入新一行
sed '/^bb/a\kjdlfkjdslkf' temp.txt  //在匹配的行之后加入新行
 sed -n '/xxx/w temp.txt' temp1.txt //temp1.txt中匹配xxx的行插入到temp.txt
sed '/xx/c\sdfdsf' temp.txt //用新的一行数据替换匹配xx的行

sed '1d' t.txt  /删除文件第一行
sed '/bro/d' t.txt  //删除带bro的行  (实际修改,删除等加-i)
sed '/^$/d' t.txt //删除空行

sed 's/^.*uid//' t.txt  //将uid前这段字符替换成空,^第一个字符不能为*,要加.,另外在sed里字符与*连接要加. 
sed 's/night/NIGHT/' 1.TXT //将night替换  (加 -i 直接修改源文件1.txt,而不是将替换后的数据输出到屏幕)
sed 's/night/NIGHT/g' 1.TXT  替换所有,不加g一行只替换一次
sed 's/.html//' b.txt >b0.txt

sed 'y/bo/BO/' test.txt  //将b替换成B,o替换成O
sed 'y/bb ll/BB LL/' test.txt  //将bb替换成BB,ll替换成LL

查询文本

sed '/he/w test1.txt' test.txt   //读取test.txt内容匹配he的行写入test1.txt
sed '1,2w test1.txt' test.txt    //读取test.txt内容,第一二行写入test1.txt
sed '/he/r test1.txt' test.txt //读取test.txt内容匹配he的行与test1.txt所有内容合并后输出
 
sed -n '1,3p' getrow.sh   //显示一行到三行的肉容
sed -n '1,/hello/'p  orig.txt //从第一行开始打印,打印到第一个含有hello行
//n的作用是取消默认输出 只打印包含模板的行,缺省为打印所有行(编辑和未编辑)

sed -n '$p' getrow.sh   //最后一行
sed -n '/echo/'p while.sh  //打印包括echo字符的行 /pattern/模式
sed -e '/echo/=' while.sh  //并且打印行号(并且整个文件都打印) -n 只打印实际行号  打印行号使用=
sed -n -e '/dfs.support.append/=' hdfs-default.xml
sed -n  -e '/we/p' -e '/we/=' 2.txt  //只打印匹配的行,并行显示行号 
sed -n  '/aa/='  aa.txt  //只显示匹配上的行号

sed -n '/^h/'p test.txt //显示h为开头的行
sed '/^hello/d' test.txt  //查询非某某开头的写法

sed -n '/s\{2,\}/'p test.txt //s字符至少匹配两次
sed -n '/[0-9]\{1,\}/'p test.txt //包含数字的行,用[0-9]+不支持

sed '2q' test.txt  //打印到第二行退出

需要注意的是,sed并不直接操作初始数据,它操作的是一份原始数据的拷贝。sed处理时,把当前处理的行存储在临时缓冲区中,然后处理缓冲区中的内容,处理完成后,如果没有重定向到文件, 将把缓冲区中的内容送往屏幕,接着处理下一行直到处理完毕
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor