WildlandsTech
Comprehensive List of GSAR Commands with Descriptions - Printable Version

+- WildlandsTech (https://wildlandstech.com)
+-- Forum: Programming (https://wildlandstech.com/forumdisplay.php?fid=3)
+--- Forum: Batch & Shell Scripting (https://wildlandstech.com/forumdisplay.php?fid=42)
+--- Thread: Comprehensive List of GSAR Commands with Descriptions (/showthread.php?tid=148)



Comprehensive List of GSAR Commands with Descriptions - Sneakyone - 09-03-2024

Comprehensive List of GSAR Commands with Descriptions

**GSAR** (Global Search And Replace) is a command-line utility used for performing search and replace operations on binary and text files. Below is a detailed list of GSAR commands, along with descriptions and examples.



1. Basic Search and Replace
Description: Replaces all occurrences of a specified search string with a replacement string in a file.
Code:
gsar -s"search_string" -r"replacement_string" [file]
Example: To replace "foo" with "bar" in `example.txt`:
Code:
gsar -s"foo" -r"bar" example.txt

2. Case-Insensitive Search and Replace
Description: Replaces all occurrences of a search string with a replacement string, ignoring the case.
Code:
gsar -i -s"search_string" -r"replacement_string" [file]
Example: To replace "Foo" or "foo" with "bar" in `example.txt`:
Code:
gsar -i -s"foo" -r"bar" example.txt

3. Replace First Occurrence Only
Description: Replaces only the first occurrence of the search string in the file.
Code:
gsar -o -s"search_string" -r"replacement_string" [file]
Example: To replace only the first occurrence of "foo" with "bar" in `example.txt`:
Code:
gsar -o -s"foo" -r"bar" example.txt

4. Replace and Create a Backup of the Original File
Description: Replaces all occurrences of a search string and creates a backup of the original file.
Code:
gsar -b -s"search_string" -r"replacement_string" [file]
Example: To replace "foo" with "bar" in `example.txt` and create a backup:
Code:
gsar -b -s"foo" -r"bar" example.txt
Note: The backup file will have the original filename with an additional `~` character.

5. Search and Replace with a Specific Offset
Description: Starts the search and replace operation at a specified byte offset within the file.
Code:
gsar -s"search_string" -r"replacement_string" -o[byte_offset] [file]
Example: To start replacing "foo" with "bar" starting at byte offset 100:
Code:
gsar -s"foo" -r"bar" -o100 example.txt

6. Search and Replace in a Specific Range
Description: Replaces occurrences of a search string within a specified byte range in the file.
Code:
gsar -s"search_string" -r"replacement_string" -n[start_offset] -m[end_offset] [file]
Example: To replace "foo" with "bar" between bytes 100 and 200 in `example.txt`:
Code:
gsar -s"foo" -r"bar" -n100 -m200 example.txt

7. Show Matching Lines Only (Silent Mode)
Description: Shows only the lines that contain the matching search string, without modifying the file.
Code:
gsar -s"search_string" -r"replacement_string" -I [file]
Example: To display lines containing "foo" in `example.txt` without replacing them:
Code:
gsar -s"foo" -r"bar" -I example.txt

8. Count the Number of Replacements
Description: Displays the number of replacements made in the file.
Code:
gsar -s"search_string" -r"replacement_string" -c [file]
Example: To count the number of times "foo" is replaced with "bar" in `example.txt`:
Code:
gsar -s"foo" -r"bar" -c example.txt

9. Replace with a Binary String
Description: Replaces a search string with a binary sequence in the file.
Code:
gsar -s"search_string" -r"\x[hex_value]" [file]
Example: To replace "foo" with a newline character in `example.txt`:
Code:
gsar -s"foo" -r"\x0A" example.txt

10. Replace Using Hexadecimal Search and Replace Strings
Description: Uses hexadecimal values for both the search and replace strings.
Code:
gsar -s"\x[search_hex]" -r"\x[replace_hex]" [file]
Example: To replace the hexadecimal value `0x666F6F` ("foo") with `0x626172` ("bar") in `example.txt`:
Code:
gsar -s"\x666F6F" -r"\x626172" example.txt

11. Replace with Confirmation
Description: Prompts the user for confirmation before replacing each occurrence.
Code:
gsar -s"search_string" -r"replacement_string" -p [file]
Example: To replace "foo" with "bar" in `example.txt` with confirmation:
Code:
gsar -s"foo" -r"bar" -p example.txt

12. Invert Match (Replace Non-Matching Lines)
Description: Replaces lines that do not match the search string.
Code:
gsar -v -s"search_string" -r"replacement_string" [file]
Example: To replace all lines not containing "foo" with "bar":
Code:
gsar -v -s"foo" -r"bar" example.txt

13. Use Regular Expressions for Search and Replace
Description: Uses regular expressions for more advanced search and replace operations.
Code:
gsar -r"regex_search" -r"regex_replace" [file]
Example: To replace all occurrences of digits with "number":
Code:
gsar -r"\d+" -r"number" example.txt

14. Perform a Dry Run (No Changes Made)
Description: Simulates the search and replace operation without making any changes to the file.
Code:
gsar -s"search_string" -r"replacement_string" -t [file]
Example: To simulate replacing "foo" with "bar" in `example.txt`:
Code:
gsar -s"foo" -r"bar" -t example.txt

15. Display Help Information
Description: Displays help information for the GSAR command, listing all available options and their descriptions.
Code:
gsar -h
Example: To display help information for GSAR:
Code:
gsar -h



Conclusion

The **GSAR** command is a powerful utility for performing search and replace operations in binary and text files. By mastering these commands, you can efficiently manipulate and modify file contents, whether for data processing, text manipulation, or binary file editing.

Happy Editing!