LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2005, 04:50 AM   #1
luckyvietman
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Rep: Reputation: 0
how to modify a text file or a string in shell programming


Please show me what command to modify a text file or a string .
Example :
I have a text file "a.txt" , its character in line 5 and column 10 is 'x' and I want to replace it with 'y' character. How can i do this ?

and in string . I have a variable $somestring="This is some string" . The first character of this string is ' T ' and i want to replace it with character ' t ' . How can i do it ?

help me, please. Thank you very much !
 
Old 05-28-2005, 04:59 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Looks like homework, although I'm not sure
So here are some pointers you could look into:

grep -> Look for a string in a file/variable
sed -> replace a string with something else.

Examples:

grep "dummy" some.file => greps any line containing dummy in some.file
sed 's/THEN/THAN/g' => Chnage THEN into THAN (case sensitive), on the whole line (the g).

Take a look at the manpages for all the options.

Hope this helps.

Last edited by druuna; 05-28-2005 at 05:00 AM.
 
Old 05-28-2005, 08:08 AM   #3
trutnev
Member
 
Registered: Sep 2003
Distribution: RedHat 9.0, Mandrake 9.1
Posts: 68

Rep: Reputation: 15
Re: how to modify a text file or a string in shell programming

gawk -v var=y -F '' 'NR==5 {$5=var; for( i = 1; i <= NF; i++) printf"%s", $i; printf "\n"}; NR!=5' a.txt

this is to replace exactly the 10th character on the 5th line

to be more generic, you should use sed and grep

to lower the characters, use the tr command



Quote:
Originally posted by luckyvietman
Please show me what command to modify a text file or a string .
Example :
I have a text file "a.txt" , its character in line 5 and column 10 is 'x' and I want to replace it with 'y' character. How can i do this ?

and in string . I have a variable $somestring="This is some string" . The first character of this string is ' T ' and i want to replace it with character ' t ' . How can i do it ?

help me, please. Thank you very much !
 
Old 07-11-2005, 08:58 PM   #4
jpgoper
LQ Newbie
 
Registered: Jul 2005
Posts: 2

Rep: Reputation: 0
I tried with:
Quote:
for file in $(grep "oldtext" /home/onedir/*)
do
sed 's/oldtext/newtext/g' $file >/tmp/$$ && mv /tmp/$$ $file
done
old/unwanted text is:
<script type ="text/javascript" src='http://www.business-plan-development.com/astracker/ast.php'></script>

new text:
<? include($ruta."/include/clicks.php");?>

and it doesn't work, it gives me a prompt ">" as if it was waiting for aditional code. The problem is with sed, because when changed sed for echo, it ends well.

I got the same problem when using:
Quote:
find . -name '*.html' | xargs perl -pi -e 's/oldtext/newtext/g'
can anyone help me? please!
 
Old 07-12-2005, 03:32 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
wow this would be eazier in c then in bash
lol go figure


mabye you could use a macro for vi or something but im not too familliar with bash scripting. sorry
 
Old 07-12-2005, 05:08 PM   #6
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Are you replacing literarly oldtext with the line you specified ?

In that case you should escape the " and / . Like :
Code:
A="<script type =\"text\/javascript\" src='http:\/\/www.business-plan-development.com\/astracker\/ast.php'><\/script>"
sed "s/$A/newtext/g"
(of course, same with new text).
 
  


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
Using grep, etc to modify a text file dangerousdave Linux - Newbie 7 02-23-2005 02:49 PM
modify text of an jpg file on linux with gimp 2.0.4 tcma Linux - Software 1 10-19-2004 11:12 AM
Removing Text from a Shell String OrangeLefty Programming 2 04-18-2004 11:08 PM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 04:17 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 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