在无的情况下分配替代值(Assigning a substitute value in case of nil)
我有几个变量:
name age address phone social_security email weight
还有一个名为
personal_details
的数组,其中每个值都位于0-6位置。所以我赋值如下:
name = personal_details[0] address = personal_details[1] phone = personal_details[2] social_security = personal_details[3] email = personal_details[4] weight = personal_details[5]
然而,在某些情况下,右侧的数据不存在。
对于数组中的每个元素,写这样的东西比什么更好的方法来处理?
if !personal_detail[0].nil? name = personal_details[0] else name = "" end if !personal_detail[1].nil? address = personal_details[1] else address = "" end
I have a few variables:
name age address phone social_security email weight
And an array called
personal_details
with each of these values in positions 0-6.So I assign values like this:
name = personal_details[0] address = personal_details[1] phone = personal_details[2] social_security = personal_details[3] email = personal_details[4] weight = personal_details[5]
In some cases, however, the data on the right hand side does not exist.
What's a more elegant way to handle this than writing something like this for each element in the array?
if !personal_detail[0].nil? name = personal_details[0] else name = "" end if !personal_detail[1].nil? address = personal_details[1] else address = "" end
原文:https://stackoverflow.com/questions/1736408
更新时间:2023-10-16 07:10
最满意答案
这个
awk
应该做:multipath -ll | awk 'NR>1 {r=f=0;for (i=1;i<=NF;i++) if ($i~/ready/) r++; else if ($i~/faulty/) f++;split($5,a,"=|]");print $3,a[2]"\tTotal: "r+f" paths, active: "r,"failed: "f}' RS="mpath" OFS=", " dm-28, 200G Total: 5 paths, active: 4, failed: 1 dm-39, 10G Total: 4 paths, active: 4, failed: 0
This
awk
should do:multipath -ll | awk 'NR>1 {r=f=0;for (i=1;i<=NF;i++) if ($i~/ready/) r++; else if ($i~/faulty/) f++;split($5,a,"=|]");print $3,a[2]"\tTotal: "r+f" paths, active: "r,"failed: "f}' RS="mpath" OFS=", " dm-28, 200G Total: 5 paths, active: 4, failed: 1 dm-39, 10G Total: 4 paths, active: 4, failed: 0
相关问答
更多-
这对我有用: for i in `awk -F":" '{print $6}' /etc/passwd | sort | grep /home`; do echo "Home Directory:" $i echo "Disk Space Used (KB):" `du -s $i |cut -f1` echo "" done 如果你在/etc/passwd中有一些你不感兴趣的东西,你可能想要或需要改进它。 This works for me: for i ...
-
这是使用GNU awk的一种方式。 运行如下: awk -f script.awk awk.script内容: BEGIN { printf "Process output of:\n" command0="tail -f -n 0 /web/*/logs/*.log" command1="vmstat" command2="free" command3="ps -eo pcpu,pid,user,tty,args" printf "\n>%s:\n\n ...
-
使用awk遍历unix命令nm和通过多个文件求和输出(Using awk to iterate unix command nm and sum output through multiple files)[2023-01-20]
你需要把读取的$ filename放一会儿; 做; 完成循环并将整个循环的输出提供给awk。 例如 while read filename ; do nm ... $filename done | awk '{print $0} { sum+=$1 } END { print "Total="sum}' awk {print $ 0}将打印每个文件的行,以便您可以看到每个文件的行。 You need to put the read $filename in a while; d ... -
这个awk应该做: multipath -ll | awk 'NR>1 {r=f=0;for (i=1;i<=NF;i++) if ($i~/ready/) r++; else if ($i~/faulty/) f++;split($5,a,"=|]");print $3,a[2]"\tTotal: "r+f" paths, active: "r,"failed: "f}' RS="mpath" OFS=", " dm-28, 200G Total: 5 paths, active: 4, fail ...
-
如何通过awk使用grep?(How to use grep via awk?)[2021-11-23]
在Gnu Awk中,您可以使用\<和\>来匹配单词的开头和结尾,所以 gawk '/\/{++i} END{print i}' 会做同样的事情 grep -wc 'GOOD' file 如果你想计算GOOD这个词的出现总数(不仅是行数,还有给定行/记录中的出现次数),你可以在Gnu Awk版本4中使用FPAT , gawk 'BEGIN { FPAT="\\ "; RS="^$" } { print NF }' file 如果要计算给定记录中短语GOOD DI的完全匹配数, ... -
使用awk重定向命令输出(redirect command output with awk)[2023-10-23]
尝试这样的tee命令: make all | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }' | tee file tee将在STDOUT上显示输出并将输出存储在文件中。 Try tee command like this: make all | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }' | tee file tee will display output on STDOUT and sto ... -
需要awk脚本来解析ethernet statistics命令的输出(need awk script to parse output of ethernet statistics command)[2022-01-18]
怎么样: # list all ent's and there counts $ awk '/ent[0-9]+/{e=$3}/^Packets:/{print e,$2}' file (ent0) 0 (ent1) 30 # list only the count for a given ent $ awk '/ent0/{e=$3}/^Packets:/&&e{print $2;exit}' file 0 $ awk '/ent1/{e=$3}/^Packets:/&&e{print $2;ex ... -
这似乎更有效: zcat Input.gz | awk -F, '{key=$2","substr($3,4,6)","substr($4,4,6)","$10","$8","$6;++a[key];b[key]=b[key]+$6}END{for(i in a)print i","a[i]","b[i]}' 输出: 0,MAY-14,MAY-14,K1,RO414,600,3,1800 0,MAY-12,MAY-12,K1,RO312,600,1,600 5,JAN-13,,K2,RO113,4150, ...
-
awk告诉您变量NF中给定行中有多少个字段,因此您可以创建两个数组来跟踪所需的信息。 一个数组将保留给定列中最后一行的值。 另一个将计算给定列中的开关数。 您还将跟踪最大列数(并将新列的计数设置为零,以便在该列的开关数为0时在末尾的输出中正确打印它们)。 您还将确保不计算从空字符串到非空字符串的转换 - 这在第一次遇到列时会发生。 实际上,如果文件的列数统一相同,那么只会影响第一行数据。 如果后续行实际上有比第一行更多的列,则会添加它们。 如果列停止出现一点,我认为它应该从它停止的地方恢复(好像缺失的列与之 ...
-
awk支持FILENAME变量,它包含,猜猜是什么?,文件名。 你甚至不需要shell循环。 只是: awk 'NR % 2 == 0 {printf "%s:%s\n", FILENAME, $0}' * awk supports the FILENAME variable which contains, guess what?, the filename. You don't even need the shell loop. Simply: awk 'NR % 2 == 0 {printf "%s: ...