Is there anything better (smarter) than sed?

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
798
Reaction score
657
Credits
5,107
This sed is seriously beginning to crawl on my nerves and IDK how long it will take before I kick it in its back out of my OS!
I have this universal file Saves_trick.txt which I copy inside each game's main dir which then I edit manually to create a link to the original saves dir. But I wanna save me the trouble of editing it for every game, so I thought I could make sed replace the string with brackets with the current directory name, thus making the Saves_trick file universal. For instance, the universal file contents looks like this:

Code:
ln -sTv /B/GAMES/[GAME_NAME]/MainSaves /home/rado/Документи/

So, for instance, I open terminal in NFS MW directory: [rado@arch]: /B/GAMES/NFSMW>$, then I run a script which copies the Saves_trick.txt to the same directory (cp Saves_trick.txt ./).
I tried using sed to replace the [GAME_NAME] string with the cirrent directory name:

Code:
sed -i 's/[GAME_NAME]/.//g' file.txt

but instead of replacing [GAME_NAME] with NFSMW, sed replaces everything else AROUND [GAME_NAME] with "./", leaving [GAME_NAME] untouched! :mad: So not only it doesn't use the current dir name to replace [GAME_NAME] but instead it replaces all characters of the path with "./"! MEaning that both paths become /.//.//[GAME_NAME]/./ /.//.//.//
I've never seen a dumber program than that!
 


But why do you use the square brackets, just leave them out so that GAME_NAME is a literal word
 
If your frustration is that you don't want to do it yourself, then i think bash scripts could help you out. You can put sed commands (or awk, perl, python, perl, the alternative text manipulation languages) into a bash script. Also, cronjob is specifically used to automate tasks.

As far as the issue you have with the text you are replacing: it seems like you are using the program totally incorrectly. In the sed command in question, you are using the "g" flag and the "-i" option. If the result is making you mad, then please stop using the "-i" option because this irreversably changes your file(s), and since you are using the g flag, then it fu*ks up every instance of your proposed change instead of just the first time it encounters it on the line. If you run it without "-i", then it will only print the result of your changes on the terminal.

but instead of replacing [GAME_NAME] with NFSMW, sed replaces everything else AROUND [GAME_NAME] with "./", leaving [GAME_NAME] untouched! :mad: So not only it doesn't use the current dir name to replace [GAME_NAME] but instead it replaces all characters of the path with "./"! MEaning that both paths become /.//.//[GAME_NAME]/./ /.//.//.//
I've never seen a dumber program than that!
With that code above, then everything in your "[GAME NAME]" regular expression will be replace with ".". I hope you understand that "[]" and "." are both regular expressions when you use it as the text you are trying to change. Sed is basically a programming language, so please use it as a text editor with a lot more caution.
 
Yeah, removing the brackets fixed the thing.
Now the only thing that remains is to find what to type in before the /g, so that sed replaces GAME_NAME with the current directory name. For instance, if I run this command in /B/123, GAME_NAME to be replaced with "123" (without the quotes, of course).
But Google and the duck are silent when it comes to sed and current directory name.
 

Members online


Latest posts

Top