小编jia*_*ian的帖子

“cat << EOF | grep”在 bash 中如何工作?

这个答案中,How does "cat << EOF" work in bash? 在 Stack Overflow 上,我得到了前两点。但我没有得到第三点Pass multi-line string to a pipeline in Bash

  1. 将多行字符串传递到 Bash 中的管道

     $ cat <<EOF | grep 'b' | tee b.txt
     foo
     bar
     baz
     EOF
    
    Run Code Online (Sandbox Code Playgroud)

因为它有 3 个单词,2 个管道字符。然后我不知道如何解释它。

bash pipe io-redirection here-document

10
推荐指数
2
解决办法
5490
查看次数

${pasteargs// /- } 参数扩展是什么意思?

上下文:https ://stackoverflow.com/a/47348104/15603477

\n
printf -v pasteargs %*s 16\npaste -d\\  ${pasteargs// /- } < <(seq 1 42)\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32\n33 34 35 36 37 38 39 40 41 42\n
Run Code Online (Sandbox Code Playgroud)\n
\n

Paste -d, --delimiters=LIST 重用 LIST 中的字符而不是\n制表符

\n
\n

${参数/模式/字符串}

\n
\n

该模式被扩展以产生与文件名扩展中一样的模式。参数被扩展,模式与其值的最长匹配被替换为字符串。匹配\n根据下面描述的规则执行(请参阅模式匹配)。如果\npattern以 \xe2\x80\x98/\xe2\x80\x99 开头,则所有匹配的pattern 都将替换为\n字符串。

\n
\n

经说明书检查后。

\n …

bash shell-script

6
推荐指数
1
解决办法
321
查看次数

awk NR%2{}1 含义

我试图理解这段代码:

awk 'NR%2{printf "%s ",$0;next;}1' yourFile
Run Code Online (Sandbox Code Playgroud)

现在我尝试定制它。

鉴于此 error.txt 内容:

KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
Run Code Online (Sandbox Code Playgroud)

然后:

KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1
Run Code Online (Sandbox Code Playgroud)

... 将返回:

1KEY 4048:1736 string 3
3KEY 0:1772 string 1
5KEY 4192:1349 string 1
7KEY 7329:2407 string 2
9KEY 0:1774 string 1
Run Code Online (Sandbox Code Playgroud)

我猜NR%2是指偶数行号,但我不确定它1指的是什么。
如果没有1 …

awk

3
推荐指数
1
解决办法
2277
查看次数

如何使用 awk 在文件中间添加一行文本?

这是一个很好的答案。 /sf/answers/471752921/ 但我还是有点困惑。

没有a变量,awk '/^nameserver/ { printf("nameserver 127.0.0.1\n")} {print}' file2将得到:

# Generated by NetworkManager
domain dhcp.example.com
search dhcp.example.com
nameserver 127.0.0.1
nameserver 10.0.0.1
nameserver 127.0.0.1
nameserver 10.0.0.2
nameserver 127.0.0.1
nameserver 10.0.0.3
Run Code Online (Sandbox Code Playgroud)

尝试了几种组合后,我发现我必须使用 awk '/^nameserver/ && !a { printf("nameserver 127.0.0.1\n"); a=1 } {print}' file2

但我仍然对停止重复的功能!a感到困惑。a=1printf("nameserver 127.0.0.1\n")

awk

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

config find 命令排除某些目录

如何配置以永久排除find命令的某些目录。 /sf/ask/294702971/ 我尝试将以下别名添加到 bashrc

alias find='find -not \( -path ./.git -prune \)'
Run Code Online (Sandbox Code Playgroud)

似乎不起作用。

在 ripgrep 中你可以配置它。https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md#configuration-file 那么如何一次完成所有配置 make find 排除某些目录(如 git)。

bash

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

awk 打印从第 n 个到最后一个的所有列

https://www.gnu.org/software/gawk/manual/gawk.html 基于第 1.1.2 节 1.1.2 在没有输入文件的情况下运行 awk 我可以理解 awk '{print $0}' marks.txt 它就像 echo 一样。它会返回

1)  Amit    Physics  80
2)  Rahul   Maths    90
3)  Shyam   Biology  87
4)  Kedar   English  85
5)  Hari    History  89
Run Code Online (Sandbox Code Playgroud)

/sf/ask/207314481/?answertab=votes#tab-top

以下代码也按预期工作。

awk '{print $0}' marks.txt  | awk '{print $2 "\t" $3 "\t"$4}'
Run Code Online (Sandbox Code Playgroud)

我不明白 awk '{$1=""; print $0}' marks.txt即使现在直觉上是有道理的。

awk

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

使用 sed 仅显示匹配行之后的下一行

/sf/answers/521603491/

我明白grep -A1 'blah' logfile | grep -v "blah"

但我不明白sed -n '/blah/{n;p;}' logfile
到目前为止我检查了手动 sed 命令行选项页面: https://www.gnu.org/software/sed/manual/html_node/Command_002dLine-Options.html。唯一的-n办法就是安静。显然/blah/是指模式/blah/然后很难理解的部分是{n;p;}

sed

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

awk 从字符串中提取数字

有多个相关问题,似乎它们不能用来awk解决问题。

echo "blah foo123bar234blah" | egrep -o '([0-9]+)' 
Run Code Online (Sandbox Code Playgroud)

回报

123
234 
Run Code Online (Sandbox Code Playgroud)

echo "blah foo123bar234blah" | 
    awk '{ match($0,/([0-9]+)/,m); print m[0], m[1],m[2]}'    
Run Code Online (Sandbox Code Playgroud)

返回123 123

echo "blah foo123bar234blah" | 
    awk '{ match($0,/([0-9]+).+([0-9]+)/,m); print m[0], m[1],m[2]}'    
Run Code Online (Sandbox Code Playgroud)

回报 123bar234 123 4

手册中,在match(string, regexp [, array])部分中,示例是:

echo foooobazbarrrrr |
    gawk '{ match($0, /(fo+).+(bar*)/, arr); print arr[1], arr[2]}'
Run Code Online (Sandbox Code Playgroud)

返回foooo barrrrr.

那么如何使用 awk (相当于grep -o)从字符串中提取多个数字?

awk

0
推荐指数
1
解决办法
7312
查看次数

bash 声明时带有额外的逗号

Bash 中的“声明”是什么?

declare -r is_debug true
if [ "$is_debug" = true ]; then
    printf "%s\n", "is debug"
else
    printf "%s\n", "is not debug"
fi

declare -p is_debug
echo "${is_debug-isunset}"
Run Code Online (Sandbox Code Playgroud)

输出:

jian@jian:~/Desktop/pg_src/src4/postgres$ bash /home/jian/scratch/scratchpads/47a6066b9dfaca1c2b13ea2e99f4e929/scratch.sh
is not debug
,declare -r is_debug
isunset
jian@jian:~/Desktop/pg_src/src4/postgres$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, …
Run Code Online (Sandbox Code Playgroud)

bash

0
推荐指数
1
解决办法
51
查看次数

标签 统计

awk ×4

bash ×4

here-document ×1

io-redirection ×1

pipe ×1

sed ×1

shell-script ×1