I have following file: $less dummyKeyAndValue.txt   apiKey=key1;some_other_data;term=abc   apiKey=key2;some_other_data;some_other_data;term=def   term=pqr;some_other_data;apiKey=key1   apiKey=key3;some_other_data;term=defI want the output as the following : $less dummyNewFile.txt   apiKey=key1 term=abc   apiKey=key2 term=def   apiKey=key1 term=pqr   apiKey=key3 term=defMainly,I want to extract 'apiKey' and 'term' from dummyKeyAndValue.txt file,they both can appear in different order in file.I tried following command :   $cat dummyKeyAndValue.txt | tee >(egrep -o 'apiKey=[a-zA-Z0-9]+')  |      egrep -o 'term=[a-zA-Z]+' | lessI get output as :     term=abc     term=def     term=pqr     term=defCan someone help me with the command for getting desired output?
Extracting only specific multiple patterns from file
shell script;text processing;regular expression
As an alternative a very efficient but slightly more complicated solutionsed 'G;s/;/\n/' | awk -F= '$1~/apiKey/ {key=$2}$1~/term/ {term=$2}/^$/ {printf(  apiKey=%s term=%s\n, key, term)      key=      term=}'First sed is used to do two things:  The G command will effectively add an open line after every record set, and secondly the substitute command (s/;/\n/) will effectively expand each record set to be one-per-line by replacing every ; with a new-line character.  What comes out of sed is they key-value pairs one per line, with an open line designating the end of each record.Then awk needs only to look at the first field to find the attributes you are interested in, and the second field for the value, which eliminates the need for index and substr.  Once awk encounters an open line it prints the values that it has found.  For resilience' sake you can clear the values at the end of each record.  Note the use of -F= to instruct awk to split the line into fields based on the =-sign.$1 ~ /.../ means When the first field matches the value /.../It then assigns a value to a variable (key or term)The /^$/ means when awk encounters an open line