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.
If you want to replace "xyx" by "abc," you can try this: (based on chidam's code, above.)
Code:
#!/bin/bash
OLD="xyz"
#Or, to replace all occurrences of xyz by abc, use OLD="/xyz"
NEW="abc"
F1PATH="a.txt"
F2PATH="b.txt"
while read line
do
line=${line/${OLD}/${NEW}}
echo $line >> $F2PATH
done < $F1PATH
mv -f $F2PATH $F1PATH
Note: Untested code. (I had to use a MS operating system for a while.)
Note 2: This is bash specific. the Bourne shell does not, I believe, support the search-and-replace parameter expansion.
Note 3: Is this a homework problem? Your requirements sound like something an instructor might think would be a useful exercise. If so, please review the LQ policy on homework.
what u send is using command.
what u send me was script using command line. But my query was without command line. Finally m able to replace without using any command in my script. U also check this script, this really works.
Replace Word from a Original text file.
Suppose a text file named abc.txt contains:
my name is xyz
change the name xyz to abc.
Script File:
#!/bin/bash
clear
rm -f ab1
while read line
do
for var1 in `echo $line`
do
if [ “$var1” == “xyz” ]; then
var1=“abc”
else
if [ “$var1” == “abc” ]; then
var1=“xyz”
fi
echo -n “$var1 ” >> ab1
done
echo >>ab1
done<abc.txt
cat ab1 > abc.txt
The above script will replace xyz from the original file to abc and next time when we run the script it replace abc to xyz. And ab1 is temp file.
---------- Post added 01-28-12 at 03:10 PM ----------
Quote:
Originally Posted by chidam
If you need see this link for bash string manipulations
what u send me was script using command line. But my query was without command line. Finally m able to replace without using any command in my script. U also check this script, this really works.
Replace Word from a Original text file.
Suppose a text file named abc.txt contains:
my name is xyz
change the name xyz to abc.
Script File:
#!/bin/bash
clear
rm -f ab1
while read line
do
for var1 in `echo $line`
do
if [ “$var1” == “xyz” ]; then
var1=“abc”
else
if [ “$var1” == “abc” ]; then
var1=“xyz”
fi
echo -n “$var1 ” >> ab1
done
echo >>ab1
done<abc.txt
cat ab1 > abc.txt
The above script will replace xyz from the original file to abc and next time when we run the script it replace abc to xyz. And ab1 is temp file.
what u send me was script using command line. But my query was without command line. Finally m able to replace without using any command in my script. U also check this script, this really works.
Replace Word from a Original text file.
Suppose a text file named abc.txt contains:
my name is xyz
change the name xyz to abc.
Script File:
#!/bin/bash
clear
rm -f ab1
while read line
do
for var1 in `echo $line`
do
if [ “$var1” == “xyz” ]; then
var1=“abc”
else
if [ “$var1” == “abc” ]; then
var1=“xyz”
fi
echo -n “$var1 ” >> ab1
done
echo >>ab1
done<abc.txt
cat ab1 > abc.txt
The above script will replace xyz from the original file to abc and next time when we run the script it replace abc to xyz. And ab1 is temp file.
Replace Word from a Original text file.
Suppose a text file named abc.txt contains:
my name is xyz
change the name xyz to abc.
Script File:
#!/bin/bash
clear
rm -f ab1
while read line
do
for var1 in `echo $line`
do
if [ “$var1” == “xyz” ]; then
var1=“abc”
else
if [ “$var1” == “abc” ]; then
var1=“xyz”
fi
echo -n “$var1 ” >> ab1
done
echo >>ab1
done<abc.txt
cat ab1 > abc.txt
The above script will replace xyz from the original file to abc and next time when we run the script it replace abc to xyz.
what u send is using command.
what u send me was script using command line. But my query was without command line. Finally m able to replace without using any command in my script. U also check this script, this really works. ..
Not to quibble (to much ), but the code I posted does not use any non-bash commands except the mv command in the last line. The line=${line/${OLD}/${NEW}} statement (where the replacement is done) is a bash parameter expansion, not a non-bash command, and you could use a cat command (which you did use in the code you posted) to accomplish the same thing. (mv has less system overhead than cat, but that's not significant in a one-time program.)
Did you look at the reference posted by chidam? (You quoted his post.)
I would suggest you try various elements available in the bash toolbox. After a while you will find that using these 'other' commands you seem to hold in low regard will slowly creep into your script just because they are better. You will not learn Bash overnight, the O'Reilly Bash Book and others show bash script writing and give numerous examples that include those 'other' programs.
Your persistent disregard for folks trying to teach you something is clearly an indication that this homework and YOU should do it not the members here. I will probably be reprimanded for saying so but "Do it yourself" it is easy!
I would suggest you try various elements available in the bash toolbox. After a while you will find that using these 'other' commands you seem to hold in low regard will slowly creep into your script just because they are better. You will not learn Bash overnight, the O'Reilly Bash Book and others show bash script writing and give numerous examples that include those 'other' programs.
Your persistent disregard for folks trying to teach you something is clearly an indication that this homework and YOU should do it not the members here. I will probably be reprimanded for saying so but "Do it yourself" it is easy!
I am not tring to teach ny1 here...wat i wanted was a script without command line. And also i hv nt disregard ny1 here in LQ. Before ter was no command line in linux....it was only the logic. And i wantd somthin with logic not with command line. Finally i did it myself...I am here in LQ Forum to learn how to develop good logics from everyone here and not to teach ny1 or disregard....I respect every1s knowledge here in LQ Forum...I think u gt me wrong my friend but nyway its fine with me....Thankx to all for helping me.
Not to quibble (to much ), but the code I posted does not use any non-bash commands except the mv command in the last line. The line=${line/${OLD}/${NEW}} statement (where the replacement is done) is a bash parameter expansion, not a non-bash command, and you could use a cat command (which you did use in the code you posted) to accomplish the same thing. (mv has less system overhead than cat, but that's not significant in a one-time program.)
Did you look at the reference posted by chidam? (You quoted his post.)
mv is fine not a problem but the line(line=${line/${OLD}/${NEW}}) is nothing but the same as sed command....it directly replaces the word. And i wanted something using general logic.....nyway i did it myself. I hv seen the reference posted by chidam but its not working. just check once my script which i posted last....its working without using any replacement command. "/" is same as sed...it actually directly replaces the words from a file. And in my script u will not find any "/" or sed command. I hv used simple logic using while,for and if....Before there was no command line in linux....it cm much later.....people used to use simple logic to write a script. And thats what i wanted....Using command line ny1 can do....but using logic is a art...which i want to learn....thats why i am here in LQ forum to learn from u guys....and not to disregard anyone here in the forum. I respect everyone here and am really thankful to everyone here to support me.
O.K., I can almost understand your goal, but - if you really want to learn "simple logic" - you might be better off learning, for example, prolog rather than limiting your statement choices when using a script language. Or, just use python as your shell script, and learn how to program. (Note that, for example, if you'd asked you question in the context of a C program, your responses would have been quite different.)
Your belief that the "original users" of GNU/Linux systems were limited to "simple logic" in their shell scripts is, I believe, somewhat mistaken.
Even the 1960's Multics system, which was the direct ancestor of the UNIX system, had a fairly sophisticated shell language. It (Multics) even had an emacs editor (programmed in LISP) that could be used, just like the emacs available on most Linux distributions can be used, as an interactive terminal on a simple tty. And very sophisticated "shell scripts" can be developed in eLisp (the "dialect" of LISP used by emacs).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.