显然,运行:
perl -n -e 'some perl code' *
Run Code Online (Sandbox Code Playgroud)
或者
find . ... -exec perl -n -e '...' {} +
Run Code Online (Sandbox Code Playgroud)
(与-p
代替相同-n
)
或者
perl -e 'some code using <>' *
Run Code Online (Sandbox Code Playgroud)
经常在本网站上发布的 one-liners 中发现,具有安全隐患。这是怎么回事?如何避免?
我有一个awk
脚本,new.awk
:
BEGIN { FS = OFS = "," }
NR == 1 {
for (i = 1; i <= NF; i++)
f[$i] = i
}
NR > 1 {
begSecs = mktime(gensub(/[":-]/, " ", "g", $(f["DateTime"])))
endSecs = begSecs + $(f["TotalDuration"])
$(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
}
{ print }
Run Code Online (Sandbox Code Playgroud)
我在 shell 中调用它
awk new.awk sample.csv
Run Code Online (Sandbox Code Playgroud)
...但我可以看到终端中的变化。如何在文件中进行就地更改,例如使用时sed -i
?
是否可以使用gawk
's-i inplace
选项并将内容打印到stdout
?
例如,如果我想更新一个文件,并且如果有任何更改,则将文件名和更改后的行打印到stderr
我可以执行以下操作
find -type f -name 'myfiles' -exec gawk -i inplace '{if(gsub(/pat/, "repl")) { print FILENAME > "/proc/self/fd/2" ; print > "/proc/self/fd/2"; } print;}' {} +
Run Code Online (Sandbox Code Playgroud)
但是有没有办法使用stdout
,或者更简洁的方式将该块打印到备用流?
我有一个keyvalue.txt
包含内容的文本文件:
one=abc
two=def
three=ghi
four=jkl
five=mno
six=pqr
Run Code Online (Sandbox Code Playgroud)
现在,我想附加xyz
到three
将成为的值
three=ghixyz
Run Code Online (Sandbox Code Playgroud)
生成的文件内容应该是:
one=abc
two=def
three=ghixyz
four=jkl
five=mno
six=pqr
Run Code Online (Sandbox Code Playgroud)
有没有办法在shell脚本中做到这一点?
我有一个包含 1200 万行的 csv 文件,格式如下:
mcu_i,INIT,200,iFlash, 11593925, 88347,,0x00092684,r,0x4606b570, ok,, 32,single,op-c,0,, 0, 0, 0,
mcu_i,INIT,200,iFlash, 11593931, 88348,,0x00092678,r,0x28003801, ok,, 32,single,op-c,0,, 0, 0, 0,
Run Code Online (Sandbox Code Playgroud)
我想使用以下逻辑根据第六列的值删除行: if (value >= X AND value <= Y ) => 删除行
我使用 gawk 找到了一个解决方案:
gawk -i inplace -F ',' -v s="$start_marker" -v e="$end_marker" '!($6 <= e && $6 >= s)' myfile.csv
Run Code Online (Sandbox Code Playgroud)
但这需要太长时间,我想要另一个性能更好的解决方案。
谢谢
我有大量的txt文件。每个txt文件的格式与此类似
200 0.2 0.1 0.5 0.4
500 0.4 0.9 0.9 0.1
Run Code Online (Sandbox Code Playgroud)
我试图删除每个 txt 文件中第一个字段值大于 400 的每一行。因此上面的文件现在应该只包含以下内容:
200 0.2 0.1 0.5 0.4
Run Code Online (Sandbox Code Playgroud)
代码
for file in *.txt; do
echo "$(awk '{ if ($1 < 401) print }' *.txt)" > tmp && mv tmp *.txt
done
rm -f tmp
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它将所有文件移动到下一个文本文件。