Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replacing content of a particular line number

Status
Not open for further replies.

duncdude

Programmer
Jul 28, 2003
1,979
GB
i'm sure this is a pretty straightforward question - but i can't seem to work out this problem

supposing i have a textual data file '3cols.txt' as follows:-

123 abc DEFG
234 def GHIJ
345 ghi JKLM
456 jkl MNOP
567 mno PQRS

and i need to replace the 4th line with:-

456 new DATA

i.e. what is the easiest way to achieve this - i.e. all i want to give the script is a pointer to the line i want to change

what is the easiest way to go about this?

i need to end up with the original file with this content:-

123 abc DEFG
234 def GHIJ
345 ghi JKLM
456 new DATA
567 mno PQRS
 
And what is the criteria ?
4th line ? 1st column='456' ? ...
man ed

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
sorry - i tried hard to be as concise as i possibly could be

1. all i will have is a line number
2. i wish to replace this line (the entire line) - with other content
3. i wish this to be saved over the original file

i could do this with Perl but would rather tackle with command-line utilities. I thought maybe sed could do this?

thanks


Kind Regards
Duncan
 
OOps, sorry, I meant man ex:
i=4 # line number
echo "${i}s!.*!new data here!\nwq" | ex -s /path/to/file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV

Thanks for the reply

Unfortunately if i run
echo "${i}s!.*!new data here!\nwq" | ex -s /path/to/file

i get... -bash: !.*!new: event not found

But if i replace the exclamation marks with hashes it doesn't error?

I can't, however, work out how to set the $i equal to 4 (for example) on a single line... can you explain? Also, if i replace the $[i} with a number 4, it still does not seem to work. Please help!


Kind Regards
Duncan
 
Sorry I don't know bash but ksh, could you use it (less buggy I guess, at least more portable)?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
darn! i don't seem to have ksh!!!

i'm on OSX

... thanks for your help so far PHV - i am grateful


Kind Regards
Duncan
 
feherke - i love that solution - looks like just what i need ... but my sed kicks up about it sed: 1: "data.txt": extra characters at the end of l command - aaarrrggghhh!

how do i find the version!? sed -v does not work...

SamBones - excellent, i did not know that. i am pretty nervous of Tiger at present though as i hear quite a few problems that need ironing out. although i would like to be able to solve this problem, i can't be sure that i would lose functionality in other areas. what do you reckon?

... Tiger does look good though

Thank you both!


Kind Regards
Duncan
 
Hi

There is only GNU-style option for that : --version.

The error message looks like [tt]sed[/tt] tried to interpret the data file as script file. Interesting incompatibility.

Feherke.
 
-bash: !.*!new: event not found
And this ?
echo "4s/.*/new data here/\nwq" | ex -s /path/to/file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I've only been on Tiger for a week or so, but I love it! I had do download a few things to get where I wanted to be (i.e. Xcode 2.0), but it rocks!

Hope this helps.
 
Or, alternatively awk '{print} NR==4 {print "456 new DATA"}' filename > output.

Annihilannic.
 
Oh, sorry, replacing line 4... uhmm... awk 'NR==4 {print "456 new DATA"; next} {print}' filename > output.

Annihilannic.
 
Annihilannic, here one requirement:
i need to end up with the original file with this content
awk '{print (NR==4 ? "456 new DATA" : $0)}' filename > out.$$ && mv out.$$ filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top