substitution without editing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitution without editing
# 1  
Old 07-12-2011
substitution without editing

I have a script with 100's of lines in it. I want to edit the script with out manually openning it (with vi editor or some thing like that). Basically I want to do a substitution using regular expression (%s/G_/28_/i). Please let me know the best way to do this.
# 2  
Old 07-12-2011
What is your input and what is your expected output?
# 3  
Old 07-12-2011
At the 86th line of the script (say: test.pl) I need to substitute the file name 'file1.txt' to file2.txt.

so basically my input will be the script and what I need to edit and out put will be the same script but with the edition (substitution) already done.
# 4  
Old 07-12-2011
You could take the filename as a parameter for the script easily enough.

Code:
...
my $file=shift;
...

Code:
./myscript.pl filename.txt

or you could just
Code:
sed 's/file1\.txt/file2.txt/' < myscript.pl > output
cat output > myscript.pl

Every time you edit it like this you run the risk of making a mistake though, so a parameter would be much better. Never need to edit the file at all.
# 5  
Old 07-12-2011
Quote:
Originally Posted by Lucky Ali
I have a script with 100's of lines in it. I want to edit the script with out manually openning it (with vi editor or some thing like that). Basically I want to do a substitution using regular expression (%s/G_/28_/i). Please let me know the best way to do this.
If I'm reading it correctly, that sample editing command will subsitute 28_ for the first instance of G_ or g_ in every line in a file. The following pipeline will accomplish the same thing non-interactively:
Code:
printf '%s\n' '1,$s/[gG]_/28_/' w q | ed -s file

Or, if you prefer, using a heredoc:
Code:
ed -s file <<'EOED'
1,$s/[gG]_/28_/
w
q
EOED

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. Shell Programming and Scripting

File Editing

Never mind!. Thanks (1 Reply)
Discussion started by: smarones
1 Replies

3. Shell Programming and Scripting

Problem editing with ed...

I have troubles passing variables to ed. i'm trying to do something like this: #!/bin/bash User=`cat /etc/pmx.conf |grep User | cut -d "=" -f2` new=$1 ed -s /etc/pmx.conf <<< $'/User/s/$User/$new/g\nw' :S (9 Replies)
Discussion started by: Tadeo Armenta
9 Replies

4. Shell Programming and Scripting

Editing headers

Hi, I have a folder that contains many (multiple) files 1.fasta 2.fasta 3.fasta 4.fasta 5.fasta . . 100's of files Each such file have data in the following format for example: vi 1.fasta >AB_1 200bp MLKKPIIIGVTGGSGGGKTSVSRAILDSFPNARIAMIQHDSYYKDQSHMSFEERVKTNYDHPLAFDTDFM... (4 Replies)
Discussion started by: Lucky Ali
4 Replies

5. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. UNIX for Dummies Questions & Answers

Editing BRUTAB

Hello, I get the following in one of my error logs: Device /dev/sda, SATA disks accessed via libata are not currently supported by smartmontools. When libata is given an ATA pass-thru ioctl() then an additional '-d libata' device type will be added to smartmontools. --------------- I... (0 Replies)
Discussion started by: mojoman
0 Replies

7. UNIX and Linux Applications

Editing BRUTAB

Hello, We use BRU to back up the UNIX machines on our network. We need to tell BRU NOT to backup a director, call it directory1, on a particular machine. I have been told that a modification needs to be made to BRUTAB on that machine. If this is the case, what do I need to do? if it is not,... (0 Replies)
Discussion started by: mojoman
0 Replies

8. Shell Programming and Scripting

file editing

hi experts, please help me in writting the script.. i have two files file1 and file 2 i have to write a script which will take input parameters as file1 and file2 file1: ...... 1 2 3 4 file2: ..... 1 2 output (6 Replies)
Discussion started by: subhendu81
6 Replies

9. Shell Programming and Scripting

Editing a File

Hi all, I have a file with following contents # rad124 # radkus # raddebug # radtrace I could like to remove the # and space present before the key word "rad". Any ways to do this using "subsitution method(:%s/old/new/g)" will be hepful. (1 Reply)
Discussion started by: ramkriz
1 Replies

10. UNIX for Dummies Questions & Answers

Editing problem

Hi, I want to do this: original file: a a hello e e bye becames: a hello e bye I think this can be done with sed or tr, but I've already tried all the options and can't find the solution. Thanks in advance (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question