因此 vimdiff 是一个针对 cli 环境的文件比较工具。我想知道它是否提供在两个选项卡之间交换代码行的功能?
Sollosa's questions
我想使用 ansible 输出一些以制表符作为分隔符的文本
这是任务片段
- name: Create output file
blockinfile:
block: |
Some text\tmore text
path: '{{ playbook_dir }}/output.txt'
create: true
电流输出
# BEGIN ANSIBLE MANAGED BLOCK
Some text\tmore text
# END ANSIBLE MANAGED BLOCK
期望的
# BEGIN ANSIBLE MANAGED BLOCK
Some text more text
# END ANSIBLE MANAGED BLOCK
该剧本获取总 RAM,并在此基础上设置两个变量,例如 A 和 B A 是总 RAM 的 40% B 是总 RAM 的 60%
任务是,如果 A 大于或等于 32,则 A 必须为 32,但如果小于 32,则保留 A,因为总 RAM 的 40% 与 B 相同,如果 B 大于或等于32,则 B 必须为 32,但如果小于 32,则保留 B,因为是总 RAM 的 60%
- name: get total RAM
set_fact:
total_ram_mb: "{{ ansible_memtotal_mb }}"
- name: calculate min & max values
set_fact:
A: "{{ (total_ram_mb * 0.4 / 1024) | int }}"
B: "{{ (total_ram_mb * 0.6 / 1024) | int }}"
- debug:
var: A
- debug:
var: B
- name: limit min value
set_fact:
A: "{{ 32 if A >= 32 else A }}"
- name: limit max value
set_fact:
B: "{{ 32 if B >= 32 else B }}"
- debug:
var: A
- debug:
var: B
但后来的任务AB总是设置为32,无论A更大、等于还是更小,我的虚拟机上的内存都是8G不知道问题出在哪里
这是该剧的输出
[root@autom ansible]# ansible-playbook -i inv.txt memory.yml
PLAY [es] ********************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [es1]
ok: [es2]
TASK [memory : get total RAM] *****************************************************************************************************************
ok: [es1]
ok: [es2]
TASK [memory : calculate min & max values] ****************************************************************************************************
ok: [es1]
ok: [es2]
TASK [memory : debug] *************************************************************************************************************************
ok: [es1] => {
"A": "3"
}
ok: [es2] => {
"A": "3"
}
TASK [memory : debug] *************************************************************************************************************************
ok: [es1] => {
"B": "4"
}
ok: [es2] => {
"B": "4"
}
TASK [memory : limit min value] ***************************************************************************************************************
ok: [es1]
ok: [es2]
TASK [memory : limit max value] ***************************************************************************************************************
ok: [es1]
ok: [es2]
TASK [memory : debug] *************************************************************************************************************************
ok: [es1] => {
"A": "32"
}
ok: [es2] => {
"A": "32"
}
TASK [memory : debug] *************************************************************************************************************************
ok: [es1] => {
"B": "32"
}
ok: [es2] => {
"B": "32"
}
我有2个目录
$ tree dir{1..2}
dir1
├── file1
└── file2
dir2
├── file1
└── file2
我想使用 ansible 将 dir1 中的所有文件与 dir2 中的所有文件进行比较,并像这样打印差异
output:
${file1} from ${dir1}:
diff content
${file1} from ${dir2}:
diff content
它将遍历所有文件以打印它们的差异
下面是需要修改的ansible片段
---
- name: Compare files in two directories
hosts: localhost
gather_facts: false
tasks:
- name: Find files in directory 1
find:
paths: ~/dir1
file_type: file
register: dir1_files
- name: Find files in directory 2
find:
paths: ~/dir2
file_type: file
register: dir2_files
- name: Compare files
shell: diff dir1/file1 dir2/file1 ## how can I make sure path changes but filenames stay same using variables
loop: "{{ dir1_files.files }}"
register: diff_output
changed_when: false
failed_when: diff_output.rc == 1
- name: Print differences
debug:
msg: |
{{ item.item.path }} from dir1:
{{ item.stdout }}
{{ item.item.path }} from dir2:
{{ item.stdout }}
loop: "{{ diff_output.results }}"
when: item.stdout_lines | length > 0
对于 Vladimir 的回答中的建议代码,我得到以下输出
TASK [debug] *****************************************************************************************************************************************
ok: [localhost] => {
"msg": "file2 from dir1: \n 1,2c1,2\n < abc123\n < def456\n ---\n > abc101\n > def111\nfile2 from dir2: \n 1,2c1,2\n < abc123\n < def456\n ---\n > abc101\n > def111\nfile1 from dir1: \n 1,2c1,2\n < 123abc\n < 456def\n ---\n > 101abc\n > 111def\nfile1 from dir2: \n 1,2c1,2\n < 123abc\n < 456def\n ---\n > 101abc\n > 111def\n"
}
我有一个数据库主机,上面有一些 sql 文件。我想使用 ansible 剧本将其来源到数据库中。所以我找到所有文件并在变量中注册,然后尝试使用以下代码获取这些文件。
- name: get schema files
find:
paths: "~/dbs/"
recurse: no
patterns: "*.sql"
register: db_sql_files
- name: import schemas
mysql_db:
name: all
state: import
target: "{{ item['path'] }}"
login_user: "{{ db_user }}"
login_password: "{{ db_pass }}"
with_items: "{{ db_sql_files['files'] }}"
当我运行剧本时,出现以下错误。
The task includes an option with an undefined variable. The error was: 'item' is undefined
我有主机,我可以在其中在单节点架构或分布式架构上设置应用程序。所以我有一个库存。
[STG]
node1
[LIVE]
app_node
db_node
gateway_node
因此具有默认值的变量是single
但可以在 CLI 上更改为distributed
我有一个角色定义
- hosts:
gather_facts: no
roles:
- {role: setup, tags: ['setup', 'orchestra']}
所以我希望主机线根据地图值是动态的
- hosts: 'if single then host == STG else LIVE'
我有主文件,可以启动不同的个人角色。
---
- hosts: all
gather_facts: False
roles:
- role: "{{ rnames }}"
...
当我运行个人角色时,它运行良好
ansible-playbook -i inv initiate.yml -e rnames=install_packages
但我更喜欢通过向rname
变量提供角色列表来运行多个角色
ansible-playbook -i inv initiate.yml -e rnames=install_packages,config_merge
我尝试了with_items
&loop
构造,但似乎无法弄清楚。
我正在运行一个处理特定目录中文件的守护进程。我想一一处理。所以我的想法是我将在守护进程目录中逐个复制文件,并尾随其日志,当日志文件首先完成处理时,第二个文件应该复制到守护进程目录,即日志文件停止更新。
我怎样才能做到这一点?说
daemon_dir=/var/daemon_dir/
daemon_log_file=/var/daemon/daemon_log
我有 5 个文件要处理,即文件 {1..5}
# ls -1
file1
file2
file3
file4
file5
所以我的循环应该按原样进行。
for file in file{1..5}
do
cp ${file} ${daemon_dir}
# daemon sees new file arriving in its dir, & starts processing
# log is tailed
tail -f ${daemon_log_file}
# when log file stops updating say for 1minute or specified time, tail quits
done
并且循环从下一个文件开始。
我有 2 个文件,当它们的列匹配时,尝试将从 file2 获取的列值设置为 file1
file1:
signup||20200620|A3332|en|LA||ACTIVE
signup||20200620|B4443|en|CA|66001|ACTIVE
signup||20200620|C2221|en|WC||ACTIVE
signup||20200620|D1110|en|LA||ACTIVE
signup||20200620|E5554|en|WC|66003|ACTIVE
file2:
A3332||99001
B4443|66001|99003
D1110|66004|99007
E5554||99004
标准: file1 的 $4 必须匹配 file2 的 $1,匹配后它应该设置 file1 的 $7 等于 file2 的 $3
期望:
signup||20200620|A3332|en|LA|99001|ACTIVE
signup||20200620|B4443|en|CA|99003|ACTIVE
signup||20200620|C2221|en|WC||ACTIVE
signup||20200620|D1110|en|LA|99007|ACTIVE
signup||20200620|E5554|en|WC|99004|ACTIVE
我正在尝试什么
awk 'BEGIN{ FS=OFS="|" } NR==FNR{a[NR]=$1; b[NR]=$3; next} {if (a[FNR] = $4); $7=b[FNR]; print}' file2 file1
我得到了什么
signup||20200620|A3332|en|LA|99001|ACTIVE
signup||20200620|B4443|en|CA|99003|ACTIVE
signup||20200620|C2221|en|WC|99007|ACTIVE
signup||20200620|D1110|en|LA|99004|ACTIVE
signup||20200620|E5554|en|WC||ACTIVE
如果这是逻辑错误或其他原因,我会摸不着头脑
我有一个巨大的文件要解析并需要搜索和替换文本,但在特定字段中,共享一个名为 dest 的小样本以供参考。第一行是标题供参考。
cat dest
ID|NAME|COMPANY|NUMBER
1001|Adam||15001
1002|eve|adam&eve|15002
1003|||
1004|||50000
1005|||50001
我有单独的文件,其中包含要匹配的模式、要替换的文本和要替换的文本
cat src
1003||15003
1004|50000|15004
1005|50001|15005
所以我可以使用 sed 运行下面给定的 while 循环来实现 src 文件中的最后 2 行。
cat src | while IFS=$'|'; read id old new; do sed -i "/^${id}/s/${old}/${new}/" dest; done
但是对于ID=1003
我得到空字符串$old
,它将替换该ID的dest文件中的所有空列。我想避免。我想要做的是只替换最后一个字段。
期望:
ID|NAME|COMPANY|NUMBER
1001|Adam||15001
1002|eve|adam&eve|15002
1003|||15003
1004|||15004
1005|||15005
当涉及到柱状数据时,我可以使用 awk,因为它更细化。但据我了解,awk 会多次打印到标准输出,这在我的情况下也不实用。
那么有没有一种方法可以让我以简洁明了的方式做到这一点?
我想构建一个函数,该函数将根据其参数更改其用户输入提示。
my_fucntion
db_host
在提示用户输入后取 1 个参数:
function provide_host () {
echo "Enter NAME OR IP OF ${function_param1} DATBASE HOST: "
read global function_param1_db_host
}
所以如果我把这个函数称为
function provide_host (primary)
它应该提示为
echo "Enter NAME OR IP OF PRIMARY DATBASE HOST: "
但如果我使用
function provide_host (secondary)
它提示
"Enter NAME OR IP OF SECONDARY DATBASE HOST: "
我的想法是我必须为此使用一个if
语句,但我不确定是否可以将函数的参数用作在函数内部提示用户的变量。
文件1:
G A 4
H B 3
I C 2
J D 1
文件2:
M H 6
N H 5
O K 4
P J 3
Q I 2
R I 1
S G 0
T L 1
期望:
M H 6
N H 5
P J 3
Q I 2
R I 1
S G 0
仅打印 file2 中的 $2 与 file1 中的 $1 匹配的行
我正在尝试构造 if 语句,但它给了我下面的语法错误
awk 'NR==FNR{a[NR]=$1; b[NR]=$2; next} ; {if{a[FNR]==b[FNR]}; {next}; { print }' file1 file2
syntax error at source line 1
context is
NR==FNR{a[NR]=$1; b[NR]=$2; next} ; >>> if <<< {a[FNR]==b[FNR]; next}; { print }
或者如果不使用 if 动词则没有输出
awk 'NR==FNR{a[NR]=$1; b[NR]=$2; next} ; {a[FNR]==b[FNR]; next}; { print }' file1 file2
我有一个管道分隔文件
d1000|1000
d1001|100
d1002|10
d1003|1
d1004|
d1005|
如果长度小于 4 位,我想修改 $2,并且保留空格
所以试图通过 awk 脚本来做
BEGIN { FS="|"; OFS="\t" }
{
n=1100
{ if (length($2)!=4 && length($2)>0) {$2=++n}};
print $1, $2
}
但它一遍又一遍地打印相同的数字
d1000 1000
d1001 1101
d1002 1101
d1003 1101
d1004
d1005
而想要的输出
d1000 1000
d1001 1101
d1002 1102
d1003 1103
d1004
d1005
编辑:这是上面的代码,格式为gawk -o-
:
BEGIN {
FS = "|"
OFS = "\t"
}
{
n = 1100
if (length($2) != 4 && length($2) > 0) {
$2 = ++n
}
print $1, $2
}
我有这样的文件
d1000 1000
d1001 100
d1002 10
d1003 1
我想修改长度不等于 4 的第二列。但我只想打印已修改的行,因此第 2 列中的原始文本保留在第 2 列中,并且更改打印在第 3 列中,并增加一个数字。
期望的输出:
d1001 100 1101
d1002 10 1102
d1003 1 1103
我正在尝试这些方面,但无法获得语法或结果
awk -v n=1100 '((length($2)!=4 && length($2)>0) {new=($2=++n)}; {print $1, $2, new}' file
我有非标准数据,我想对其进行标准化
文件:
d101 11001
e101 9665
f101 9663
d102 11002
e102 11003
f102 11004
g102 11005
所需的输出:
d101 11001
e101 12001
f101 12002
d102 11002
e102 11003
f102 11004
g102 11005
所以逻辑应该是,如果 column2 = 4 的长度,它应该用提供的系列的增量编号替换它:在这种情况下,1200 是系列,& 1、2、3 .. 是增量。
我有一个包含值的文件,有些是整数,有些是小数
# cat file
value1 100
value2 500.10
value3 2505
value4 35.4
我想将 field2 中的每个值除以一个固定的数字 = 1000,也将除法后的所有值相加。结果值必须在小数点后保留 5 位数字
# cat output.txt
value1 100 0.10000
value2 500.10 0.50010
value3 2505 2.50500
value4 35.4 0.03450
sum 3140.50000 3.14050
我有 2 个文件,第一行是标题行供参考
file1
userId userContact parentId parentContact 200 0900200 100 - 201 0900201 100 - 300 0900300 101 -
file2
userId userContact parentId parentContact 100 0900100 100 - 101 0900101 100 -
我想填充 的第 4 列file1
,以便它必须搜索in $3
of ,然后将from代替 of ,
因此输出应如下所示:file1
$1
file2
$2
file2
$4
file1
output
userId userContact parentId parentContact 200 0900200 100 0900100 201 0900201 100 0900100 300 0900300 101 0900101
我更喜欢awk
,因为它会更快。
我有 2 个包含列表的文件。第 1 列是用户 ID,第 2 列是关联值
# cat file1
e3001 75
n5244 30
w1453 500
#cat file2
d1128 30
w1453 515
n5244 30
e3001 55
要考虑的事情。
- userIds 可能不会在两个文件中完全排序
- userId 的数量可能因文件而异
必需的
- 首先,file1:column1 中的 userId 必须与 file2:column1 中的 UserId 匹配
- 接下来将它们在 file1:column2 中的值与 file2:column2 进行比较
- 打印值有差异的地方。如果有的话,还有额外的用户 ID
输出:
e3001 has differnece, file1 value: 75 & file2 value: 55
w1453 has differnece, file1 value: 500 & file2 value: 515
d1128 is only present in filename: file1|file2
欢迎使用 1liner-awk 或 bash 循环的解决方案
我正在尝试循环,但它在吐垃圾,猜想有一些错误的逻辑
#!/usr/bin/env bash
## VARIABLES
FILE1=file1
FILE2=file2
USERID1=(`awk -F'\t' '{ print $1 }' ${FILE1}`)
USERID2=(`awk -F'\t' '{ print $1 }' ${FILE2}`)
USERDON1=(`awk -F'\t' '{ print $2 }' ${FILE1}`)
USERDON2=(`awk -F'\t' '{ print $2 }' ${FILE2}`)
for user in ${USERID1[@]}
do
for (( i = 0; i < "${#USERID2[@]}"; i++ ))
#for user in ${USERID2[@]}
do
if [[ ${USERID1[$user]} == ${USERID2[i]} ]]
then
echo ${USERID1[$user]} MATCHES BALANCE FROM ${FILE1}: ${USERDON1[$i]} WITH BALANCE FROM ${FILE2}: ${USERDON2[$i]}
else
echo ${USERID1[$user]}
fi
done
done
下面是从 linux 框中复制的文件。它是制表符分隔的,但据我所知,awk 也可以与制表符一起使用。
#cat file1
e3001 55
n5244 30
w1453 515
我有 2 个数组要同时在 bash 脚本中处理。第一个数组包含某种标签。第二个数组包含值,如下所示
LABELS=(label1 label2 label3 labe4 )
VALUES=(91 18 7 4)
需要的是:一个循环,它将回显 LABELS 数组 & 中的索引项,并在 VALUES 数组中该项的相应值前面,如下所示
label1 91
label2 18
label3 7
label4 4
我猜嵌套循环不起作用,我在下面尝试过,但它不会按语法工作
for label in {LABELS[@]} && value in {VALUES[@]}
do
echo ${label} ${value}
done