modifying file using script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modifying file using script
# 1  
Old 09-01-2009
Question modifying file using script

Hi,

I am new to shell programming, and want to know is it possible to change the contents of file using script?

for example, if want to search 2 words and want to replace these words with 2 new words.

Regards,

Manoj
# 2  
Old 09-01-2009
Yes, it is posssible

Eg

file.txt
aaa
bbb
ccc

Now if i want to replace bbb with ddd ,use

Code:
sed 's/bbb/ddd/' file.txt > file1.txt

Note you can't directly modify a file, you have to redirect it to other file
# 3  
Old 09-01-2009
Code:
Note you can't directly modify a file, you have to redirect it to other file

You can using perl one liner.
# 4  
Old 09-01-2009
Quote:
Originally Posted by sarbjit
Note you can't directly modify a file, you have to redirect it to other file
you can... but not with "sed" which is the "stream editor". but you can use "ed" to alter a file directly. afaik it uses the same syntax as "sed"!
# 5  
Old 09-01-2009
You can edit a file inplace with sed, without redirection with the '-i' switch if you have it...

Code:
sed -i~ 's/bbb/ddd/' 'file'

-Enjoy
fh : )_~
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying bash script to take each line in a file and execute command

I need to modify a bash script to to take each line in a file and execute command. I currently have this: #!/bin/bash if ; then echo "Lipsa IP"; exit; fi i=1 ip=$1 while ; do if ; then rand=`head -$i pass_file | tail -1` user=`echo $rand | awk '{print $1}'` pass=`echo $rand | awk '{print $2}'`... (3 Replies)
Discussion started by: galford
3 Replies

2. Shell Programming and Scripting

Modifying contents of the file in shell script

Hello all, I have a Kconfig file that looks like something below ... ================================ menu "Application type" config GUI_TYPE_STANDARD bool "Standard Application" source "cfg/config/std.in" source... (12 Replies)
Discussion started by: anand.shah
12 Replies

3. Shell Programming and Scripting

Need some help modifying script

I have a script that currently runs fine and I need to add or || (or) condition to the if statement and I'm not sure the exact syntax as it relates to the use of brackets. my current script starts like this: errLog="/usr/local/website-logs/error.log" apacheRestart="service httpd restart"... (3 Replies)
Discussion started by: jjj0923
3 Replies

4. Shell Programming and Scripting

awk script for modifying the file

I have the records in the format one row 0009714494919I MY010727408948010 NNNNNN N PUSAAR727408948010 R007YM08705 9602002 S 111+0360832-0937348 I want to get it int the format 0009714494919I MY010727408948010 NNNNNN N PUSAAR727408948010 R007YM08705 9602002 S ... (2 Replies)
Discussion started by: sonam273
2 Replies

5. Shell Programming and Scripting

Modifying the Restart Script

Hello, I have a shell script which calls for an existing script with appropriate parameters. It looks something like this. -------------------------------------------------------------------------- #!/bin/bash sh /root/ams_rc stop_shepherd > /dev/null sleep 30 sh /root/ams_rc... (9 Replies)
Discussion started by: Siddheshk
9 Replies

6. UNIX for Dummies Questions & Answers

Modifying a shell script without using an editor

Hi all, I really wan't to know that how to edit a shell script with out using an editor.. Is there any command? (4 Replies)
Discussion started by: buddhi
4 Replies

7. Shell Programming and Scripting

modifying perl script

Hi freinds I have a small problem I want u to help me in, I have a syslog server and configured it to send me email automatically, I get a small perl script to help me in, and tested it to send alerts to root and it worked successfully without any problems Now I want to send it outside, I... (4 Replies)
Discussion started by: reaky
4 Replies

8. Shell Programming and Scripting

Modifying a .ksh script

Hi users, I am on a beginner level and just joined this site. I have created a simple .ksh file in the following manner cat <<EOF >mfile #!/bin/ksh echo "hello world" EOF Request for some help with 2 cases 1. now i would like to add a second line after the first echo command... (4 Replies)
Discussion started by: corbusier
4 Replies

9. Shell Programming and Scripting

need help with understanding and modifying script

hi all, i am new to UNIX. this is my first time using Ubuntu. i need to do this for my fyp. i am using an artificial neural network model to predict the yield strength of steel. the shell script used to execute this model is as shown here: #Thomas Sourmail, Cambridge University /... (4 Replies)
Discussion started by: dakkorn
4 Replies

10. Shell Programming and Scripting

Modifying a csv file from Shell Script

Hi all, I have some script that creates a temp csv file. What I need to do is do some search and replace and modify the file from my shell script. I know the commands to open the file and then apply the reg ex but wasnt sure how I could do this from a script and modify the file? Any help... (2 Replies)
Discussion started by: not4google
2 Replies
Login or Register to Ask a Question