user6376225
user6376225

Reputation: 43

Linux command to extract the value for a given name

Suppose I have one text file(EmployeeDetails.txt) in which the content is written(all name/value in new line) as mentioned below:-

EmployeeName=XYZ
EmployeeBand=D5
EmployeeDesignation=SSE

I need the Linux command which will read this file EmployeeDetails.txt and give the value for EmployeeBand. Output should be

D5

Upvotes: 0

Views: 433

Answers (1)

P....
P....

Reputation: 18391

Using grep: If anything is followed by EmployeeBand= will be printed.

grep -oP 'EmployeeBand=\K.*' EmployeeDetails.txt

Using awk where = is used as field separator and second field is printed. if search criteria is meet.

awk -F'=' '/EmployeeBand/{print $2}' EmployeeDetails.txt

Using sed ,here the band D5 is captured is a group inside () and later used using \1.

sed -r '/EmployeeBand/ s/.*=(.*$)/\1/g' EmployeeDetails.txt

Upvotes: 5

Related Questions