爱情和面包是什么意思| 湿疹用什么药膏好| 内膜厚吃什么药掉内膜| 梦见自己光脚走路是什么意思| 漫不经心是什么意思| 血压表什么牌子的好最准确最耐用| 有小肚子是什么原因| 降钙素原检测是查什么的| 北京的简称是什么| 补血最快的方法是什么| 12.21是什么星座| 生长发育科检查些什么| 孤僻的人给人什么感觉| 药店加盟需要什么条件| 低能儿是什么意思| 妤字属于五行属什么| 男性吃什么增强性功能| 西洋参可以和什么一起泡水喝| 反酸烧心吃什么药| 产后恶露吃什么排干净| 猿是什么动物| 湖南有什么景点| 朱元璋原名叫什么| 逐是什么意思| 体检挂什么科室| 炙是什么意思| 米诺地尔搽剂和米诺地尔酊有什么区别| 什么是动脉瘤| 糠是什么| 40min是什么意思| peaches是什么意思| 秦王属什么生肖| 头皮特别痒是什么原因| 乐福鞋是什么鞋| 彩虹是什么形状| 梦见芹菜是什么意思| 今年是什么生肖年| 人为什么会突然晕倒| 吃什么东西可以补血| 口腔溃疡吃什么药| 乳房里面有硬块是什么原因| 频繁打嗝是什么原因| cheese是什么意思| 红眼病不能吃什么东西| 芦笋炒什么好吃| 什么鱼吃泥鳅| 尿频是什么症状| 保鲜卡是什么原理纸片| 黄药是什么| 什么叫钙化灶| 万事达卡是什么卡| 手术后为什么要平躺6小时| hcg阴性是什么意思| 孕囊长什么样| 口臭口苦吃什么药最好| 猫不能吃什么| 脑堵塞有什么症状| 蟾蜍属于什么动物| 刘备和刘邦是什么关系| 中单是什么| 什么空如洗| 肌酐高什么东西不能吃| 胎盘成熟度2级是什么意思| 蜥蜴人是什么| 世界上什么最大| 铁锭是什么意思| 耳朵里长痘是什么原因| 更年期吃什么药| a醇对皮肤有什么作用| 九王念什么| 什么的口罩| 皮肤软组织感染用什么消炎药| 强磁对人体有什么危害| 颈椎不舒服挂什么科| 80岁是什么之年| 11月15日出生是什么星座| 危机四伏是什么生肖| 忌口是什么意思| 男人肾虚吃什么最补| 4pcs是什么意思| 距骨在什么位置| cdf1是什么意思| 血管炎吃什么药| 爻是什么意思| 护肝喝什么茶| 姓黑的都是什么族| 子加一笔是什么字| 贫血是什么症状| 孩子b型血父母什么血型| hpv阴性什么意思| 赵本山什么时候去世的| 阿斯巴甜是什么| 喉咙痛不能吃什么东西| 耳石症吃什么药| 细菌感染引起的发烧吃什么药| 身份证尾号代表什么| 16年属什么生肖| 溢字五行属什么| 十二朵玫瑰花代表什么意思| 吃什么食物降尿酸最快| 站久了腰疼是什么原因| 牛肉可以炖什么| 兰花叶子发黄是什么原因| 足金是什么意思| 九月二十四号是什么星座| dennis什么意思| 蜜蜡五行属什么| 要强的女人是什么性格| 天空像什么| 向日葵为什么会随着太阳转动| 青霉素主治什么病| 身上长红点很痒是什么原因| 口腔溃疡用什么药治疗| 乙肝肝炎表面抗体阳性是什么意思| 黄精吃了有什么好处| 铁瓷是什么意思| pcl是什么意思| 邹的左边读什么| 甲氧氯普胺片又叫什么| 行政管理是做什么的| 手会发抖是什么原因| 指甲凹凸不平是什么原因| 查肝肾功能挂什么科| 什么是假性银屑病| 月经三个月没来是什么原因| 外感风热是什么意思| 推背有什么好处和坏处| 黄芩有什么功效| 嗓子疼吃什么药见效最快| 犯困是什么原因| 扁桃体结石长什么样| 日落是什么时辰| 甲基硫菌灵治什么病| 的是什么意思| 缺钾是什么原因| 什么是翻新机| 大拇指有黑色竖纹是什么原因| s和m是什么意思啊| 每天头疼是什么原因引起的| 眼底出血是什么原因引起的| 鱼靠什么呼吸| 日本全称是什么| 金秘书为什么那样| 一路向北是什么意思| 吃氨糖有什么好处和坏处| 广州白云区有什么好玩的地方| 吃布洛芬有什么副作用| 九月24日是什么星座| 大德是什么意思| 组织部是干什么的| 佩字五行属什么| hiv弱阳性是什么意思| 怎么知道自己对什么过敏| 肝郁气滞吃什么中成药| 益生菌对人体有什么好处| 月经没来吃什么药可以催月经来| 肾阴虚吃什么药| 酸菜鱼里面加什么配菜好吃| 荔枝什么季节成熟| 子宫破裂有什么危险| 心肌缺血吃什么药最好| 那是什么| 小产可以吃什么水果| 盗汗是什么| 八方来财是什么意思| 蝗虫吃什么| 怀孕补铁吃什么| 毛泽东什么时候死的| 牛奶盒属于什么垃圾| 12月15日什么星座| 驴板肠是什么部位| bv是什么意思| 250为什么是骂人的话| 血糖高会有什么症状| 高大的什么| 96122是什么电话| 初级中学是什么意思| 归脾丸治什么病| vintage什么意思| mcm中文叫什么牌子| 有肝病的人吃什么好| 发财树用什么肥料最好| 激素是什么| 什么时候量血压最准确| 樱菜是什么菜| 什么转自如| 胃疼胃胀用什么药效果最好| 特异性是什么意思| dn是什么| 过敏性皮炎吃什么药| 什么大叫| 胃胀气是什么症状| 减肥吃什么玉米| 苦瓜泡水喝有什么功效和作用| 棉纶是什么面料| 瘦的人吃什么才能变胖| 什么是日间手术| 脚心发热是什么原因| 内膜薄是什么原因| 低密度脂蛋白是什么意思| 大熊猫为什么有黑眼圈| 尿毒症是什么原因导致的| 银花指什么生肖| 手代表什么生肖| 霍启刚家族做什么生意| 甲亢挂什么科室| 睡觉咬牙齿是什么原因引起的| 五年是什么婚| 子宫内膜囊性增生是什么意思| 哈气是什么意思| 岁贡生是什么意思| 女人被操是什么感觉| 医保卡有什么用| 什么地问填词语| 女性湿气重喝什么茶| 做梦吃屎有什么预兆| supreme是什么牌子| 四维彩超是检查什么| 重生什么意思| 张宇的老婆叫什么名字| 乌龟吃什么食物| 专一是什么意思| 总胆固醇什么意思| 29周岁属什么生肖| 精子是什么样的| 乳房疼挂什么科| 吃毓婷有什么副作用| 指甲表面凹凸不平是什么原因| 五福是什么生肖| smeg什么品牌| 非分之想是什么意思| 全国劳动模范有什么待遇| 无缘是什么意思| 路政属于什么单位| 领略是什么意思| 男人跑马是什么原因| 肺部结节灶是什么意思啊| esr医学上是什么意思| 什么可以消肿快的方法| 怀孕肚子胀是什么原因| 下巴肿大是什么原因| 财神是什么生肖| 眼睛模糊吃什么好| 同房为什么会出血| 梦见打台球是什么意思| 巴字加一笔是什么字| 减肥喝什么牛奶| 尿颜色很黄是什么原因| 桑蚕丝用什么洗最好| 天蝎男和什么星座最配| 抓拍是什么意思| 缺钙应该吃什么| 在水一方什么意思| 什么食物补锌| 沉默是什么意思| 老年痴呆吃什么药| 右手小指疼痛预兆什么| 什么食物利尿| 川芎治什么病最好| 为什么会突然得荨麻疹| 红花泡脚有什么好处| 血热吃什么药快速见效| 头顶疼是什么原因引起的| 肺热吃什么中成药| 百度
Table of Contents
2. Using awk for Field-Based Text Processing
Basic awk Syntax
3. Combining sed and awk
4. Practical Examples
Home System Tutorial LINUX How to Use `sed` and `awk` for Text Processing in Linux

韩国滑板女神不仅美 还是一本行走的穿搭指南

Jul 29, 2025 am 01:59 AM

百度 1月19日上午十时,由中央直属机关工委、中央国家机关工委、全国妇联指导,中央直属机关妇工委、中央国家机关妇工委、全国妇联宣传部主办的“树清廉家风·创最美家庭”专题活动在人民网在线直播,为即将到来的新年带来缕缕清新之风。

sed and awk are powerful text processing tools in Linux, suitable for efficient text operations under the command line, for log parsing, configuration file editing, and data conversion. 1. sed is used for stream editing, and can perform text replacement (such as s/old/new/replacement for the first time, s/old/new/g global replacement), operate on specific lines (such as 3s/old/new/only line 3, 2,5s/old/new/g lines 2 to 5), delete lines (/^$/d delete empty lines, /error/d delete lines containing error), insert or append text (/start/i\New line before insert before matching, /end/a\New line after matching line append after matching), use the -i option to directly modify the file. It is recommended to use other separators such as | to avoid "slash hell" when path replacement. 2. awk is good at field-based text processing. The basic syntax is awk 'pattern { action }' file.txt. The fields are separated by blank by default. The specified fields can be printed ({print $1, $3}), the delimiter is specified with -F (-F','process CSV, -F':'process passwd files), and the pattern filtering is supported ($2 == "active" prints the line with the second field active, /error/ matches the line with error and prints the line number NR), and can be calculated ({sum = $2} END {print "Total:", sum} sum), adds conditions and formatted output ($3 > 100 {print $1 " is over budget"}), built-in variables include NR (current line number), NF (current line field number), FILENAME (file name), such as {print NR ". " $0 " (fields: " NF ")"}. 3. Sed and awk can be used in combination to achieve complex processing through pipelines: first use sed to clean the text and then awk processing, such as sed 's/^\[ \\t\]\*//; s/\[ \\t\]\*$//' file.txt | awk '$2 > 50 {print $1}' and print the first field with the second field greater than 50; or awk extracts the field and appends the content, such as awk -F'|' '{print $2, $4}' data.txt | sed 's/$/ added/'. 4. Practical applications include: extracting the IP in the log and counting grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log | sort | uniq -c, and then filtering the IP accessed more than 100 times with awk '$1 > 100 {print $2}'; safely modify the configuration file sed -i.bak 's/^#\(Port 22\)/\1/' /etc/ssh/sshd_config uncomment Port 22 and back up the original file; formatting ps output ps aux | awk '$3 > 5.0 {print $1, $2, $3, $11}' | head -10 lists the top 10 processes that use more than 5% of the CPU. In short, sed is used when processing text streams, and awk is used when analyzing structured data. The combination of the two can greatly improve the efficiency of command line text processing.

How to Use `sed` and `awk` for Text Processing in Linux

sed and awk are two of the most powerful text-processing tools in Linux. They allow you to manipulate text efficiently from the command line, making them essential for log parsing, configuration file editing, and data transformation. Here's how to use them effectively.

How to Use `sed` and `awk` for Text Processing in Linux

1. Using sed for Stream Editing

sed (stream editor) is ideal for performing basic text transformations on an input stream (a file or input from a pipeline).

Common sed Operations

  • Substitute text
    Replace the first occurrence of a pattern on each line:

    How to Use `sed` and `awk` for Text Processing in Linux
     sed 's/old/new/' file.txt

    Replace all occurrences:

     sed 's/old/new/g' file.txt
  • Replace on specific lines
    Only replace on line 3:

    How to Use `sed` and `awk` for Text Processing in Linux
     sed '3s/old/new/' file.txt

    Replace in a range (lines 2 to 5):

     sed '2,5s/old/new/g' file.txt
  • Delete lines
    Delete blank lines:

     sed '/^$/d' file.txt

    Delete lines containing a pattern:

     sed '/error/d' file.log
  • Insert or append text
    Insert a line before a match:

     sed '/start/i\New line before' file.txt

    Append a line after a match:

     sed '/end/a\New line after' file.txt
  • Edit files in place
    Use -i to save changes directly:

     sed -i 's/foo/bar/g' config.txt

? Tip: Use a different delimiter (like | ) to avoid "slash hell" when working with paths:

 sed 's|/home/user|/tmp|g' file.txt

2. Using awk for Field-Based Text Processing

awk excels at processing structured text (like CSV or log files), where data is organized in fields.

Basic awk Syntax

 awk 'pattern { action }' file.txt
  • Print specific fields
    By default, fields are separated by whitespace. Print the first and third fields:

     awk '{print $1, $3}' data.txt
  • Use a custom delimiter
    For comma-separated values:

     awk -F',' '{print $2}' users.csv

    Or with a colon (eg, /etc/passwd ):

     awk -F':' '{print $1, $6}' /etc/passwd
  • Filter lines with patterns
    Print lines where the second field equals "active":

     awk '$2 == "active" {print $0}' status.txt

    Print lines containing the word "error":

     awk '/error/ {print NR, $0}' app.log
  • Perform calculations
    Sum values in the second column:

     awk '{sum = $2} END {print "Total:", sum}' numbers.txt
  • Add conditions and formatting

     awk '$3 > 100 {print $1 " is over budget"}' expenses.txt
  • Built-in variables

    • NR – Current record (line) number
    • NF – Number of fields in the current line
    • FILENAME – Name of the input file

    Example:

     awk '{print NR ". " $0 " (fields: " NF ")"}' data.txt

3. Combining sed and awk

You can pipe sed and awk together for advanced processing:

  • Clean up text with sed , then process with awk :

     sed 's/^[ \t]*//; s/[ \t]*$//' file.txt | awk '$2 > 50 {print $1}'

    (Removes leading/trailing whitespace, then prints first field if second field > 50)

  • Extract and reformat data:

     awk -F'|' '{print $2, $4}' data.txt | sed 's/$/ added/'

    (Prints fields 2 and 4, then appends " added" to each line)


4. Practical Examples

  • Extract IPs from a log and count occurences:

     grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log | sort | uniq -c

    Then use awk to filter suspicious ones:

     awk '$1 > 100 {print $2}' # IPs accessed more than 100 times
  • Modify configuration files safely:

     sed -i.bak 's/^#\(Port 22\)/\1/' /etc/ssh/sshd_config

    (Uncomments "Port 22" and creates a backup)

  • Format output from ps :

     ps aux | awk '$3 > 5.0 {print $1, $2, $3, $11}' | head -10

    (Lists top 10 processes with CPU > 5%)


Both tools are scriptable and can handle complex logic, but for quick command-line text manipulation, even simple one-liners save a lot of time. Start with basic substitutions and field printing, then build up as needed.

Basically, if you're editing text streams — use sed . If you're analyze or reporting on structured data — reach for awk .

The above is the detailed content of How to Use `sed` and `awk` for Text Processing in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Install LXC (Linux Containers) in RHEL, Rocky & AlmaLinux Install LXC (Linux Containers) in RHEL, Rocky & AlmaLinux Jul 05, 2025 am 09:25 AM

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

7 Ways to Speed Up Firefox Browser in Linux Desktop 7 Ways to Speed Up Firefox Browser in Linux Desktop Jul 04, 2025 am 09:18 AM

Firefox browser is the default browser for most modern Linux distributions such as Ubuntu, Mint, and Fedora. Initially, its performance might be impressive, however, with the passage of time, you might notice that your browser is not as fast and resp

How to troubleshoot DNS issues on a Linux machine? How to troubleshoot DNS issues on a Linux machine? Jul 07, 2025 am 12:35 AM

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

How would you debug a server that is slow or has high memory usage? How would you debug a server that is slow or has high memory usage? Jul 06, 2025 am 12:02 AM

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.

Install Guacamole for Remote Linux/Windows Access in Ubuntu Install Guacamole for Remote Linux/Windows Access in Ubuntu Jul 08, 2025 am 09:58 AM

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

How to Burn CD/DVD in Linux Using Brasero How to Burn CD/DVD in Linux Using Brasero Jul 05, 2025 am 09:26 AM

Frankly speaking, I cannot recall the last time I used a PC with a CD/DVD drive. This is thanks to the ever-evolving tech industry which has seen optical disks replaced by USB drives and other smaller and compact storage media that offer more storage

How to find my private and public IP address in Linux? How to find my private and public IP address in Linux? Jul 09, 2025 am 12:37 AM

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

How to Install NodeJS 14 / 16 & NPM on Rocky Linux 8 How to Install NodeJS 14 / 16 & NPM on Rocky Linux 8 Jul 13, 2025 am 09:09 AM

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

See all articles
中国劲酒有什么功效 指甲长出来是白色的什么原因 转氨酶高有什么症状 佐匹克隆片是什么药 海胆是什么动物
冬虫夏草为什么叫冬虫夏草 什么叫腺瘤 绿茶不能和什么一起吃 大男子主义是什么意思 什么时候测量血压最准确
胃疼什么原因 儿童水痘吃什么药 猪拉稀用什么药最快 天五行属什么 把妹什么意思
brunch是什么意思 普通感冒吃什么药 道德经适合什么人看 龙日冲狗要忌讳什么 五蕴皆空是什么意思
什么叫放射性疼痛hcv8jop5ns0r.cn 缺维生素会有什么症状hcv8jop3ns7r.cn 开业送什么好imcecn.com 什么是虫草hcv8jop4ns1r.cn 地图舌吃什么好得快hcv9jop3ns8r.cn
医院有什么科室hcv8jop0ns6r.cn 空气刘海适合什么脸型hcv8jop5ns5r.cn 豪五行属什么hcv9jop1ns6r.cn 恒心是什么意思hcv9jop0ns7r.cn 肠结核是什么病hcv8jop7ns5r.cn
11月15日出生是什么星座hcv8jop7ns5r.cn 什么叫suv车adwl56.com 碘是什么东西hcv8jop2ns9r.cn 为什么会得荨麻疹zhongyiyatai.com 世界上最毒的蜘蛛叫什么hcv9jop2ns6r.cn
下焦不通吃什么中成药hcv8jop1ns3r.cn 长方形纸能折什么tiangongnft.com 生快是什么意思hcv8jop4ns3r.cn 钟馗是什么意思hcv8jop3ns1r.cn 乡政府属于什么单位clwhiglsz.com
百度