Edit a line in a file with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit a line in a file with perl
# 1  
Old 02-07-2008
Edit a line in a file with perl

Hi,
How can I edit a line in a file?

For example, a.txt contains:
start: 1 2 3 4
stop: a b c d

and I want to change "3" to "9"

and to add "5" after "4"

the result should be (a.txt):
start: 1 9 3 4 5
stop: a b c d

Thanks,
zed
# 2  
Old 02-07-2008
I didn't mention this but it need to be in a perl script.

Thanks,
Zed
# 3  
Old 02-07-2008
Code:
perl -i.bak -p -e 's/^(start:) (1 2 3 4)/$1 1 2 9 4 5/' a.txt

# 4  
Old 02-07-2008
Thanks for the reply,
but my problem is that I don't know the value in advance.
I need to read them and according to their values to change them.

Thanks.
# 5  
Old 02-07-2008
Quote:
Originally Posted by zed
For example, a.txt contains:
start: 1 2 3 4
and I want to change "3" to "9"
the result should be (a.txt):
start: 1 9 3 4 5
If you replace 3 with 9, how do you get 19345. Should it not be 12945 ?

Also, what is the logic ? Would all 3's get replaced by 9 ? Would a 5 follow 4 always ?

Code:
sed -e "/start/{
s/ 3 / 9 /g;s/ 4 [^5]/ 4 5 /g"
}" input.txt

# 6  
Old 02-07-2008
Quote:
Originally Posted by zed
Thanks for the reply,
but my problem is that I don't know the value in advance.
I need to read them and according to their values to change them.

Thanks.
fpmurphy has shown you how you can edit a file with a perl command line one-liner, now you need to supply the logic to figure out what you need to do. Since we can only go by the one sample you provided that is all we can really help you with. So if you want more help you will need to supply more information about what you are trying to do. And please post any code you have written up to now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Edit a file in perl

Hi, I have a file like $ cat abc HDR XXX content XXX content YYY content XXX content YYY content XXX content YYY TRL YYYI want to replace the lines staritng with HDR and TRL For this I have written below code #!/usr/bin/perl -w use strict; open ( FH , "+< abc" ) || die "Can't... (1 Reply)
Discussion started by: sam05121988
1 Replies

3. Shell Programming and Scripting

Edit first line of a text file

Hi friends, Issue1: I have a text file with the first line like this #chrom start end Readcount_A Normalized_Readcount_A ReadcountB Normalized_Readcount_B Fc_A_vs_B pvalue_A_vs_B FDR_A_vs_B Fc_B_vs_A pvalue_B_vs_A FDR_B_vs_A <a href="http://unix.com/">Link</a> How can I change it to the... (11 Replies)
Discussion started by: jacobs.smith
11 Replies

4. Shell Programming and Scripting

Edit a Huge one line file

We have a huge file which has just one really large line; about 500 MB. I want to 1. Count all the occurrences of a phrase 2. Replace the phrase with another. Trying to open it using vi has not helped as it complains that it is too large. Can any script help? Please advise. Thank you, (12 Replies)
Discussion started by: kaushikadya
12 Replies

5. Shell Programming and Scripting

How to edit file to have one line entry?

Hello All, My file content is: DROP TABLE "FACT_WORLD"; CREATE TABLE "FACT_WORLD" ( "AR_ID" INTEGER NOT NULL, "ORG_ID" INTEGER NOT NULL ) DATA CAPTURE NONE COMPRESS YES; I want to change this file to have entries in one... (6 Replies)
Discussion started by: akash2508
6 Replies

6. Shell Programming and Scripting

Edit file content at the specific line.

How to edit file content at the specific line? For example at below The things to edit --> This is line 2. And it is below line 1. This is line 1. This is line 2. # i want to append some words at this row line. How? This is line 3. (8 Replies)
Discussion started by: alvin0618
8 Replies

7. Shell Programming and Scripting

perl edit file

Is there a way to edit a file without opening two files the only method I know is one file for reading from and one file writing to I cannot think of any other ways (4 Replies)
Discussion started by: 3junior
4 Replies

8. Shell Programming and Scripting

help on a perl script to edit file

Hi, sample file looks like this.. <hp> <name> <detail>adsg</detail> ... ... </name><ft>4264</ft> </hp> I need to edit the last but one line using perl script. I want the format to be .. <hp> <name> <detail>adsg</detail> ... ... </name> (9 Replies)
Discussion started by: meghana
9 Replies

9. Shell Programming and Scripting

Edit number of lines in a file to single line

Greetings, I have a file: hostnames.txt which has - # cat hostnames.txt machine1 machine2 I need the output to be saved to a variable as: HOSTNAMELIST=machine1,machine2 Please advise. Thanks, Chiru (3 Replies)
Discussion started by: chiru_h
3 Replies

10. UNIX for Dummies Questions & Answers

edit each line in the file

I am trying to edit each line in a file. The file has several columns delimitted by '|'. I need to take out the last two columns. Each line starts with a unique word through which I am storing the lines in a variable and cutting the last two colums. But, when I am echoing the line, it is... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question