• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Replace keyword with its filename without extension : bash script

February 11, 2014 by Sharad Chhetri Leave a Comment

Today in this example we will use bash script. The task is replace keyword with its filename without extension.To accomplish this task we will use bash script.In bash script we will use for loop and sed command .To elaborate this example,lets assume we have 4 files called

file1.txt
red.txt
blue.txt
roger.txt

The above each file has keyword called hello in its content. Hence we will replace hello keyword with its own filename but without extension.
For example. in red.txt , hello keyword will be replaced with red [note: red is file name without extension]

I am writing two scripts which do same job, you can use anyone at one time

Bash script 1: Create a file test.sh and paste the below given content. replace the keyword as per your scenario .

#!/bin/bash
for i in *.txt
do
sed -i -e "s,hello,${i/.txt/},g" $i
done

After saving the file,give permission chmod 750 test.sh

Now run the script by typing sh test.sh

Bash script 2:

First change to directory where you want to do this practical.Then list the filename in txtlist file

ls -1 *.txt > txtlist

Create a file sharad.sh and paste the below given content. replace the keyword as per your scenario.

#!/bin/bash
for i in `cat txtlist|sed 's/.txt//g'`
do
sed -i "s/hello/$i/g" $i.txt
done

After saving the file,give permission chmod 750 sharad.sh

Now run the script by typing sh sharad.sh

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. send email after mysql backup through bash script in simple way
  2. Run the script using nohup without hitting enter key two times
  3. How to Use Expect In Bash Script
  4. How To Use case in Bash Script, Example
  5. How To Use opt In Bash Script
  6. Set and reset user password by bash script
  7. Xen Server License Nagios Plugin (bash script)
  8. Nagios plugin check_mailfromd bash script
  9. MySQL backup bash script
  10. Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Filed Under: Tips And Tricks Tagged With: bash script, for loop, sed

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

awk command to search keyword or strings in file

grep command to remove commented lines

group ownership reference to file/dir from other file/dir in linux

sed : find the pattern (keyword) and delete the line from file

semanage command not found on CentOS 7 and RHEL 7

How to show user account password expiration detail on Linux

6 df Command Examples To Check Mounted Filesystem On Linux

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

How to encode and decode the strings with base64

New XAMPP security concept

Could not find make anywhere!!! Please edit the config section to include the path to make. at ./install.pl line 2101

Agent admitted failure to sign using the key ssh

How to install korn shell ksh in Linux

Do not show line haveing particular keyword by grep command

How to download package using apt-get command in ubuntu

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy