我想使日期命令具有良好的格式,如下所示:
awk -F, '{system("date -d " $1 " +%d/%m/%Y %H:%M")}' file.log
但它不起作用
Try 'date --help' for more information.
date: extra operand ‘%H:%M’
正确的做法是什么,以便 date 命令获得一个正确的参数?为什么当我在“year”和“hour”之间加一个空格时会失败?
提前致谢
我想使日期命令具有良好的格式,如下所示:
awk -F, '{system("date -d " $1 " +%d/%m/%Y %H:%M")}' file.log
但它不起作用
Try 'date --help' for more information.
date: extra operand ‘%H:%M’
正确的做法是什么,以便 date 命令获得一个正确的参数?为什么当我在“year”和“hour”之间加一个空格时会失败?
提前致谢
我想让我的 (g)awk 脚本更具可移植性,尤其是对于 mawk,因为它是 Debian 的库存 awk。
我一直避免length(array)
使用诸如此类的东西,并将array[outer, inner]
其用于多维数组,而不是array[inner][outer]
(gawk 的真正多数组,而不是 SUBSEP hack)。
and()
但是现在我陷入了位运算符(例如and )的困境lshift()
,gawk 提供了这些运算符,而 mawk 没有。我该如何为 mawk 重新实现这些运算符,最好是不使用 shell?
维基百科(https://en.wikibooks.org/wiki/AWK#Bit_manipulation)只是说:
传统 awk 没有位运算符来提供位操作。但是,gawk 内置了位操作函数,包括 and()、or()、xor()、compl()、lshift() 和 rshift()。
有一个 Zsh 变量,其内容如下:
$if(description-meta)$
<meta name="description" content="$description-meta$" />
$endif$
<title>$if(title-prefix)$$title-prefix$ – $endif$$pagetitle$</title>
<style>
$styles.html()$
</style>
$for(css)$
<link rel="stylesheet" href="$css$" />
$endfor$
我需要删除 <title>
这一行,即:
<title>$if(title-prefix)$$title-prefix$ – $endif$$pagetitle$</title>
并覆盖该变量或创建一个新变量,使其变为:
$if(description-meta)$
<meta name="description" content="$description-meta$" />
$endif$
<style>
$styles.html()$
</style>
$for(css)$
<link rel="stylesheet" href="$css$" />
$endfor$
如何使用 awk
或 sed
来实现?
我正在尝试将以下内容添加到我的 .bashrc 中:
cpu_info() {
top -b -n1 | grep Cpu(s) | awk '{print $2 + $4}'
}
但 bash 告诉我:
bash: .bashrc: line 131: syntax error near unexpected token `('
bash: .bashrc: line 131: ` top -b -n1 | grep Cpu(s) | awk '{print $2 + $4}''
该命令在终端中运行良好,不知道为什么它会破坏我的 bash 脚本
有任何想法吗?
我使用此语法删除2.txt
to中的行1.txt
:
awk 'NR==FNR{a[$0]=1;next}!a[$0]' 2.txt 1.txt > lines.txt
我的行采用以下格式:
[email protected]:something
如果这行在两个文件中是相同的,我就lines.txt
没有这个,所以很好,但是如果[email protected]
相同的话我想删除行并忽略后面的单词:
。
我想自动注释掉PHP文件中的代码块,如下所示:
原始块:
// Enable all errors
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL);
带注释的新块:
/* For the production version, the following codelines are commented
out
// Enable all errors
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL);
*/
所以我打算将这些行放在两个文件中,并使用 sed 自动执行替换。然而,在网上搜索后,我只找到Replace string with content of a file using sed和sed - Replace string with file content,这意味着要么只有源模式或目标模式在一个文件中,其余的在网上。但文件中没有两者的样本。
那么,如何进行更换呢?我应该使用 sed 还是 awk?
有一个包含内容的文件
{
"first_name": "John",
"last_name": "Smith",
"is_alive": true,
"age": 27,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": "10021-3100"
},
"phone_numbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"Thomas",
"Trevor"
],
"spouse": null
}
如何使用 Busybox 实用程序将内容添加到文件最后一个 } 字符之前,以使文件内容如下所示?
{
"first_name": "John",
"last_name": "Smith",
"is_alive": true,
"age": 27,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": "10021-3100"
},
"phone_numbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"Thomas",
"Trevor"
],
"spouse": null,
"field1": "value1",
"field2": "value2",
"field3": "value3",
"field4": "value4"
}
但是,} 字符不一定是文件中的最后一个字符,也不一定位于最后一行。
到目前为止我只找到了这个解决方案
tac file2 | sed '0,/}/s/}/}\n"field4": "value4"\n"field3": "value3",\n"field2": "value2",\n"field1": "value1",\n,/' | tac>tmp_file && mv tmp_file file2
测试用例:(使用非 root 用户,因为 root 会忽略 000 权限...)
#in a clean directory:
[ -f file_1 ] && chmod 600 file_? # for repeat tests...
for i in file_1 file_2 file_3; do
printf 'A\nB\n' > "$i"
# we need at least 1 char : awk/gawk silently skips empty files...
done
chmod 000 file_2
awk '(FNR==1) { print FILENAME }' file_?
# tried with : regular (old unixes) awk on AIX. and gawk on Linux.
# the fatal "permission denied" on file_2 stops [g]awk.
有没有办法捕获该致命错误并继续处理下一个文件?
(否则我觉得这很令人担忧:对一堆文件使用 awk 并不能保证处理所有文件,因为如果其中任何一个文件不可读,它将致命地退出)
如果可以的话请:回答
简单而快速的问题。
我有这个
awk -f file # awk use file as input
awk '/word/' file # awk show me word
awk '/32$/' text # all words with 32..
我要这个
# awk use file as input
awk -f file
# awk show me word
awk '/word/' file
# all words with 32..
awk '/32$/' text
可以使用 awk 来做到这一点吗?也欢迎使用其他命令(perl、sed、python ..)