The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing the last data of each line ina file jisha Shell Programming and Scripting 6 08-04-2008 04:47 AM
Replacing a line in a file - HELP!!! maxmave Shell Programming and Scripting 1 06-04-2008 08:55 PM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-27-2008 11:43 PM
replacing a line of unknown charecters in a file malavm Shell Programming and Scripting 12 07-26-2007 01:25 AM
replacing specific lines in a file hcclnoodles Shell Programming and Scripting 6 09-30-2006 08:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-20-2006
Terrible
Guest
 

Posts: n/a
Stumble this Post!
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-21-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
Stumble this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 06-26-2006
Registered User
 

Join Date: Mar 2006
Location: Nashville
Posts: 77
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 06-28-2006
Terrible
Guest
 

Posts: n/a
Stumble this Post!
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0