LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-24-2012, 10:09 PM   #16
chidam
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Posts: 4

Rep: Reputation: Disabled
Thumbs up


If you need see this link for bash string manipulations

http://www.thegeekstuff.com/2010/07/...-manipulation/
 
Old 01-28-2012, 03:40 AM   #17
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Hi

Quote:
Originally Posted by PTrenholme View Post
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 View Post
If you need see this link for bash string manipulations

http://www.thegeekstuff.com/2010/07/...-manipulation/
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.
 
Old 01-28-2012, 03:41 AM   #18
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Hi all,

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.
 
Old 01-28-2012, 04:08 AM   #19
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Hi All,

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.
 
Old 01-28-2012, 08:30 PM   #20
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by amit4465 View Post
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.)
 
Old 01-29-2012, 09:24 PM   #21
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
Without Sed

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!
 
Old 01-29-2012, 11:53 PM   #22
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Smile Hi

Quote:
Originally Posted by lisle2011 View 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 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.

Last edited by amit4465; 01-30-2012 at 12:14 AM.
 
Old 01-30-2012, 12:08 AM   #23
amit4465
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Distribution: Linux
Posts: 9

Rep: Reputation: Disabled
Smile Hi

Quote:
Originally Posted by PTrenholme View Post
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.
 
Old 01-30-2012, 11:04 PM   #24
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 06:36 PM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM
Find & Replace Carriage Return in ooo linuxian Linux - Software 1 04-09-2005 05:43 PM
Find & Replace Benr Linux - Newbie 5 05-03-2004 02:37 PM
find and replace in files from command line dexter_modem Linux - General 4 06-10-2003 11:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration