replacing first line or lines in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users replacing first line or lines in a file
# 1  
Old 06-21-2006
replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file?


see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can, but i keep circling around and dont know the exact command.

what i'm trying to do is run a script remotely that checks diskspace on a server. if diskspac is above a certain percentage this script will look at the file i'm trying to edit and delete files on the disk based on the threshold, number of days, given in the file.

this may be confusing but really all i need is the one liner that will just replace a line for me if the script fails to free up enough space. thanks guys.
Terrible
# 2  
Old 06-21-2006
Are you looking for something like this:
Code:
# cat test
this is line 1
this is line 2
this is line 3
# sed '1s/this is line/that is line/' test
that is line 1
this is line 2
this is line 3
# sed '2s/this is line/that is line/' test
this is line 1
that is line 2
this is line 3

Members may be able to help you better if you posted a part of the file you are working with and exactly what you are trying to do with the file.
# 3  
Old 06-26-2006
Since the source file and destination file are the same, consider doing something like this.

Input file called inputfile.txt contains just the word 'This'

commandline
perl -pi -w -e 's/This/I did it/g;' inputfile.txt
#This replaces the word This with I did it all from the commandline

inside script called myscript.pl
perl -pi -w -e 's/This/I did it/g;' $1
#Put this perl line inside a script and run it. Passing your input file to the script

Run script from commandline.

#myscript.pl inputfile.txt

-X
# 4  
Old 06-28-2006
Quote:
Originally Posted by x96riley3
Since the source file and destination file are the same, consider doing something like this.

Input file called inputfile.txt contains just the word 'This'

commandline
perl -pi -w -e 's/This/I did it/g;' inputfile.txt
#This replaces the word This with I did it all from the commandline

inside script called myscript.pl
perl -pi -w -e 's/This/I did it/g;' $1
#Put this perl line inside a script and run it. Passing your input file to the script

Run script from commandline.

#myscript.pl inputfile.txt

-X


thanks for your suggestions. what i ended up doing was just reconstructing the file using a synchronized copy of the original. what i was trying to edit couldn't be done with sed or awk. i tried but it just couldn't be done. so i just used a combination of echo, awk, tee and a variety of other commands to draw out out the wanted line and then appended the rest of the file content to the end of that file
Terrible
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching and Replacing file lines starting with $

Here is the task that I was presented with: I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck. The input deck (deckToChange in the code below) comes with 2 separate files. File 1... (5 Replies)
Discussion started by: tiktak292
5 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

5. UNIX for Dummies Questions & Answers

Replacing 2 lines by single line

Hi I have a file with below content : a b S I need to replace the lines which have a and b continuously by d. d S I have used the below code tr '\n' '#'<file|sed. 's/a#b/d/g's?|tr '#' '\n' where # is not occurring anywhere in the file.. Is there any other efficient way to do this? ... (7 Replies)
Discussion started by: pandeesh
7 Replies

6. Shell Programming and Scripting

replacing multi lines with 1 line

I have an xml file that is stripped down to output that looks bacically like; <!-- TABLEA header --> <tablea> some fields </tablea> <!-- TABLEB header --> <!-- TABLEC header --> <tablec> some fields </tablec> I want to remove the header... (3 Replies)
Discussion started by: Griffs_Revenge
3 Replies

7. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

8. Shell Programming and Scripting

Replacing Block of lines in a text file

Dear All, Regards of the Day. I have a text file with some functions: Function1 { parameter 1 parameter 2 parameter 3 } end Function2 { parameter 1 parameter 2 parameter 3 } (1 Reply)
Discussion started by: ashisharora
1 Replies

9. Shell Programming and Scripting

replacing multiple lines with single line

Can any one give me the idea on replacing multiple blank lines with a single blank line? Please conside it for a file having more than 100 number of characters. Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

10. Shell Programming and Scripting

replacing specific lines in a file

Hi there I have a file which has the lines # Serial number for hostid EXP_SERIAL_="" These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with # Serial number for hostid $var1 EXP_SERIAL_$var1="$var2" Is there a quick and simple... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question

Featured Tech Videos