简体   繁体   English

Linux脚本查找包含特定格式的字符串并处理数据

[英]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中:

  • sub(pat, rep,input) will do replacement; sub(pat,rep,input)将进行替换;
  • toupper(string) will change string into upper case (yes, there is tolower() too) toupper(string)会将字符串更改为大写(是的,也有tolower())
  • print $1,$2 will print col1 and col2 separated by OFS 打印$ 1,$ 2将打印由OFS分隔的col1和col2

the command much more important than the above one-liner: 该命令比上述单行代码重要得多:

man gawk

a solution using sed: 使用sed的解决方案:

 sed -r 's/^0*([a-f0-9]+):0*([a-f0-9]+):0*([a-f0-9]+):0*([a-f0-9]+):[a-f0-9]+:[a-f0-9]+$/\1_\3_\4_\2/'

see DEMO 演示

与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.

相关问题 查找子目录并删除不包含特定字符串LINUX的文件 - Find subdirectory and remove files not containing a specific string LINUX 如何在 Linux 上查找包含特定文本(字符串)的所有文件? - How to find all files containing specific text (string) on Linux? Linux bash:查找包含特定文本的文件夹 - Linux bash: find folders containing specific text 如何在Linux中名称包含特定字符串的文件中搜索字符串? - How to search for a string in files with name containing a specific string in linux? linux脚本查找文件名中的特定单词 - linux script to find specific words in file names 在Terminal / Linux上查找包含特定日期范围文件名的所有文件 - Find all files containing the filename of specific date range on Terminal/Linux 如何更改 linux 上包含特定字符串的所有目录/文件的名称 - how to change names of all directories / files containing a specific string on linux 仅打开包含特定字符串的文件,然后在Linux命令行上进行替换 - Open only files containing a specific string and then replace on Linux command line Bash脚本处理包含输入字符串的数据 - Bash Script to process data containing input string 使用 Linux 查找包含特定字符串的文件并复制到目录 - Find Files Containing Certain String and Copy To Directory Using Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM