美文网首页
AWK Cookbook

AWK Cookbook

作者: 夜空最亮的9星 | 来源:发表于2018-09-28 14:35 被阅读15次

AWK Cookbook Kiran Gangadharan

Contents
1 Recipes 1
1.1 Countlinesinafile........................ 1
1.2 Countnoofwordsinafile.................... 1
1.3 Findsumofcolumnsforeachrow ............... 1
1.4 Findandreplaceinfile...................... 2
1.5 Removeduplicatelinesfromafile................ 2
1.6 Printalllinescontaining"Programming". . . . . . . . . . . . 2
1.7 Prefixinglinewithincreasingnumber. . . . . . . . . . . . . . 2
1.8 Print account name and role from /etc/passwd . . . . . . . . 2
1.9 Find the line containing largest value in the first column . . . 2
1.10Swapfirsttwocolumnsofeveryline .............. 2
1.11 Delete second column from every line . . . . . . . . 
1.12 Perform search similar to SQL ’where’ . . . . . . . 
1.13 Delete leading whitespace from each line . . . . . . 
1.14 Delete trailing whitespace from each line . . . . . . 
1.15 Replace all instances of "foo" with "bar" . . . . . .

1 Recipes
1.1 Count lines in a file
awk ’END { print NR }’
1.2 Count no of words in a file
awk ’{ total = total + NF } END { print total }’
1.3 Find sum of columns for each row
awk ’{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}’
1.4 Find and replace in file
# Replace only first instance in line
awk ’{sub(/HTML/, "html")}; 1’ data.txt > data_new.txt # Replace all instances in line
awk ’{gsub(/HTML/, "html")}; 1’ data.txt > data_new.txt
1.5 Remove duplicate lines from a file
awk ’!a[$0]++’ data.txt
1.6 Print all lines containing "Programming"
awk ’/Programming/ { n++ }; END { print n+0 }’
1.7 Prefixing line with increasing number
awk ’{print FNR ". " $0}’ data.txt
1.8 Print account name and role from /etc/passwd
awk -F : ’/^[^(#|_)]/ {print $1, "=>", $5}’ /etc/passwd
1.9 Find the line containing largest value in the first column
awk ’$1 > max {max=$1; maxline=$0}; END{ print maxline}’ data.txt
1.10 Swap first two columns of every line
awk ’{temp=$1; $1=$2; $2=temp} 1’ data.txt 2
1.11 Delete second column from every line
awk ’{ $2 = ""; print }’ data.txt
1.12 Perform search similar to SQL ’where’
awk ’$2 == "hello"’ data.txt
1.13 Delete leading whitespace from each line
awk ’{sub(/^[ \t]+/, ""); print}’
1.14 Delete trailing whitespace from each line
awk ’{sub(/[ \t]+$/, ""); print}’
1.15 Replace all instances of "foo" with "bar"
awk ’{gsub(/foo/,"bar");print}’

相关文章

  • AWK Cookbook

    AWK Cookbook Kiran Gangadharan

  • Python 字典排序

    Python 字典排序 《Python3 CookBook》[https://python3-cookbook.r...

  • 18-文本处理三剑客之awk

    本章内容 ◆ awk介绍◆ awk基本用法◆ awk变量◆ awk格式化◆ awk操作符◆ awk条件判断◆ aw...

  • 2017 09-04 AWK

    本章主要学习内容awk介绍 awk基本用法 awk变量 awk格式化 awk操作符 awk条件判断 a...

  • 【技术案例】跟老男孩学运维-awk项目案例

    一个awk数组应用案例 [TOC] 0.技术点: awk awk数组 awk判断 awk数组赋值 awk函数spl...

  • awk

    awk:报告生成器,格式化文本输出 内容: awk介绍 awk基本用法 awk变量 awk格式化 awk操作符 a...

  • awk

    Linux System Environment awk功能 awk格式 awk 参数 一、awk截取列 二、显示...

  • 笔记-awk

    1.Awk基础介绍 2.awk语法格式 2.Awk工作原理 3.Awk内部变量 4.Awk格式输出 5.Awk模式...

  • awk用法详解

    awk 用法 awk ' pattern {action} ' 1、awk '/101/' file ...

  • Day64-shell编程_正则表达式_awk

    1.Awk基础介绍 2.Awk工作原理 3.Awk内部变量 4.Awk格式输出 5.Awk模式匹配 5.1符号 ...

网友评论

      本文标题:AWK Cookbook

      本文链接:https://www.haomeiwen.com/subject/wxkpoftx.html