Thursday, October 6, 2016

Command: Get only the string escaping the quotes around it in UNIX

The situation is like you have a file having key='value' pairs.
Values are kept under quotes ' or ".
you need to get the value for the given key pattern.

You know the key you just need the value which is stored against the key pattern.

The simple command to get the string value against the key pattern from a file,


awk -F"=" '/<Key-PATTERN>/{printf $2}' <filename> | cut -d \' -f2

In the above case I am assuming the key value pairs are kept with the separator "="

filename
ABC='English'
KaKhaGa='Hindi'
AlifBeTe='Urdu'

If I need the value for AlifBeTe it should give output as Urdu.

The above command gives you the string value escaping the quotes around it.

No comments:

Post a Comment