[英]Linux Script to find string containing specific formatting & manipulate the data
I need to create a linux script to search for lines in a file that are formatted like this: 我需要创建一个Linux脚本来搜索文件格式如下的行:
text:text:text:text:number:number 文字:文字:文字:文字:数字:编号
so 6 text/number strings divided by 5 semicolon 所以6个文本/数字字符串除以5分号
For example: 例如:
2f0d:011a0000:07f8:0002:1:0
I want to treat the semicolon as column divider 我想把分号当作列分隔符
eg 例如
Column1:Column2:Column3:Column4:Column5:Column6
I then want to rearrange the data like so: 然后,我想像这样重新排列数据:
Column1:Column3:Column4:Column2 discarding column5 & column6
For example: 例如:
2f0d:07f8:0002:011a0000
I then want to replace semicolon with underscore, remove leading Zeros from each column & convert to UPERCASE 然后,我想用下划线替换分号,从每列中删除前导零并转换为大写
For example: 例如:
2F0D_7F8_2_11A0000
End Result 最终结果
in file1, an entry like this 在file1中,像这样的条目
2f0d:011a0000:07f8:0002:1:0
E4+1
p:BSkyB,C:0000
will be converted to this: 将被转换为:
2F0D_7F8_2_11A0000
E4+1
p:BSkyB,C:0000
Please note also, there are 100's if not 1000s of these 3 line entries in file1 另请注意,file1的这3行条目中有100个(如果不是1000个)
kent$ awk -F: -v OFS="_" 'NF==6{for(i=1;i<=4;i++){sub(/^0*/,"",$i);$i=toupper($i)};print $1,$3,$4,$2;next}7' file
2F0D_7F8_2_11A0000
E4+1
p:BSkyB,C:0000
you may want to know that, in awk: 您可能想知道,在awk中:
the command much more important than the above one-liner: 该命令比上述单行代码重要得多:
man gawk
与sed:
sed -r 's/^0*([[:alnum:]]+):0*([[:alnum:]]+):0*([[:alnum:]]+):0*([[:alnum:]]+):0*([[:digit:]]+):0*([[:digit:]]+)$/\U\1_\3_\4_\2/' foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.