grep values and re-arranging the file

2019-09-21 03:37发布

hi i have a file name test.txt

    (standard input):8:      <property name="ProcedureName" value="abc"/>
(standard input):7:         <property name="PackageName" value="123abc"/>
(standard input):8:         <property name="ProcedureName" value="bac"/>
(standard input):7:      <property name="PackageName" value="bac123"/>
(standard input):8:      <property name="ProcedureName" value="cde"/>
(standard input):7:      <property name="PackageName" value="cd123"/>
(standard input):8:      <property name="ProcedureName" value="b4u"/>
(standard input):7:      <property name="PackageName" value="b4u234"/>

i have to grep only values of packagename and procdeurename from this file in the follwing format: into an o/p file

abc/123abc
bac/bac123
cde/cd123
b4u/b4u234

tried cut and awk but couldnt get that

6条回答
Deceive 欺骗
2楼-- · 2019-09-21 04:11

My first attempt (and the one I'd actually recommend) was the same as @sat's so I deleted it and here's a different approach in case it's useful in some other context:

$ awk -F'"' '{n2v[$2]=$4} !(NR%2){print n2v["ProcedureName"] "/" n2v["PackageName"] }' file
abc/123abc
bac/bac123
cde/cd123
b4u/b4u234

n2v means name2value, an array name I often use for the type of application where we have name to value mappings in the input file.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-21 04:12

awk should be able to do this for you:

awk -F'"' 'BEGIN { OFS="/" } $2=="ProcedureName"{procedureName=$4} $2=="PackageName" { print procedureName,$4 }' yourfilename

This will use double-quote as the seperator. It tests for the string "ProcedureName" in position 2 and stores position 4 in the variable procedureName. Then if it finds "PackageName" in position 2, it prints out the stored procedureName and the stuff from position 4. And it uses backslash as the OutputFieldSeperator.

Technically you could pipe your grep to this, but awk can just do the search itself, which is what I've written up here.

查看更多
狗以群分
4楼-- · 2019-09-21 04:16

Try this awk:

awk -F'"' 'NR%2{p1=$4;next} {print p1 "/" $4}' 

Test:

$ awk -F'"' 'NR%2{p1=$4;next} {print p1 "/" $4}' file
abc/123abc
bac/bac123
cde/cd123
b4u/b4u234
查看更多
甜甜的少女心
5楼-- · 2019-09-21 04:16

This might work for you (GNU sed):

sed 'N;s#.*value="\([^"]*\)".*value="\([^"]*\)".*#\1/\2#' file

Read two lines at a time and extract the values between double quotes preceeded by the literal value=.

Another slightly shorter version using a backreference in the LHS and -r option to make the regexp easier to read:

sed -r 'N;s#.*(value=")([^"]*)".*\1([^"]*)".*#\2/\3#' file

Yet another way, using the hold space and substitution:

sed -r 's/.*"(.*)".*/\1/;h;N;s//\1/;H;g;s#\n#/#' file

Extracts the last value between double quotes in two successive lines and re-arranges the results to the required string.

查看更多
我只想做你的唯一
6楼-- · 2019-09-21 04:28

As an alternative to awk and grep solutions.

sed -rn 's|.*"([^"]*)"/>|\1|p' xml | pr -2ats/
查看更多
叛逆
7楼-- · 2019-09-21 04:29

With GNU grep and paste:

grep -oP '"\K[^"]*(?="/)' file | paste -d / - -

Output:

abc/123abc
bac/bac123
cde/cd123
b4u/b4u234
查看更多
登录 后发表回答