Reputation: 637
I need to extract value using sed
command;
I have a file, that contains something like this:
field="value"
The result needs to be: value
Upvotes: 0
Views: 139
Reputation: 948
Or you can use awk to look for "field", split the fields up using '=', and then deleting the extra quotes using tr
awk -F= '/field/ {print $2}' <file | tr -d '"'
Upvotes: 0
Reputation: 439397
Just to provide an awk
alternative, since awk
is often the better choice when it comes to field-based processing.
Your exact requirements are unclear, but here I'm making the following assumptions:
=
and all values are "
-enclosedThe following allows you to pass in a field name via the -v fname=...
option and returns that field's value unquoted; sample input is provided via a here-document.
awk -F\" -v fname='field' '$1 == (fname "=") { print $2; exit }' <<'EOF'
key="largo"
field="value"
id="fake"
EOF
Upvotes: 0
Reputation: 948
try
#!/bin/sh
sed -n 's/field="\(.*\)"/\1/p'
This looks for lines that have field=
If it finds them, it removes everything except the value, and prints the value. Otherwise, it prints nothing.
Upvotes: 4