从格式错误的纯文本文件中grep键值对

时间:2013-06-29 22:36:20

标签: shell awk sed grep cygwin

我正在编写一个需要从错误地检索键值对的shell脚本 格式化明文.txt文件。 .txt是具有的MS Word文档 被保存为明文。正如您从下面的示例中看到的那样 Sample_Profile.txt,键由已分隔的值继承 打开和关闭括号。

User First Name

(Goofball)

User Last Name

(Goofberg) Email Address

(goofball@example.com)

Password (sogoofedrightnow)

1. Profile details

Profile name*  (Goofball's Profile) Profile Id**
(Guid2763944-a234)

唯一的问题似乎是在匹配时忽略空格和空行 价值的关键。总之,我想要做的是指定密钥(例如 “用户名”或“个人资料名称”)和grep只有相应的值, 最后管道到我的sed,所以我得到了我需要的值。

这是我编写的脚本,旨在获取“用户”的值 名字“。

FIRST_NAME=$(grep "User First Name" Sample_Profile.txt | sed 's|[^(]*(\([^)]*\)).*|\1|') 
#grep User First Name key and pipe to sed to get the value bewteen parentheses
sed -i -e 's/USER_FIRST_NAME/'"$FIRST_NAME"'/g' UserName.txt 
echo $FIRST_NAME 
# outputs "User First Name" when it should get "Goofball" (grep is not
# piping correctly due to white space)

1 个答案:

答案 0 :(得分:4)

awk '/User First Name/ {print $2}' RS=')' FS='('

输出:

Goofball
相关问题