小编mon*_*onk的帖子

在 awk 命令中使用 `NF`

我试图了解两个命令之间的区别(我期待这两个命令的结果相同):

案例一

echo 'one,two,three,four,five' |awk -v FS=,  '{NF=3}1'
one two three
Run Code Online (Sandbox Code Playgroud)

案例二

echo 'one,two,three,four,five' |awk -v FS=,  -v NF=3 '{$1=$1}1'
one two three four five
Run Code Online (Sandbox Code Playgroud)

这是我目前的理解: $1=$1用于强制 awk 重构并使用定义的变量。我正在分配FSlike-v FS=","这实际上与-v NF=3 .

问:为什么NF=3在那里没有生效FS=,

awk

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

如果值或两个连续行与列不匹配,如何打印分隔符

我输入如下,如果两行之间的第三列的值不同,我需要在行之间放置一个分隔符.

one two three four
five six three seven
eight nine ten elevel
alpha beta ten gama
tango charlie oscar bla
Run Code Online (Sandbox Code Playgroud)

预期结果:

one two three four
five six three seven
=
eight nine ten elevel
alpha beta ten gama
=
tango charlie oscar bla
Run Code Online (Sandbox Code Playgroud)

这是我认为会起作用但不是.

awk '{col3=$3;next} $3!=col3{$0="\n="$0;print $0}' input
Run Code Online (Sandbox Code Playgroud)

bash awk

5
推荐指数
3
解决办法
129
查看次数

如何处理来自 ansible 角色的错误

我一直在调用具有多个角色的剧本,每个角色都代表一个测试用例。如果角色之一在执行过程中失败,我需要运行剧本而不会失败。我正在使用ignore_errors: yes. 但是,这确实忽略了错误,我需要在最后打印失败的角色的名称?是否可以 ?

- hosts: WEB
  gather_facts: no
  vars:
  roles:
        - { role:  CHECK_CONNECTION, ignore_errors: yes, tags: always }
        - { role:  CHECK_CPU,ignore_errors: yes, tags: always }
        - { role:  CHECK_MEM,ignore_errors: yes, tags: always }
Run Code Online (Sandbox Code Playgroud)

问题:如何执行整个剧本并最终打印失败的角色?

ansible

5
推荐指数
1
解决办法
2085
查看次数

在ansible中从until循环打印自定义消息

我正在尝试多次运行命令并检查输出中是否包含某些字符串(“hi”)。我有目的地模拟失败并期望until循环失败。到目前为止一切都很好。

现在,我需要一些自定义消息来说明until循环或task失败的原因。例如:"Your command failed to print hi"

所以问题是,如果循环在重试中失败,如何从直到循环打印自定义消息。

剧本:

-->cat until.yml
---

- hosts: localhost
  gather_facts: no
  tasks:

    - name: "check command"
      shell: echo hello
      register: var
      until: var.stdout.find('hi') != -1
      retries: 5
      delay: 1
Run Code Online (Sandbox Code Playgroud)

剧本输出:

 -->ansible-playbook until.yml
PLAY [localhost] *************************************************************************************************************************************************************************************************************************

TASK [check command] ********************************************************************************************************************************************************************************************************
FAILED - RETRYING: who triggered the playbook (5 retries left).
FAILED - RETRYING: who triggered the playbook (4 retries left).
FAILED - RETRYING: who triggered the playbook …
Run Code Online (Sandbox Code Playgroud)

ansible

5
推荐指数
1
解决办法
4259
查看次数

使用正则表达式提取两个块之间的文本

我正在尝试使用以下正则表达式提取两个字符串之间的文本。

(?s)Non-terminated Pods:.*?in total.\R(.*)(?=Allocated resources)
Run Code Online (Sandbox Code Playgroud)

perl这个正则表达式在 regex101 中看起来很好,但在与或 一起使用时不知何故不会打印 pod 详细信息grep -P。以下命令会产生空输出。

kubectl describe  node |perl -le '/(?s)Non-terminated Pods:.*?in total.\R(.*)(?=Allocated resources)/m; printf "$1"'
Run Code Online (Sandbox Code Playgroud)

这是示例输入:

PodCIDRs:                     10.233.65.0/24
Non-terminated Pods:          (7 in total)
  Namespace                   Name                                        CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                        ------------  ----------  ---------------  -------------  ---
  default                     foo                                         0 (0%)        0 (0%)      0 (0%)           0 (0%)         105s
  kube-system                 nginx-proxy-kube-worker-1                   25m (1%)      0 (0%)      32M (1%)         0 (0%)         9m8s
  kube-system                 nodelocaldns-xbjp8                          100m (5%)     0 (0%) …
Run Code Online (Sandbox Code Playgroud)

regex perl grep

4
推荐指数
1
解决办法
171
查看次数

如何在 tcsh if 语句中使用 OR 条件

我试图在 tcsh shell 的 IF 语句中使用 OR 条件。同样的语句也适用于 CSH。

示例语句:(cat tesh.sh)

if [ "$1" == "hi" -o "$2" == "hello" ];then
echo hi
else
echo hello
fi
Run Code Online (Sandbox Code Playgroud)

现在,如果我执行此示例脚本,我会收到以下错误:

[35] % sh -x test hi hi hello
+ test hi hi hello
test[7]: hi: A test command parameter is not valid.
+ exit 1
[36] % sh -x test hi hi
+ test hi hi
test[7]: test: Specify a parameter with this command.
+ exit 1
[37] % sh -x test …
Run Code Online (Sandbox Code Playgroud)

unix shell if-statement tcsh

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

在当前行中使用上一行进行数学运算

我有以下示例数据:

co1 co2 co3
100 200 300
110 230 310
125 235 320
Run Code Online (Sandbox Code Playgroud)

我需要对数据执行以下逻辑:

(col2 - col2 of the previous line)/(col1 - col1 of previous line) (col3-col3 of the previous line)/(col1 - col1 of previous line) 
Run Code Online (Sandbox Code Playgroud)

翻译成:

(230-200)/(110-100) (310-300)/(110-100)
(235-230)/(125-110) (320-310)/(125-110)
Run Code Online (Sandbox Code Playgroud)

我试过:

cat sample |awk 'NR>1' |'NR>1{f=$1;s=$2;t=$3;next} {print f,s;print $2-s/$1-f,$3-t/$1-f}'
Run Code Online (Sandbox Code Playgroud)

期望的输出:

3   1
0.3 0.6
Run Code Online (Sandbox Code Playgroud)

使用此逻辑输出将减少一行,因为第二行将消耗第一行。

以下数据出错:

 104937 20776001011 6893034089
 104937 20776001011 6893034089
 104938 20776062501 6893040119
 104938 20776062501 6893040119
 104938 20776062501 6893040119
 104938 20776062501 6893040119
 104938 20776062501 6893040119
 104939 20776124802 …
Run Code Online (Sandbox Code Playgroud)

bash awk

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

sscanf 读取直到字符串末尾

我需要将字符串分成两部分,字符串的第一列是第一部分,字符串的其余部分是第二部分。第一部分需要存储在 中first_str,第二部分需要存储在 中rest_str

我正在使用sscanf来实现结果,当sentence[]不包含任何文字时,我设法通过以下示例获得所需的输出\n

简而言之,我需要知道格式说明符直到输入字符串的末尾。到目前为止,我能够让它工作直到\n看到,但不能再使用它了。有人可以帮助阅读直到看到字符串末尾而不是直到\n看到。

这是按比例缩小的示例:

#include <stdio.h>

int main ()
{
  char sentence []="abc@xyz.com foo bar foobar\nhey there";
  char first_str [200];
  char rest_str [200];

  //sscanf (sentence,"%s %99[^\0]",first_str,rest_str);
  sscanf (sentence,"%s %99[^\n]",first_str,rest_str);

  printf ("first column is %s\nevertyhing else is %s\n",first_str,rest_str);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

期望的结果:

first column is abc@xyz.com
evertyhing else is foo bar foobar\nhey there
Run Code Online (Sandbox Code Playgroud)

或者

first column is abc@xyz.com
evertyhing else is foo bar foobar
hey …
Run Code Online (Sandbox Code Playgroud)

c scanf

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

如何从用户定义的 ansible.cfg 设置 ansible_config 变量

我有一个目录结构,在其中保存我的剧本,如下所示:

/home/monk/
    |_____Ansible_work
               |_____[ansible.cfg]
               |_____[playbook_dir_1]
               |                  |_______playbook_1.yml
               |_____[playbook_dir_2]
               |                  |_______playbook_2.yml
               |_____[playbook_dir_3]
               |                  |_______playbook_3.yml
               |_____[playbook_dir_4]
               |                  |_______playbook_4.yml
               |_____[inventory]
                     |___[inventory_1]
                     |___[inventory_2]
Run Code Online (Sandbox Code Playgroud)

我目前正在从目录执行我的剧本,如下所示ansible_work

ansible-playbook -i inventory/inventory_1 playbook_dir_1/playbook_1.yml
Run Code Online (Sandbox Code Playgroud)

或者

ansible-playbook -i inventory/inventory_1 playbook_dir_1/playbook_2.yml
Run Code Online (Sandbox Code Playgroud)

或者

ansible-playbook -i inventory/inventory_2 playbook_dir_1/playbook_1.yml
Run Code Online (Sandbox Code Playgroud)

每个剧本都需要引用 ansible.cfg 中设置的一些变量log_path,例如role_path等。一切正常。

现在我被告知,我应该使配置足够灵活,以便在给定清单和剧本的完整路径时可以从任何位置执行这些剧本。(这似乎是公平的要求)但由于我的剧本引用了 ansible.cfg 的本地副本,因此如果我从 dir 之外的任何点触发执行,则不会设置此本地 ansible.cfg 中设置的内容/home/monk/ansible_work/

默认情况下 Ansible.cfg 查找优先级如下:

* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)                 <--------------I am currently using this
* .ansible.cfg (in the home directory)                   <--------------Cannot use this …
Run Code Online (Sandbox Code Playgroud)

ansible

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

perl oneliner 在同一打印语句中打印数组和其他匹配项

我有一组包含示例数据的文件,如下所示,我需要转换数据以将所有对象文件(以下)放在-o第一列中,将链接库(以下)-l放在第二列中。这个格式在整个make输出中是一致的。

hello there -o one two three four -labc -lfoo -lbar
something useless -o abc doo zoo  -lkoo -lfoo -lmoo
Run Code Online (Sandbox Code Playgroud)

我正在尝试将其解析为更简单的格式以进行进一步处理:

one two three four, abc foo bar
abc doo zoo, koo foo moo
Run Code Online (Sandbox Code Playgroud)

我正在尝试这个,显然这不是我想要得到的:

perl  -ne '/-o(.*?)-/m; @libs = /-l([^ ]+)/gs; printf "%s %s\n", $1 , join(", ", @libs);' inputfile
bar
 abc, foo, bar

moo
 koo, foo, moo
Run Code Online (Sandbox Code Playgroud)

在这里,我尝试将所有对象存储到数组中$1并将所有库存储到@libs数组中。只有库被正确打印,但对象不正确,有人可以帮助修复它吗?我单独验证了它$1是否具有正确的值。

perl  -wne '/-o(.*?)-/m;  printf "%s %s\n", $1, " "' inputfile …
Run Code Online (Sandbox Code Playgroud)

regex perl

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

标签 统计

ansible ×3

awk ×3

bash ×2

perl ×2

regex ×2

c ×1

grep ×1

if-statement ×1

scanf ×1

shell ×1

tcsh ×1

unix ×1