Unix Sed Tutorial6 : Append, Insert, Replace, and Count File Lines

本文介绍了使用sed命令进行文件内容操作的方法,包括追加、插入、替换和获取行号等,通过具体示例展示了如何在Linux环境中使用sed进行文件编辑。

This article is part of the on going Unix sed command tutorial series. In our previous articles we learned sed with single commands — printingdeletionsubstitute and file write.

Sed provides lot of commands to perform number of operations with the lines in a file.

In this article let us review how to append, insert, replace a line in a file and how to get line numbers of a file.

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

$cat thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Append Lines Using Sed Command

Sed provides the command “a” which appends a line after every line with the address or pattern.

Syntax:

#sed 'ADDRESS a\
	Line which you want to append' filename

#sed '/PATTERN/ a\
	Line which you want to append' filename

Sed Append Example 1. Add a line after the 3rd line of the file.

Add the line “Cool gadgets and websites” after the 3rd line. sed “a” command inserts the line after match.

$ sed '3 a\
> Cool gadgets and websites' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot

Sed Append Example 2. Append a line after every line matching the pattern

The below sed command will add the line “Linux Scripting” after every line that matches the pattern “Sysadmin”.

$ sed '/Sysadmin/a \
> Linux Scripting' thegeekstuff.txt

Linux Sysadmin
Linux Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Linux Scripting

Sed Append Example 3. Append a line at the end of the file

The following example, appends the line “Website Design” at the end of the file.

$ sed '$ a\
> Website Design' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Website Design

Insert Lines Using Sed Command

Sed command “i” is used to insert a line before every line with the range or pattern.

Syntax:

#sed 'ADDRESS i\
	Line which you want to insert' filename

#sed '/PATTERN/ i\
	Line which you want to insert' filename

Sed Insert Example 1. Add a line before the 4th line of the line.

Add a line “Cool gadgets and websites” before 4th line. “a” command inserts the line after match whereas “i” inserts before match.


可以写做一行 sed '4 i  Cool Gadets' thegeekstuff.txt

$ sed '4 i\
> Cool gadgets and websites' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Insert Example 2. Insert a line before every line with the pattern

The below sed command will add a line “Linux Scripting” before every line that matches with the pattern called ‘Sysadmin”.

$ sed '/Sysadmin/i \
> Linux Scripting' thegeekstuff.txt

Linux Scripting
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Linux Scripting
Windows- Sysadmin, reboot etc.

Sed Insert Example 3. Insert a line before the last line of the file.

Append a line “Website Design” before the last line of the file.

$ sed '$ i\
> Website Design' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Website Design
Windows- Sysadmin, reboot etc.

Replace Lines Using Sed Command

“c” command in sed used to replace every line matches with the pattern or ranges with the new given line.

Syntax:

#sed 'ADDRESS c\
	new line' filename

#sed '/PATTERN/ c\
	new line' filename

Sed Replace Example 1. Replace a first line of the file

The below command replaces the first line of the file with the “The Geek Stuff”.

$ sed '1 c\
> The Geek Stuff' thegeekstuff.txt

The Geek Stuff
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Replace Example 2. Replace a line which matches the pattern

Replace everyline which has a pattern “Linux Sysadmin” to “Linux Sysadmin – Scripting”.

$ sed '/Linux Sysadmin/c \
> Linux Sysadmin - Scripting' thegeekstuff.txt

Linux Sysadmin - Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Replace Example 3. Replace the last line of the file

Sed command given below replaces the last line of the file with “Last Line of the file”.

$ sed '$ c\
> Last line of the file' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Last line of the file

Print Line Numbers Using Sed Command

“=” is a command in sed to print the current line number to the standard output.

Syntax:

#sed '=' filename

The above send command syntax prints line number in the first line and the original line from the file in the next line .

sed ‘=’ command accepts only one address, so if you want to print line number for a range of lines, you must use the curly braces.

Syntax:

# sed -n '/PATTERN/,/PATTERN/ {
=
p
}' filename

Sed Line Number Example 1. Find the line number which contains the pattern

The below sed command prints the line number for which matches with the pattern “Databases”

$ sed -n '/Databases/=' thegeekstuff.txt

2

Sed Line Number Example 2. Printing Range of line numbers

   一行的写法 :   sed -n  '1,5{=;p}' thegeekstuff.txt

Print the line numbers for the lines matches from the pattern “Oracle” to “Productivity”.

$ sed -n '/Oracle/,/Productivity/{
> =
> p
> }' thegeekstuff.txt

2
Databases - Oracle, mySQL etc.
3
Security (Firewall, Network, Online Security etc)
4
Storage in Linux
5
Productivity (Too many technologies to explore, not much time available)

Sed Line Number Example 3. Print the total number of lines in a file

Line number of the last line of the file will be the total lines in a file. Pattern $ specifies the last line of the file.

$ sed -n '$=' thegeekstuff.txt

6
内容概要:本文围绕基于支持向量机的电力短期负荷预测方法展开基于支持向量机的电力短期负荷预测方法研究——最小二乘支持向量机、标准粒子群算法支持向量机与改进粒子群算法支持向量机的对比分析(Matlab代码实现)研究,重点对比分析了三种方法:最小二乘支持向量机(LSSVM)、标准粒子群算法优化的支持向量机(PSO-SVM)以及改进粒子群算法优化的支持向量机(IPSO-SVM)。文章详细介绍了各模型的构建过程与优化机制,并通过Matlab代码实现对电力负荷数据进行预测,评估不同方法在预测精度、收敛速度和稳定性方面的性能差异。研究旨在为电力系统调度提供高精度的短期负荷预测方案,提升电网运行效率与可靠性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的科研人员、电气工程及相关专业的研究生或高年级本科生;对机器学习在能源领域应用感兴趣的技术人员。; 使用场景及目标:①应用于电力系统短期负荷预测的实际建模与仿真;②比较不同优化算法对支持向量机预测性能的影响;③为相关课题研究提供可复现的代码参考和技术路线支持。; 阅读建议:建议读者结合文中提供的Matlab代码,深入理解每种支持向量机模型的参数设置与优化流程,动手实践以掌握算法细节,并可通过更换数据集进一步验证模型泛化能力。
【源码免费下载链接】:https://renmaiwang.cn/s/qaiji 18、MapReduce的计数器与通过MapReduce读取_写入数据库示例网址: input files to process”表示处理的总输入文件数量,“number of splits”指示文件被分割成多少个块进行处理,“Running job”显示作业的状态等。自定义计数器则是开发者根据实际需求创建的,用于跟踪特定任务的特定指标。开发者可以在Mapper或Reducer类中增加自定义计数器,然后在代码中增加计数器的值。这样,当作业完成后,可以通过查看计数器的值来分析程序的行为和性能。接下来,我们将讨论如何通过MapReduce与数据库交互,尤其是MySQL数据库。在大数据场景下,有时需要将MapReduce处理的结果存储到关系型数据库中,或者从数据库中读取数据进行处理。Hadoop提供了JDBC(Java Database Connectivity)接口,使得MapReduce作业能够与数据库进行连接和操作。要实现MapReduce读取数据库,首先需要在Mapper类中加载数据库驱动并建立连接。然后,可以在map()方法中使用SQL查询获取所需数据。在Reduce阶段,可以对数据进行进一步处理和聚合,最后将结果写入到数据库中。对于写入数据库,通常在Reducer类的reduce()方法或cleanup()方法中进行,将处理后的数据转换为适合数据库存储的格式,然后通过JDBC API执行插入、更新或删除等操作。需要注意的是,由于MapReduce作业可能涉及大量的数据写入,因此需要考虑数据库的并发处理能力和性能优化策略。总结一下,MapReduce的计数器提供了强大的监控和调试能力,而通过MapReduce与数据库的交互则扩展了大数据处理的应用场景。开发者可以根据需求利用计数器来优化作业
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值