Skip to main content
updated the command for revised question
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/*_-/0xX/' infile 

This will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

This ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

Every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters).

After all done, those characters *_- will translate to 0Xx0xX.

Add -i option to above command to update the changes in your input file like sed -i -E ... .


Update: The command for the revised question:

sed -E '/KEYWORD/{ :lower s/^(([^"]*("[^"]*"){0,1})*)("[^"]*)[a-z]([^"]*")/\1\4_\5/; t lower; :upper s/^(([^"]*("[^"]*"){0,1})*)("[^"]*)[A-Z]([^"]*")/\1\4+\5/; t upper; :digit s/^(([^"]*("[^"]*"){0,1})*)("[^"]*)[0-9]([^"]*")/\1\4*\5/; t digit; }; y/*_+/0xX/' infile 

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/*_-/0xX/' infile 

This will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

This ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

Every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters).

After all done, those characters *_- will translate to 0Xx.

Add -i option to above command to update the changes in your input file like sed -i -E ... .

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/*_-/0xX/' infile 

This will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

This ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

Every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters).

After all done, those characters *_- will translate to 0xX.

Add -i option to above command to update the changes in your input file like sed -i -E ... .


Update: The command for the revised question:

sed -E '/KEYWORD/{ :lower s/^(([^"]*("[^"]*"){0,1})*)("[^"]*)[a-z]([^"]*")/\1\4_\5/; t lower; :upper s/^(([^"]*("[^"]*"){0,1})*)("[^"]*)[A-Z]([^"]*")/\1\4+\5/; t upper; :digit s/^(([^"]*("[^"]*"){0,1})*)("[^"]*)[0-9]([^"]*")/\1\4*\5/; t digit; }; y/*_+/0xX/' infile 
y does not take expressions, so no [...] needed. Minor grammar fixes. +1
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/[*_*_-]/[0xX]0xX/' infile 

thisThis will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

thisThis ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

everyEvery part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters);.

afterAfter all done, those characters [*_*_-] will translate to [0Xx]0Xx;.

addAdd -i option to above command to update the changes in your input file like sed -i -E ... .

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/[*_-]/[0xX]/' infile 

this will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

this ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters);

after all done, those characters [*_-] will translate to [0Xx];

add -i option to above command to update the changes in your input file like sed -i -E ... .

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/*_-/0xX/' infile 

This will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

This ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

Every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters).

After all done, those characters *_- will translate to 0Xx.

Add -i option to above command to update the changes in your input file like sed -i -E ... .

deleted 280 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/[*_-]/[0xX]/' infile 

this will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

  • ("[^"]*)[a-z]([^"]*") it's match to " and anything after that until first lower-case character found which flowed by anything until another quote matched.
  • ("[^"]*)[A-Z]([^"]*") it's match to " and anything after that until first upper-case character found which flowed by anything until another quote matched.
  • ("[^"]*)[0-9]([^"]*") it's match to " and anything after that until first digit character found which flowed by anything until another quote matched.

this ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters);

after all done, those characters [*_-] will translate to [0Xx];

add -i option to above command to update the changes in your input file like sed -i -E ... .

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/[*_-]/[0xX]/' infile 

this will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

  • ("[^"]*)[a-z]([^"]*") it's match to " and anything after that until first lower-case character found which flowed by anything until another quote matched.
  • ("[^"]*)[A-Z]([^"]*") it's match to " and anything after that until first upper-case character found which flowed by anything until another quote matched.
  • ("[^"]*)[0-9]([^"]*") it's match to " and anything after that until first digit character found which flowed by anything until another quote matched.

every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters);

after all done, those characters [*_-] will translate to [0Xx];

add -i option to above command to update the changes in your input file like sed -i -E ... .

Using sed:

sed -E '/KEYWORD/{ :lower s/("[^"]*)[a-z]([^"]*")/\1_\2/; t lower; :upper s/("[^"]*)[A-Z]([^"]*")/\1-\2/; t upper; :digit s/("[^"]*)[0-9]([^"]*")/\1*\2/; t digit; }; y/[*_-]/[0xX]/' infile 

this will run the set of codes in block /KEYWORD/{...} only when a line matched with a string KEYWORD.

this ("[^"]*)[###]([^"]*") matches to a " and anything after that until first lower-case [a-z]/upper-case[A-Z]/digit[0-9] character found which flowed by anything until another quote matched.

every part will loop over again and again until all those characters were converted lower-case to _, upper-case to - and digits to * (note: chose different characters if these may occurred in your file; the reason is we didn't replace directly with x or X or 0 because it will cause infinite loop for sed since of using sed's loops to replace every lower/upper/digit characters);

after all done, those characters [*_-] will translate to [0Xx];

add -i option to above command to update the changes in your input file like sed -i -E ... .

added 166 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118
Loading
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118
Loading