如何才能从文本文件中获取此字符串

时间:2012-03-12 04:44:48

标签: sed awk

我可以阅读这种格式文本(例如id.txt)文件:

 - 1BCF-2982 -- Installing Driver
 - 1BCF-2982 -- Driver Installed
 - (Testing Wireless 5630 (EVDO-HSPA) Mobile Broadband Mini-Card)
 - Executing Test -- MEID hex value
 - Extended test data -- MEID:0xA1000013B8F66E
 - Executing Test -- IMEI decimal value
 - Extended test data -- IMEI:355023040845227
 - Executing Test -- ICCID decimal value
 - (Wireless 5630 (EVDO-HSPA) Mobile Broadband Mini-Card Test Results)

或以下格式id.txt文件

 - 1BCF-2982 -- Installing Driver
 - 1BCF-2982 -- Driver Installed
 - (Testing Wireless 5630 (EVDO-HSPA) Mobile Broadband Mini-Card)
 - Extended test data -- MEID:0xA1000013B8F66E
 - Extended test data -- IMEI:355023040845227
 - (Wireless 5630 (EVDO-HSPA) Mobile Broadband Mini-Card Test Results)

这是我的方式,建立meid.sed和imei.sed

/MEID/{
s/^.*:0x([-0-9a-zA-Z]+)/set meid=\1/p;
}

和imei.sed

/IMEI/{
s/^.*:([-0-9a-zA-Z]+)/set imei=\1/p;
}

你能用简单的方式对我说,输出看起来像这样:

set meid=A1000013B8F66E
set imei=355023040845227

谢谢!

3 个答案:

答案 0 :(得分:3)

$ sed -nr '/.*(MEID|IMEI):(0x)?([0-9A-F]+)/s//set \L\1\E=\3/p' id.txt
set meid=A1000013B8F66E
set imei=355023040845227

答案 1 :(得分:2)

肮脏而快速的awk one liner适用于您的示例:

awk -F' -- |:' 'NF>2{print "set "tolower($2)"="$3}' yourFile

使用示例1进行测试:

kent$  echo "- 1BCF-2982 -- Installing Driver
 - 1BCF-2982 -- Driver Installed
 - (Testing Wireless 5630 (EVDO-HSPA) Mobile Broadband Mini-Card)
 - Extended test data -- MEID:0xA1000013B8F66E
 - Extended test data -- IMEI:355023040845227
 - (Wireless 5630 (EVDO-HSPA) Mobile Broadband Mini-Card Test Results)
"|awk -F' -- |:' 'NF>2{print "set "tolower($2)"="$3}'
set meid=0xA1000013B8F66E
set imei=355023040845227

答案 2 :(得分:0)

您可以使用此命令实现相同的功能

cat <your input file> | grep -E "MEID:|IMEI:" | awk '{print $NF;}' | tr ':' '=' | awk '{print "set " $0;}'

要么可以从命令prmpt运行它,要么将它放在.sh文件中并运行它