top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Shell Script to replace contents inside the files?

+1 vote
469 views

I have thousands of html files inside a folder. I want to replace the filename present inside another files. Say for ex:- fileName: 'abcd1234.html' is found inside another file say file2.html. Then I want to remove the last 4 digits of the fileName i.e,. 'abcd1234.html' => 'abcd.htm'.

posted Jul 24, 2017 by Ramakrishnan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Using below command you can replace string inside file. Use the same code and implement desired script for multiple files.

  sed -i 's/original/new/g' file.txt

Explanation:

sed = Stream EDitor

-i = in-place (i.e. save back to the original file)

s = the substitute command

original = a regular expression describing the word to replace (or just the word itself)

new = the text to replace it with

g = global (i.e. replace all and not just the first occurrence)

file.txt = the file name
answer Jul 25, 2017 by Chirag Gangdev
Similar Questions
+1 vote

I have some unique requirement, where I need to do something like this -
1. My script takes the argument as file name which is absolute filename.
2. I need to separate the filename and directory from this absolute file name for the further processing.

Any suggestions -

0 votes

I have a shell script copyScript.sh present in my pendrive.
What i want is, Whenever i connect pendrive to my pc this script should be run automatically.

Is it possible?
If yes then how to achieve this?

+2 votes

I have a process in Linux which can have different memory footprint, I want a program or shell script which can print the time of the day when the memory usage is highest for this process.

Can someone help me.

+3 votes
...