Jump to content

could someone help with a complex search and replace query? (gsar or sed for Windows)


Recommended Posts

Hi there!

I am working on a conversion script but got stuck with 1 thing that I cannot get past... 
I want to change a certain word in a file with a hex-value, with the use of gsar.exe (command line!), but ignore it if the word is somewhere between quotes

Original text:
9023 AAAA "this is a AAAA" "AAAA" "*:;AAAA'" Aaaa " Because AAAA" " aaaa " AaAa

text after gsar is done:
9023 XXXX "this is a AAAA" "AAAA" "*:;AAAA'" XXXX " Because AAAA" " aaaa " XXXX

(where XXXX represents the hex value a5)

Please note:
-it should be case insensitive
-it should ignore everything between quotes
-I want to use gsar.exe but sed.exe (Windows) or another command line tool would be good as well
-The opening quotes are the same as the closing quotes (0x22)

I have been working on this for a whole day now and I am not even close :P

I know this be done and probably with only 1 line. This is what makes it extra frustrating. It doesn't have to be gsar. Sed.exe or another command line tool would be good as well! 
 

Link to comment
Share on other sites


You might need two passes with gsar:

1) [SPACE]AAAA[SPACE]

2) [SPACE]AAAA[CR][LF]

(this of course does *another* thing from what you asked, instead of ignoring what is inside quotes, it picks what is between spaces or between a space and newline, taken  from your example)

SED is probably more suited, but it is of course much more complex.

jaclaz 

 

Link to comment
Share on other sites

Hi @Henric. I don't know gsar.

Basic sed solution below, two passes, one for each instance. This assumes a flat text file. As @jaclaz indicated carriage returns and such, it would be useful to see the full layout of the file being modified.

sed -i 's| AAAA | XXXX |g' test_file
sed -i 's| AaAa| XXXX|g' test_file

Simple one-liner to keep the sed commands separate, if && is available:

sed -i 's| AAAA | XXXX |g' test_file && sed -i 's| AaAa| XXXX|g' test_file

Above uses the -i switch, modify file in place. If this option isn't available then direct output to temporary files and overwrite original or keep working from newly created files.

sed 's| AAAA | XXXX |g' test > test_file.tmp
mv -f test_file.tmp test_file

Note the 'g' (global i assume) option is also used in sed example above. This will change all instances it finds in the entire file. So if there is more than one instance of ' AAAA ', for example, they will all get changed. Removing the 'g' will just replace the first instance it finds.

sed -i 's| AAAA | XXXX |' test_file
sed -i 's| AaAa| XXXX|' test_file

If you need to change only one line, and other instances of the same patterns are present, it gets more complicated but doable, don't know exactly from your description. Unix tools like 'sed' work best in partnership with others, so 'head', 'tail' or 'grep' would be useful too. If there are more instances but you're always changing the exact same line number, then the line number can be specified in the sed command too.

PS I'm not trolling you @jaclaz, just saw this interesting topic on an otherwise slow forum :)
Link to comment
Share on other sites

Hi again @Henric. After reply i realized above probably wasn't what you wanted but didn't have a chance to revisit computer. Below is a one-liner that replaces exactly the string you provided. Slashes are used to escape quotes and special characters. Since the entire sed command is wrapped in single quotes, any single quotes inside the command need to be escaped and re-wrapped, suspect this is where you encountered difficulty.

Note this is just a cut/paste of what you provided in first post, including spaces. If there are line breaks or anything else that changes things, you did not elaborate.

sed -i 's|9023 AAAA \"this is a AAAA\" \"AAAA\" \"\*\:\;AAAA'\''\" Aaaa \" Because AAAA\" \" aaaa \" AaAa|9023 XXXX \"this is a AAAA\" \"AAAA\" \"\*\:\;AAAA'\''\" XXXX \" Because AAAA\" \" aaaa \" XXXX|' test_file
Link to comment
Share on other sites

Note to less than perfect eyes (like mine), back to back single quotes appear to be a double quote. This snippet contains three single quotes, escaping and re-wraping a single quote.

'\''

Logging in without JavaScript the code snippet appears to have been pasted and interpreted correctly by the forum software. Take care.
Link to comment
Share on other sites

16 hours ago, Wunderbar98 said:

PS I'm not trolling you @jaclaz, just saw this interesting topic on an otherwise slow forum :)

Sure you are not :).

Sed is waaay more powerful than gsar and much more suited to this task than gsar (and in some versions sed can also use Regex expressions), but as I said earlier it is much more complex and BTW I suspect that the "generalization/simplification" of the search/replace string by the OP may not be entirely accurate, I think he should post an example text with more specific data than AAAA and aaaa, as they risk to be misinterpreted, resulting in a "non working" suggestion.

jaclaz

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...