问题
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
回答1:
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
回答2:
With GNU grep and paste:
grep -oP '"\K[^"]*(?="/)' file | paste -d / - -
Output:
abc/123abc bac/bac123 cde/cd123 b4u/b4u234
回答3:
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.
回答4:
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.
回答5:
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:
As an alternative to awk
and grep
solutions.
sed -rn 's|.*"([^"]*)"/>|\1|p' xml | pr -2ats/
来源:https://stackoverflow.com/questions/32726286/grep-values-and-re-arranging-the-file