batch append string to the end of a determined line in text files
I have text files of several thousand rows and I need to append a string (.php) at the end of the first line of all of them
I've managed to understand the sed program to the point of writing a working command for stdout: sed -e '1,1s/$/.php/1' It works great if I write the output files to a different directory (made for the occasion): #!/bin/bash for X in *.txt do sed -e '1/1s/$/.php/1' $X >> ~/appended/$X done If I write $X >> $X, the filenames of the current directory remain but their text is all gone. How can I output the file with the same name to the same directory? |
i run in to this problem a lot to. im not sure what the clean way to do it is, but an ugly fix is to:
#! /bin/bash for X in *.txt do sed -e '1/1s/$/.php/1' $X > tmp mv tmp $X done you only want to use one '>' so it will overwrite tmp everytime instead of appending to it. |
If your sed is version 4.x use the -i flag.
|
Tinkster,
#!/bin/bash for X in *.txt do sed -e '1/1s/$/.php/1i' $X > $X done emptied the output *.txt files. Does w for write prevent this? sirclif, the tmp trick works as expected . When i tested it, it took some time to move all the huge files. I guess that mv parses all the file length or the memory reads through all over again. I wonder if there is a way of making the bash edit the first line and go. |
sed -i -e ....
[edit] -i stands for in-place, no redirection at all required. Read the man page, quite good reading. [/edit] Cheers, Tink |
#!/bin/bash
for X in *.txt do sed -i -e '1/1s/$/.php/1' $X done did the job. Thanks! Also, advanced reads: Manipulating Strings with sed http://www.unet.univie.ac.at/aix/aix...trings_sed.htm sed tutorial http://pegasus.rutgers.edu/~elflord/unix/sed.html handy one-liners for sed http://www.student.northpark.edu/pem...d/sed1line.txt executable sed script end of http://www.cs.hmc.edu/tech_docs/qref/sed.html |
oh wow, that is a handy thing to know.
Tinkster, your sed sorcerer. I always see you giving good tips on sed. |
All times are GMT -5. The time now is 11:45 PM. |