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
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:
n2v
meansname2value
, an array name I often use for the type of application where we have name to value mappings in the input file.awk
should be able to do this for you: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 storedprocedureName
and the stuff from position 4. And it uses backslash as the OutputFieldSeperator.Technically you could pipe your
grep
to this, butawk
can just do the search itself, which is what I've written up here.Try this
awk
:Test:
This might work for you (GNU sed):
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:Yet another way, using the hold space and substitution:
Extracts the last value between double quotes in two successive lines and re-arranges the results to the required string.
As an alternative to
awk
andgrep
solutions.With GNU grep and paste:
Output: