标签: awk

为什么使用双引号将 awk 的操作语句括起来与使用单引号将它们括起来会产生不同的结果?

如果我使用单引号将 awk 的 action 语句括起来,它会按预期工作:

$ dpkg -l | grep "linux\-[a-z]*\-" | awk {'print $2'}
linux-headers-3.13.0-27
linux-headers-3.13.0-27-generic
linux-headers-3.14.4-031404
linux-headers-3.14.4-031404-generic
linux-headers-generic
linux-image-3.13.0-27-generic
linux-image-3.14.4-031404-generic
linux-image-extra-3.13.0-27-generic
linux-image-generic
linux-libc-dev:i386
linux-sound-base
Run Code Online (Sandbox Code Playgroud)

但是如果我使用双引号将它们括起来,它会打印整行而不是特定的列/字段:

$ dpkg -l | grep "linux\-[a-z]*\-" | awk {"print $2"}
ii  linux-headers-3.13.0-27                    3.13.0-27.50                                           all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-27-generic            3.13.0-27.50                                           i386         Linux kernel headers for version 3.13.0 on 32 bit x86 SMP
ii  linux-headers-3.14.4-031404                3.14.4-031404.201405130853                             all          Header files related to Linux kernel version 3.14.4
ii  linux-headers-3.14.4-031404-generic        3.14.4-031404.201405130853                             i386 …
Run Code Online (Sandbox Code Playgroud)

command-line awk

8
推荐指数
1
解决办法
6662
查看次数

如何删除代码块中的行号?

通常在网页中发布的代码包含行号。当我将代码复制并粘贴到文本编辑器中时(尽管一些文本编辑器会自动删除行号,但有时它不起作用),删除行号会很累。

一定有一些sed、awk或者其他linux解析工具来处理。

假设我有以下代码,如何使用上述工具安全地删除行号?

   1 /*
   2  * Beep using pcm
   3  *
   4  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
   5  *
   6  *   This program is free software; you can redistribute it and/or modify
   7  *   it under the terms of the GNU General Public License as published by
   8  *   the Free Software Foundation; either version 2 of the License, or
   9  *   (at your option) any later version.
  10  *
  11  *   This program is distributed in …
Run Code Online (Sandbox Code Playgroud)

sed awk

7
推荐指数
1
解决办法
5026
查看次数

在 Ubuntu 14.04 上选择 awk 版本

我使用Ubuntu 14.04,并大量使用所有伟大的命令awksedsort等等。

最近我注意到Ubuntumawk用作默认实现,但由于我想使用特定于 的某些功能gawk,我立即apt-get install编辑了该包。

在这一点上,我的 Ubuntu 似乎切换到gawk默认设置,这是我不想要的,因为它在行为上有一些细微的差异(例如,rand()似乎是确定性的gawk,这很糟糕,因为我一直在使用它)。

如何安装两个软件包,但mawk用作默认实现?

configuration awk 14.04

7
推荐指数
1
解决办法
5205
查看次数

我需要安装 awk 还是它内置在 Ubuntu 中?

我是 Linux 操作系统的新手,正在学习移动计算。我必须使用awk. 我正在使用 Ubuntu 14.04。

我需要安装awk还是内置在 Ubuntu 中?

awk 14.04

7
推荐指数
1
解决办法
2万
查看次数

按日期拆分日志文件

我有一个具有这种日志格式的日志文件:

###<Aug 8, 2016 11:59:05 PM>
different text
...
different text
###<Aug 15, 2016 9:10:55 AM>
different text
...
...
...
different text
###<Aug 22, 2016 10:02:17 PM>
different text
...
...
...
...
different text
###<Sep 1, 2016 1:00:01 AM>
different text
###<Sep 7, 2016 3:00:01 PM>
different text
...
...
different text
Run Code Online (Sandbox Code Playgroud)

如何按日期将此日志文件拆分为文件 YYYY_MM_DD.log?

sed awk text-processing

7
推荐指数
3
解决办法
1709
查看次数

如何打印 awk 中的最后 5 个字段?

我有 10 个字段,我想从第 5 个字段开始到第 10 个字段并忽略前 5 个字段。我如何在 awk 中使用 NF 来做到这一点?

f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
Run Code Online (Sandbox Code Playgroud)

我只想显示:

f6 f7 f8 f9 f10
c6 c7 c8 c9 c10
Run Code Online (Sandbox Code Playgroud)

command-line bash awk text-processing

7
推荐指数
2
解决办法
3万
查看次数

替换文件中包含撇号的模式

我有一个包含这一行的文本文件:

set stime = '0 0'
Run Code Online (Sandbox Code Playgroud)

我需要将其更改为:

set stime = '0 4'
Run Code Online (Sandbox Code Playgroud)

我试过这些命令:

sed -i 's/original/new/g' file.txt
Run Code Online (Sandbox Code Playgroud)

awk '{gsub(/pattern/,"replacement")}' file 
Run Code Online (Sandbox Code Playgroud)

但它们不起作用。我想这是因为模式中的撇号。我怎样才能正确地做到这一点?

command-line sed awk text-processing

7
推荐指数
2
解决办法
3732
查看次数

awk:仅打印特定保护线模式之后的行

考虑这个文件:

#!/usr/bin/env bash
cat > example_file.txt <<EOL
|dms,wew,qwqw|
|m|head1|
|3,4,6|
|3e,2,23|
|m|head2|
|xw,12,2123|
|23,d,213|
|23,22q,2skl|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|
EOL
Run Code Online (Sandbox Code Playgroud)

每隔一段时间,就会有一个保护模式:一行以:

|m|
Run Code Online (Sandbox Code Playgroud)

(是的,文件的第一行不需要是保护模式)现在我只想打印最后一个保护模式的那些行:

|m|head1|
Run Code Online (Sandbox Code Playgroud)

并忽略所有其他行。在其他最坏的情况下,我希望输出为:

#!/usr/bin/env bash
cat > desired_result_file.txt <<EOL
|m|head1|
|3,4,6|
|3e,2,23|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|
EOL
Run Code Online (Sandbox Code Playgroud)

command-line awk

7
推荐指数
1
解决办法
486
查看次数

如何使用 awk/sed 组织无序的列?

我有 2 列,包括一些数据,例如:

one      two
one two
one    two
Run Code Online (Sandbox Code Playgroud)

我们如何将其转换为:

one two
one two
one two
Run Code Online (Sandbox Code Playgroud)

command-line sed awk

7
推荐指数
1
解决办法
588
查看次数

为什么这个在 CentOS 上运行的 awk 脚本不能在 Ubuntu 上运行?

(作为参考,代码也可能在这里:https : //github.com/garyexplains/examples ,这可能是比视频更好的来源。)


CentOS 上的这个简单脚本有什么理由不能在 Ubuntu 上运行吗?

[nsaunders@rolly awk]$ 
[nsaunders@rolly awk]$ awk -f loop.awk numbers.txt 
1 2 3 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 9 10 11 12 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
1 2 3 4 5 6 7 8 …
Run Code Online (Sandbox Code Playgroud)

bash scripts awk text-processing

7
推荐指数
2
解决办法
1895
查看次数

标签 统计

awk ×10

command-line ×5

sed ×4

text-processing ×4

14.04 ×2

bash ×2

configuration ×1

scripts ×1