Avatar billede carnudo Nybegynder
03. oktober 2001 - 10:05 Der er 11 kommentarer og
1 løsning

How to replace words in bash?

Here is the example I would like you to help me with...I appreciate any help.

***

I have a folder in LINUX called /home/mamon
and inside /home/mamon there are 100 files and other directories like this:

/home/mamon>
DirX
DirY
DirZ
fil1
fil2
fil3
fil4
fil5
...
...
...
fil100

As you can see the files do not have extension in linux.

In fil1 to fil100 it is writen \"I like rise\"
and I want to replace the word \"rise\" for \"fish\"
Inside the Directories DIRX DIRY and DIRZ there are also files with the same text which I also want to replace with \"fish\".

How can I do that?
Avatar billede peter_m Nybegynder
03. oktober 2001 - 11:36 #1

Make a script that use a for statement


for FIL in /placering/*
do
cat $FIL | sed s/rise/fish/g > tmpfile
cp tmpfile $FIL
done
rm tmpfile

This will in all files substitute rise with fish
you might wanna limit the *.
Avatar billede peter_m Nybegynder
03. oktober 2001 - 11:37 #2
if you wanna change fil names

its just a mv you do it with the principble is the same.
Avatar billede carnudo Nybegynder
03. oktober 2001 - 12:02 #3
Thanks Peter for the answer, here some questions.
-Will this also change the word in the subdirectories?
-The deletion in the end \"rm tmpfile\" I guess is
manually? and shouldnt it be rm -R tmpfile?
-Rgds
Avatar billede peter_m Nybegynder
03. oktober 2001 - 14:29 #4
What I wrote was just a example and not the salution.

if you wanna remove all files from . and below you do so

You may consider reading the help files on find also course you are able to put commands on find.

If you try this example


for FIL in *
do
  echo $FIL
done

You will get printe to screen what the * will give you.
Avatar billede peter_m Nybegynder
03. oktober 2001 - 14:30 #5
and thats is tmpfiles you wanna remove
Avatar billede carnudo Nybegynder
03. oktober 2001 - 14:39 #6
Oh, I thought you gave me a solution, sorry I am not used to this commands. Do you think you could write exactly what I am looking for? I will of course read the info to understand the commands you probably will enter now.
Thanks
Avatar billede peter_m Nybegynder
03. oktober 2001 - 14:50 #7
for FIL in *
do
  cat $FIL | sed s/\"I like rise\"/\"I like fish\"/g > tmpfile
cp tmpfile  $FIL
done
rm tmpfile

try it

If your not sure bout the changed there will be make a copy of you dirs and extract it in f.eks /tmp/yourusername

I dont put garenties on this stuff :)
Avatar billede carnudo Nybegynder
03. oktober 2001 - 21:34 #8
ohhh what a bad luck, it did not work.
Thank you Peter anyway for your help. I appreciate it very much.
Avatar billede carnudo Nybegynder
02. november 2001 - 13:40 #9
Did not work.
Avatar billede carnudo Nybegynder
03. december 2001 - 12:53 #10
Not good. Closing case.
Avatar billede carnudo Nybegynder
28. december 2001 - 10:55 #11
closing case
Avatar billede carnudo Nybegynder
28. december 2001 - 10:56 #12
Closed.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester