Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My script is something like below to replace port in the file template.txt and rename as test.txt. This script is named as test .sh
#!/bin/bash
filename="test.txt
port="80"
cp template.txt $filename
sed -i.bak "s/port/$port/g" $filename
I am implementing script as below and getting error as below,
#test.sh
sed: can't find label for jump to 'ak'
sed: can't find label for jump to 'ak'
sed: can't find label for jump to 'ak'
sed: can't find label for jump to 'ak'
sed: can't find label for jump to 'ak'
My script is something like below to replace port in the file template.txt and rename as test.txt. This script is named as test .sh
Do not name files or scripts with the name test. It is a built-in bash command. Credit to rknichols for that knowledge, because I made the exact same mistake just a short while ago.
Quote:
Originally Posted by chinna448
[/code]#!/bin/bash [/code]
What are these garbage characters at the end of every code line? They should not be there.
$ var=red
$ sed 's|green|'$var'|g' testfile
pea red soup
to get sed to actually change the file itself, rather than show you the results on the cli, add your -i
Code:
sed -i 's|green|'$var'|g' testfile
Single quotes all around. That is the way I've always done it.
For the others, he said notepad file. being the txt to denote a notepad file.
OP:
If you are looking for the word(s) 'port=??' and want to replace it with 'port=80'.
Code:
#!/bin/bash
filename="testfile"
Template="template.txt"
port="port=80"
#test for files presents before issuing commands on it.
[[ -f "$Template" ]] &&
{ cp "$Template" "$filename" ;
sed -i 's|port.*|'$port'|g' "$filename" ;
cat "$filename" ; }
#cat can be used for testing purposes to show results.
read -p "Do you want to make this your file? " ans
[[ "${ans,,}" = 'y' ]] &&
{ echo "Otay.. changing it to template file" ;
mv -v $filename $Template ;
echo " done deal..." ; } ||
{ echo "OTay, not doing that..." ; }
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.