How do I search for a keyword in a file and print out the string following the keyword? - perl

I want to search for keyword "mykey = " in a file and print out the string that is following the keyword.
I cannot do a "grep", because each line is very long. I just want to extract the string following the keyword.

Here's what I came up with. Not first, but works, and without the final grep.
grep 'mykey = ' file | sed 's/.*\(mykey = [A-Za-z]*\).*/\1/'

Assuming the keyword is a single word and a space follows it, like this:
mykey = myCoolValue
grep 'mykey' /your/file/here | sed -r 's/.*mykey = (^[ ]*) .*/\1/g' | grep .

If you have pcregrep at hand, you can issue this command in terminal or in a script to get only desired text after mykey =
$ pcregrep -o '(?<=mykey = ).+' file
The regex uses a positive lookbehind, where -o returns only the matched text, not the whole line.

Related

Capturing groups with sed command

I have strings like below
_c_VehCfg1_oCAN00_f276589c_In_Int_buf *pVehCfg1_oCAN00_f276589c_In_IntBuf = (_c_VehCfg1_oCAN00_f276589c_In_Int_buf *)can_Msg_tmp_buffer;
I want replace can_Msg_tmp_buffer with ptr as below
_c_VehCfg1_oCAN00_f276589c_In_Int_buf *pVehCfg1_oCAN00_f276589c_In_IntBuf = (_c_VehCfg1_oCAN00_f276589c_In_Int_buf *)ptr;
I have tried sed as below
echo "_c_VehCfg1_oCAN00_f276589c_In_Int_buf *pVehCfg1_oCAN00_f276589c_In_IntBuf = (_c_VehCfg1_oCAN00_f276589c_In_Int_buf *)can_Msg_tmp_buffer;" | sed 's/\(_C_[[:alnum:]_]*IntBuf = [[:alnum:]_]*\)can_Msg_tmp_buffer/1\ptr/g'
Still I'm not getting expected result instead sed output is same as input.
The problem is I have strings like below also
_c_GW_C4_oCAN00_f276589c_In_Moto_buf *pGW_C4_oCAN00_f276589c_In_MotoBuf = (_c_GW_C4_oCAN00_f276589c_In_Moto_buf *)can_Msg_tmp_buffer;
I only want to replace where type is ending with _Int_buf not _Moto_buf.
It gets extremely convoluted to match individual words with a regex and get a captured group out of it. One way would be to work with known parts of the string which are guaranteed to occur.
For your case, using the strings _In_IntBuf and can_Msg_tmp_buffer; we try to uniquely identify those pattern of lines and do the substitution
sed 's/\(.*\)_In_IntBuf = \(.*\)can_Msg_tmp_buffer;/\1_In_IntBuf = \2ptr;/'
In case you are ok with awk try following.
awk '/_In_IntBuf =/{sub(/can_Msg_tmp_buffer/,"ptr")} 1' Input_file
In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file in above code.

Need assistance with escapes in my groovy command

I need to replace a version string in a file. My search pattern is regex
and my replacement is a variable.
String search = "\\d+.\\d+.\\d+-.\\d+"
String replace = "1.0.0-${BUILD_ID}"
MyFile = "foo"
sh ("""
sed -i -r "s/($search/$replace/g)" $MyFile
""")
The result I am getting
+ sed -i -r s/(\d+.\d+.\d+-.\d+/1.0.0-25/g) foo
sed: bad option in substitution expression
I found the issue with my code. If I remove parenthesis (), the string replacement works as a charm.

How to use Sed command to get following output?

My input is:
"INTC_KEY,ABC1|OBJID,ABC2"
And I want to send the output to a file like:
DDS.INTC_KEY = REPL.OBJID AND DDS.ABC1 = REPL.ABC2
Here is what I've tried so far:
sed 's/^/DDS./g' | sed 's/|/=REPL./g' | tr '\n' '~' | sed 's/~/_N~/g' | sed 's/~$/\n/g' | sed 's/~/~\n/g' | sed 's/~/ AND/g' > ${LOG_DIR}/JOIN.tmp
Based on the single line of input, the following regular expression will transform the input into the desired output:
s/"\([^,]*\),\([^|]*\)|\([^,]*\),\(.*\)"/DDS.\1 = REPL.\3 AND DDS.\2 = REPL.\4/
This shell command shows it working:
$ echo '"INTC_KEY,ABC1|OBJID,ABC2"' | sed 's/"\([^,]*\),\([^|]*\)|\([^,]*\),\(.*\)"/DDS.\1 = REPL.\3 AND DDS.\2 = REPL.\4/'
DDS.INTC_KEY = REPL.OBJID AND DDS.ABC1 = REPL.ABC2
The regular expression basically matches four pieces of text (using the escaped parentheses), delimited by the commas and vertical bar and made available as the \1-\4 back references for the substitution.
Note: I’ve tried to stick to using the features of standard sed and I tested using GNU sed with the POSIXLY_CORRECT environment variable set to 1 to emulate standard sed.

replace a line that contains a string with special characters

i want to replace lines which contains a string that has some special characters.
i used \ and \ for escape special characters but nothing changes in file.
i use sed like this:
> sed -i '/pnconfig\[\'dbhost\'\] = \'localhost\'/c\This line is removed.' tco.php
i just want to find lines that contains :
$pnconfig['dbhost'] = 'localhost';
and replace that line with:
$pnconfig['dbhost'] = '1.1.1.1';
Wrap the sed in double quotes as
sed -i "s/\(pnconfig\['dbhost'\] = \)'localhost'/\1'1.1.1.1'/" filename
Test
$ echo "\$pnconfig['dbhost'] = 'localhost';" | sed "s/\(pnconfig\['dbhost'\] = \)'localhost'/\1'1.1.1.1'/"
$pnconfig['dbhost'] = '1.1.1.1';
Use as below:
sed -i.bak '/pnconfig\[\'dbhost\'\] = \'localhost\'/pnconfig\[\'dbhost\'\] = \'1.1.1.1\'/' tco.php
Rather than modifying the file for the first time, create back up and then search for your pattern and then replace it with the other as above in your file tco.php
You don't have to worry about backslashing single quotes by using double quotes for sed.
sed -i.bak "/pnconfig\['dbhost'\] = 'localhost'/s/localhost/1.1.1.1/g" File
Try this one.
sed "/$pnconfig\['dbhost']/s/localhost/1.1.1.1/"

cut off known substring sh

How to cut off known substring from the string in sh?
For example, I have string "http://www.myserver.org/very/very/long/path/mystring"
expression "http://www.myserver.org/very/very/long/path/" is known. How can I get "mystring"?
Thanks.
E.g. using perl:
echo "http://www.myserver.org/very/very/long/path/mystring" | perl -pe 's|^http://www.myserver.org/very/very/long/path/(.*)$|\1|'
E.g. using sed:
echo "http://www.myserver.org/very/very/long/path/mystring" | sed 's|^http://www.myserver.org/very/very/long/path/\(.*\)$|\1|'
E.g. when the search string is held in a variable, here named variable. Use double quotes to expand the variable.
echo "http://www.myserver.org/very/very/long/path/mystring" | sed "s|^${variable}\(.*\)$|\1|"
Tested under /bin/dash
$ S="http://www.myserver.org/very/very/long/path/mystring" && echo ${S##*/}
mystring
where
S is the variable-name
## remove largest prefix pattern
*/ upto the last slash
For further reading, search "##" in man dash
Some more illustrations:
$ S="/mystring/" ; echo ${S##*/}
$ S="/mystring" ; echo ${S##*/}
mystring
$ S="mystring" ; echo ${S##*/}
mystring