How to use the command 'ned' (with examples)
The ’ned’ command is a powerful tool that is similar to ‘grep’ but with additional capabilities for replacing text. It provides a flexible alternative to ‘sed’ as it is not limited to line-oriented editing. With ’ned’, you can search for patterns in files and directories, and perform various types of replacements.
Use case 1: Recursively search starting in the current directory, ignoring case
Code:
ned --ignore-case --recursive '^[dl]og' .
Motivation: This use case is useful when you want to find all files and directories that start with either ’d’ or ’l’ followed by ‘og’, regardless of case. By using the ‘–ignore-case’ option, ’ned’ will perform a case-insensitive search. The ‘–recursive’ option ensures that the search is performed recursively starting from the current directory (’.’).
Example output:
./dogs/dog.txt: This is a sample dog file.
./logs/log.txt: This is a log file.
./Logs/dog.log: This is a dog log.
Use case 2: Search always showing colored output
Code:
ned --colors '^[dl]og' .
Motivation: When searching for patterns in files, it can be helpful to have the matching text highlighted with colors for better visibility. The ‘–colors’ option enables colored output whenever there is a match. In this example, ’ned’ will search for files and directories that start with either ’d’ or ’l’ followed by ‘og’.
Example output:
dogs/
logs/
Logs/
Use case 3: Search never showing colored output
Code:
ned --colors=never '^[dl]og' .
Motivation: There may be situations where you want to disable colored output for the search results. The ‘–colors=never’ option ensures that ’ned’ does not apply any colors to the output, providing a plain and simple view of the results.
Example output:
dogs/
logs/
Logs/
Use case 4: Search ignoring certain files
Code:
ned --recursive --exclude '*.htm' '^[dl]og' .
Motivation: Sometimes, you may want to ignore specific files or file patterns during a search operation. The ‘–exclude’ option allows you to specify files or patterns that ’ned’ should exclude from the search. In this example, ’ned’ will perform a recursive search starting from the current directory (’.’), excluding any files with the ‘.htm’ extension and looking for files and directories that start with either ’d’ or ’l’ followed by ‘og’.
Example output:
./dogs/dog.txt: This is a sample dog file.
./logs/log.txt: This is a log file.
./Logs/dog.log: This is a dog log.
Use case 5: Simple replace
Code:
ned 'dog' --replace 'cat' .
Motivation: When you want to replace a specific pattern with another pattern in your files, use the ’ned’ command with the ‘–replace’ option. In this example, ’ned’ will search for occurrences of ‘dog’ and replace them with ‘cat’.
Example output:
This is a sample cat file.
This is a log file.
This is a cat and dog log.
Use case 6: Replace using numbered group references
Code:
ned 'the ([a-z]+) dog and the ([a-z]+) dog' --replace 'the $2 dog and the $1 dog' .
Motivation: Sometimes, you may need to perform more complex replacements using captured groups within the search pattern. By using numbered group references (e.g., $1, $2), you can rearrange the captured groups during the replacement. In this example, ’ned’ searches for patterns like ’the
Example output:
This is the black dog and the brown dog.
Use case 7: Replace changing case
Code:
ned '([a-z]+) dog' --case-replacements --replace '\U$1\E! dog' --stdout .
Motivation: ’ned’ provides the ‘–case-replacements’ option to modify the case of the replaced text based on the original pattern. By using ‘\U’ and ‘\E’ in the replacement pattern, you can convert the matched text to uppercase (\U) or lowercase (\L) before replacing it. In this example, ’ned’ searches for patterns like ‘
Example output:
This is a BLACK dog.
This is a BROWN dog.
Use case 8: Preview results of a find and replace without updating the target files
Code:
ned '^[sb]ad' --replace 'happy' --stdout .
Motivation: When you want to preview the changes that will be made by a find and replace operation without actually modifying the target files, use the ‘–stdout’ option. This option directs the output to the standard output instead of modifying the files. In this example, ’ned’ searches for patterns that start with either ’s’ or ‘b’, followed by ‘ad’ and replaces them with ‘happy’.
Example output:
happy
bad
happy
Conclusion:
The ’ned’ command is a versatile tool for searching and replacing text in files and directories. It offers a wide range of options to customize the search and replace operations, making it a powerful alternative to ‘grep’ and ‘sed’. Whether you need to perform simple replacements or complex rearrangements, ’ned’ has you covered.