awk one lines

2023-05-16


From :http://www.student.northpark.edu/pemente/awk/awk1line.txt

HANDY ONE-LINERS FOR AWK 22 July 2003
compiled by Eric Pement <pemente@northpark.edu> version 0.22
Latest version of this file is usually at:
http://www.student.northpark.edu/pemente/awk/awk1line.txt


USAGE:

Unix: awk '/pattern/ {print "$1"}' # standard Unix shells
DOS/Win: awk '/pattern/ {print "$1"}' # okay for DJGPP compiled
awk "/pattern/ {print /"$1/"}" # required for Mingw32

Most of my experience comes from version of GNU awk (gawk) compiled for
Win32. Note in particular that DJGPP compilations permit the awk script
to follow Unix quoting syntax '/like/ {"this"}'. However, the user must
know that single quotes under DOS/Windows do not protect the redirection
arrows (<, >) nor do they protect pipes (|). Both are special symbols
for the DOS/CMD command shell and their special meaning is ignored only
if they are placed within "double quotes." Likewise, DOS/Win users must
remember that the percent sign (%) is used to mark DOS/Win environment
variables, so it must be doubled (%%) to yield a single percent sign
visible to awk.

If I am sure that a script will NOT need to be quoted in Unix, DOS, or
CMD, then I normally omit the quote marks. If an example is peculiar to
GNU awk, the command 'gawk' will be used. Please notify me if you find
errors or new commands to add to this list (total length under 65
characters). I usually try to put the shortest script first.

FILE SPACING:

# double space a file
awk '1;{print ""}'
awk 'BEGIN{ORS="/n/n"};1'

# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
# NOTE: On Unix systems, DOS lines which have only CRLF (/r/n) are
# often treated as non-blank, and thus 'NF' alone will return TRUE.
awk 'NF{print $0 "/n"}'

# triple space a file
awk '1;{print "/n"}'

NUMBERING AND CALCULATIONS:

# precede each line by its line number FOR THAT FILE (left alignment).
# Using a tab (/t) instead of space will preserve margins.
awk '{print FNR "/t" $0}' files*

# precede each line by its line number FOR ALL FILES TOGETHER, with tab.
awk '{print NR "/t" $0}' files*

# number each line of a file (number on left, right-aligned)
# Double the percent signs if typing from the DOS command prompt.
awk '{printf("%5d : %s/n", NR,$0)}'

# number each line of file, but only print numbers if line is not blank
# Remember caveats about Unix treatment of /r (mentioned above)
awk 'NF{$0=++a " :" $0};{print}'
awk '{print (NF? ++a " :" :"") $0}'

# count lines (emulates "wc -l")
awk 'END{print NR}'

# print the sums of the fields of every line
awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'

# add all fields in all lines and print the sum
awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'

# print every line after replacing each field with its absolute value
awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'
awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'

# print the total number of fields ("words") in all lines
awk '{ total = total + NF }; END {print total}' file

# print the total number of lines that contain "Beth"
awk '/Beth/{n++}; END {print n+0}' file

# print the largest first field and the line that contains it
# Intended for finding the longest string in field #1
awk '$1 > max {max=$1; maxline=$0}; END{ print max, maxline}'

# print the number of fields in each line, followed by the line
awk '{ print NF ":" $0 } '

# print the last field of each line
awk '{ print $NF }'

# print the last field of the last line
awk '{ field = $NF }; END{ print field }'

# print every line with more than 4 fields
awk 'NF > 4'

# print every line where the value of the last field is > 4
awk '$NF > 4'


TEXT CONVERSION AND SUBSTITUTION:

# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
awk '{sub(//r$/,"");print}' # assumes EACH line ends with Ctrl-M

# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
awk '{sub(/$/,"/r");print}

# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
awk 1

# IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
# Cannot be done with DOS versions of awk, other than gawk:
gawk -v BINMODE="w" '1' infile >outfile

# Use "tr" instead.
tr -d /r <infile >outfile # GNU tr version 1.22 or higher

# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
awk '{sub(/^[ /t]+/, ""); print}'

# delete trailing whitespace (spaces, tabs) from end of each line
awk '{sub(/[ /t]+$/, "");print}'

# delete BOTH leading and trailing whitespace from each line
awk '{gsub(/^[ /t]+|[ /t]+$/,"");print}'
awk '{$1=$1;print}' # also removes extra space between fields

# insert 5 blank spaces at beginning of each line (make page offset)
awk '{sub(/^/, " ");print}'

# align all text flush right on a 79-column width
awk '{printf "%79s/n", $0}' file*

# center all text on a 79-character width
awk '{l=length();s=int((79-l)/2); printf "%"(s+l)"s/n",$0}' file*

# substitute (find and replace) "foo" with "bar" on each line
awk '{sub(/foo/,"bar");print}' # replaces only 1st instance
gawk '{$0=gensub(/foo/,"bar",4);print}' # replaces only 4th instance
awk '{gsub(/foo/,"bar");print}' # replaces ALL instances in a line

# substitute "foo" with "bar" ONLY for lines which contain "baz"
awk '/baz/{gsub(/foo/, "bar")};{print}'

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"
awk '!/baz/{gsub(/foo/, "bar")};{print}'

# change "scarlet" or "ruby" or "puce" to "red"
awk '{gsub(/scarlet|ruby|puce/, "red"); print}'

# reverse order of lines (emulates "tac")
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file*

# if a line ends with a backslash, append the next line to it
# (fails if there are multiple lines ending with backslash...)
awk '///$/ {sub(///$/,""); getline t; print $0 t; next}; 1' file*

# print and sort the login names of all users
awk -F ":" '{ print $1 | "sort" }' /etc/passwd

# print the first 2 fields, in opposite order, of every line
awk '{print $2, $1}' file

# switch the first 2 fields of every line
awk '{temp = $1; $1 = $2; $2 = temp}' file

# print every line, deleting the second field of that line
awk '{ $2 = ""; print }'

# print in reverse order the fields of every line
awk '{for (i=NF; i>0; i--) printf("%s ",i);printf ("/n")}' file

# remove duplicate, consecutive lines (emulates "uniq")
awk 'a !~ $0; {a=$0}'

# remove duplicate, nonconsecutive lines
awk '! a[$0]++' # most concise script
awk '!($0 in a) {a[$0];print}' # most efficient script

# concatenate every 5 lines of input, using a comma separator
# between fields
awk 'ORS=%NR%5?",":"/n"' file



SELECTIVE PRINTING OF CERTAIN LINES:

# print first 10 lines of file (emulates behavior of "head")
awk 'NR < 11'

# print first line of file (emulates "head -1")
awk 'NR>1{exit};1'

# print the last 2 lines of a file (emulates "tail -2")
awk '{y=x "/n" $0; x=$0};END{print y}'

# print the last line of a file (emulates "tail -1")
awk 'END{print}'

# print only lines which match regular expression (emulates "grep")
awk '/regex/'

# print only lines which do NOT match regex (emulates "grep -v")
awk '!/regex/'

# print the line immediately before a regex, but not the line
# containing the regex
awk '/regex/{print x};{x=$0}'
awk '/regex/{print (x=="" ? "match on line 1" : x)};{x=$0}'

# print the line immediately after a regex, but not the line
# containing the regex
awk '/regex/{getline;print}'

# grep for AAA and BBB and CCC (in any order)
awk '/AAA/; /BBB/; /CCC/'

# grep for AAA and BBB and CCC (in that order)
awk '/AAA.*BBB.*CCC/'

# print only lines of 65 characters or longer
awk 'length > 64'

# print only lines of less than 65 characters
awk 'length < 64'

# print section of file from regular expression to end of file
awk '/regex/,0'
awk '/regex/,EOF'

# print section of file based on line numbers (lines 8-12, inclusive)
awk 'NR==8,NR==12'

# print line number 52
awk 'NR==52'
awk 'NR==52 {print;exit}' # more efficient on large files

# print section of file between two regular expressions (inclusive)
awk '/Iowa/,/Montana/' # case sensitive


SELECTIVE DELETION OF CERTAIN LINES:

# delete ALL blank lines from a file (same as "grep '.' ")
awk NF
awk '/./'


CREDITS AND THANKS:

Special thanks to Peter S. Tillier for helping me with the first release
of this FAQ file.

For additional syntax instructions, including the way to apply editing
commands from a disk file instead of the command line, consult:

"sed & awk, 2nd Edition," by Dale Dougherty and Arnold Robbins
O'Reilly, 1997
"UNIX Text Processing," by Dale Dougherty and Tim O'Reilly
Hayden Books, 1987
"Effective awk Programming, 3rd Edition." by Arnold Robbins
O'Reilly, 2001

To fully exploit the power of awk, one must understand "regular
expressions." For detailed discussion of regular expressions, see
"Mastering Regular Expressions, 2d edition" by Jeffrey Friedl
(O'Reilly, 2002).

The manual ("man") pages on Unix systems may be helpful (try "man awk",
"man nawk", "man regexp", or the section on regular expressions in "man
ed"), but man pages are notoriously difficult. They are not written to
teach awk use or regexps to first-time users, but as a reference text
for those already acquainted with these tools.

USE OF '/t' IN awk SCRIPTS: For clarity in documentation, we have used
the expression '/t' to indicate a tab character (0x09) in the scripts.
All versions of awk, even the UNIX System 7 version should recognize
the '/t' abbreviation.

#---end of file---
 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

awk one lines 的相关文章

  • 使用 AWK 或 SED 以及以下标签以逗号分隔

    使用 AWK 或 SED 以及下面使用的标签以逗号分隔 BEGIN AccountID BEGIN CallerID BEGIN Billed Account Attributes 1111111 1111111 1111111 END B
  • awk 比较多个文件

    我有2个文件 file1 1 apple 2 mango 3 banana 44 orange file2 1 apple 22 31 xyz 2 man 3 banana 44 oran 44 orange 我需要使用第 1 列和检查第
  • awk: hping: 打印 icmp 发起/接收之间的差异

    我有以下输出hping http ports su net hping在 OpenBSD 上 hping icmp ts www openbsd org HPING www openbsd org re0 129 128 5 194 icm
  • 仅当重复行与模式匹配时才删除它们

    这个问题 https stackoverflow com questions 1444406 how can i delete duplicate lines in a file in unix有一个很好的答案说你可以使用awk seen
  • awk 子串单个字符

    这是columns txt aaa bbb 3 ccc ddd 2 eee fff 1 3 3 g 3 hhh i jjj 3 kkk ll 3 mm nn oo 3 我可以找到第二列以 b 开头的行 awk if substr 2 1 1
  • git 别名中的 AWK 语句

    我正在尝试创建一个 git 别名来以特定格式打印日志中的所有拉取请求 但是 我在使用 AWK 删除双空格时遇到问题 这是使用以下命令的 git log 的输出 git log merges grep pull request pretty
  • awk/Unix 分组依据

    有这个文本文件 name age joe 42 jim 20 bob 15 mike 24 mike 15 mike 54 bob 21 试图得到这个 计数 joe 1 jim 1 bob 2 mike 3 Thanks awk F NR
  • 使用 awk 读取文件并搜索另一个文件

    我正在读取一个文件 每行都有数字 我需要搜索另一个满足这些数字和其他条件的文件 我正在尝试使用 awk 来做到这一点 但遇到了问题 File a txt 1476 1477 1497 现在我需要打印第 12 列包含这些数字的行以及第 3 列
  • 用于计算数据文件中数字列表的平均值的脚本

    文件data txt包含以下内容 1 00 1 23 54 4 213 2 3 4 脚本的输出应该是 ave 54 646 一些简单的脚本是首选 这是一种方法 awk s 1 END print ave s NR RS file ave 5
  • awk 返回两个变量

    现在这就是我正在做的事情 ret ls la awk print 3 9 usr echo ret awk print 1 fil echo ret awk print 2 问题是我没有运行ls我正在运行一个需要时间的命令 因此您可以理解其
  • 带变量的 AWK 负正则表达式

    我在 bash 脚本中使用 awk 来比较两个文件以获取不匹配的行 我需要将第二个文件的所有三个字段 作为一个模式 与第一个文件的所有行进行比较 第一个文件 chr1 9997 10330 HumanGM18558 peak 1 150 1
  • 使用 BASH 和 AWK 创建 HTML 表

    我在创建 html 表来显示文本文件中的统计信息时遇到问题 我确信有 100 种方法可以做得更好 但这里是 以下脚本中的注释显示了输出 bin bash function getapistats curl s http api exampl
  • 从文件中删除包含非英语 (Ascii) 字符的行

    我有一个文本文件 其中包含来自不同语言的字符 例如 中文 拉丁文等 我想删除包含这些非英语字符的所有行 我想包含所有英文字符 a b 数字 0 9 和所有标点符号 我如何使用 awk 或 sed 等 unix 工具来完成此操作 Perl 支
  • 删除目录中的文件,其中匹配模式的数量低于Linux上的阈值[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我怎样才能删除所有 txt目录中的文件 其中grep gt wc l小于2 Example file1 txt gt BB gt AA
  • awk 反转行和单词

    我对编程语言之类的东西很陌生 所以我必须用 awk 反转文件中的所有行以及这些行中的所有单词并将其打印出来 要反转的 File1 aa bb cc foo 做为 File1 的输出打印应该是这样的 就像 foo 一样 cc bb aa 我在
  • sed:多行的条件合并

    我需要根据模式使用 sed 合并文件行 例如 输入文件 X1 A B C D E F G H I J 1 LK T PP E OO 2 X2 DDF F Y J W Q 预期输出 X1 A B C D E F G H I J 1 LK T
  • 使用 SED 将单词的第一个字母大写

    如何将单词的第一个字母替换为大写字母 例如 Trouble me Gold rush brides into Trouble Me Gold Rush Brides 这一行应该做到这一点 sed e s b u 1 g
  • 如何 grep 文件中不区分大小写的字符串?

    我有一个文件file1其结尾为Success OR success 我想要grep为了这个词success以一种不区分大小写的方式 我写了以下命令 但它区分大小写 cat file1 grep success 我怎样才能改变它 以便它ret
  • 选择fasta文件中氨基酸超过300个且“C”出现至少4次的序列

    我有一个包含蛋白质序列的 fasta 文件 我想选择超过 300 个氨基酸且半胱氨酸 C 氨基酸出现超过 4 次的序列 我使用此命令来选择具有超过 300 个 aa 的序列 cat 72hDOWN fasta fasta bioawk c
  • 如何使用 Bash 编写二进制文件?

    我的问题是我需要创建一个包含以下确切字节的文件 48 00 49 00 我不能使用C perl 其他脚本语言 目标是嵌入式设备 我使用 awk 尝试过 在桌面上它确实有效 awk BEGIN printf c c c c 48 00 49

随机推荐

  • Ubuntu自学笔记二

    磁盘管理 dev sd 文件 xff0c 此类文件是磁盘设备文件 xff0c 并不能直接访问磁盘 xff0c 必须要将磁盘挂载到某一个目录下才可以访问 dev sdb和 dev sdb1是U盘的设备文件 xff08 每个人的电脑U盘设备文件
  • 机器视觉设计,如何正确的选择相机和镜头?

    1 相机选择步骤 xff1a 目标物尺码 61 预估实际视场 0 75 根据精度算出分辨率 xff0c 预计出的实际视场 项目要求精度 61 相机的分辨率 根据相机分辨率大小 xff0c 选择合适的相机 xff0c 如果分别率一样的情况下
  • VCC、VDD、VSS以及VBAT的区别

    在STM32 的学习中 xff0c 发现有几种看起来相关的名称 xff0c 分别是VCC VDD VSS VBAT xff0c 在经过搜索查找之后 xff0c 总结如下 xff1a 1 VCC的C是Circuit的意思 xff0c 是指整个
  • vue中单个js写法

    1 vue export default 可以写很多东西 xff0c 包括变量和方法 xff0c 对象等 xff0c 只要是想作为开放的接口都可以写 xff0c 在 vue文件中一般写上data 以及method等 xff0c data指的
  • Ubuntu14.04安装Theano详细教程

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 因为最近需要学习深度学习 xff0c 因此想要配置Theano xff0c 来开发深度学习算法 但是发现Theano安装总是出
  • Ubuntu-安装-cuda7.0-单显卡-超详细教程

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 一 说明 本教程是在台式机上安装的 xff0c 只有一个NVIDIA显卡 操作系统是Ubuntu 14 04 64bit 双显
  • 使用Putty无法远程登录,显示服务器拒绝连接

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com putty连接不到linux的原因总结为以下几种情况 局域网内的两台电脑IP冲突 当使用DHCP自动分配IP时 xff0c 两
  • caffe安装系列——综述

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • ls改头换面

    在linux中 xff0c ls命令可以使用颜色来区别不同的文件 路径 权限等等 但是有时候 xff0c ls的颜色配置不是非常合适 例如在黑色的背景下显示蓝色的文字 xff0c 看起来真是费劲啊 要修改颜色配置非常简单 xff0c 可以修
  • caffe安装系列——安装GCC4.7和G++4.7并降级

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • caffe安装系列——安装NVIDIA显卡驱动

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • caffe安装系列——安装cuda和cudnn

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • caffe安装系列——安装Matlab

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • caffe安装系列——安装OpenCV

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • caffe安装系列——安装python依赖包

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • caffe安装系列——安装caffe

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 说明 网上关于caffe的安装教程非常多 xff0c 但是关于每一步是否操作成功 xff0c 出现了什么样的错误又该如何处理没
  • 服务器维护系列——VNC没有反应了怎么办?

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com 服务器维护系列 服务器维护系列 VNC没有反应了怎么办 xff1f 问题描述 服务器上存在多个用户 xff0c 大家通过VNC
  • PCL系列——读入PCD格式文件

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • PCL系列——将点云数据写入PCD格式文件

    博客新址 http blog xuezhisd top 邮箱 xff1a xuezhisd 64 126 com PCL系列 PCL系列 读入PCD格式文件操作PCL系列 将点云数据写入PCD格式文件PCL系列 拼接两个点云PCL系列 从深
  • awk one lines

    From http www student northpark edu pemente awk awk1line txt HANDY ONE LINERS FOR AWK 22 July 2003 compiled by Eric Peme