-1

I am trying to read input from user and update a .txt file by replacing a word. Is there any possibility to do that? If there please help me. I want this to done using SHELL Script on Ubuntu OS.

Example:

I want to replace the word "Kid" From the below .Txt file with a user input "boys".

.Txt file content:

I love that kid that they are playing on the ground.

Step by Step :

  1. Start the Shell Program.
  2. Receive a Input from user to $a.
  3. Read .txt file and find the word "Kid".
  4. Replace the "Kid" Word from the .txt file with the user input $a.

That's all I want to do.

MUHSIN MOHAMED PC
  • 167
  • 1
  • 2
  • 17

1 Answers1

1

You mean something like this ?

#!/bin/bash

WORD="foo" # what to find
TXT_FILE="/tmp/lala.txt" # where

# get the new word from user
read -p "With what \"$WORD\" should be replaced ? " newword

# replace, preserving capitalization
output=$(cat "$TXT_FILE" | sed "s/${WORD^}/${newword^}/g" | sed "s/$WORD/$newword/ig")
echo "$output" > "$TXT_FILE"
echo "done"

If you run it, you get:

/tmp » cat lala.txt 
Foo is riding a bike, while Frank is sunbathing. 
Suddenly, Foo falls. And also Foo, foo, foo.

/tmp » ./lala.sh     
With what "foo" should be replaced ? bar
done

/tmp » cat lala.txt 
Bar is riding a bike, while Frank is sunbathing. 
Suddenly, Bar falls. And also Bar, bar, bar.
Derlin
  • 9,572
  • 2
  • 32
  • 53
  • Yes. Is that file save automatically after making the changes? – MUHSIN MOHAMED PC Feb 28 '16 at 17:26
  • Can I do the same method to change words from a .JS file? I am asking this questions because, Now I don't have a Ubuntu OS running on my PC. – MUHSIN MOHAMED PC Feb 28 '16 at 17:40
  • yes, you can use it with any kind of files. But beware: if the words you find/replace contain special chars, you may have some surprise. The best way to go is to understand the script and tune it to suit your needs. – Derlin Feb 28 '16 at 17:50
  • Is there is any video tutorial or reference that might useful to solve this kind of problems? – MUHSIN MOHAMED PC Feb 28 '16 at 17:52
  • I suggest you read/follow a tutorial on the command `sed` on the internet. Then, play with it a while. – Derlin Feb 28 '16 at 17:55
  • If my answer helps you, please don't forget to upvote it and to mark your post as answered (checkmark on the left). – Derlin Feb 28 '16 at 17:56
  • How to mark as answered? I Up voted you answer but its not voting intend of increasing the vote count, I received a message showed in my Display. "You need more reputation to vote". – MUHSIN MOHAMED PC Feb 28 '16 at 18:03
  • No worries, just use the checkmark on the left, the upvote is not so important. Glad I could help. – Derlin Feb 28 '16 at 18:04
  • Thanks man. Would you like to share your Email address with me. If I can ask question. Please just); – MUHSIN MOHAMED PC Feb 28 '16 at 18:07
  • 1
    The sed line and the one following it can be simplified to `sed -i "s/${WORD^}/{newword^}/g;s/$WORD/$newword/ig" "$TXT_FILE"`, which avoids spawning a bunch of subshells and having sed process the file multiple times. Also, variable names in all caps are more likely to clash with existing variables, see http://stackoverflow.com/a/673940/3266847 – Benjamin W. Feb 28 '16 at 20:33
  • pluse-uno for usable answer and technical support ;-) Good luck to all. – shellter Feb 28 '16 at 23:31