Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-29-2005, 02:00 PM
|
#1
|
Member
Registered: Jun 2005
Posts: 70
Rep:
|
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?
|
|
|
06-29-2005, 03:01 PM
|
#2
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
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.
|
|
|
06-29-2005, 03:20 PM
|
#3
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
If your sed is version 4.x use the -i flag.
|
|
|
06-29-2005, 06:34 PM
|
#4
|
Member
Registered: Jun 2005
Posts: 70
Original Poster
Rep:
|
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.
|
|
|
06-29-2005, 06:58 PM
|
#5
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
sed -i -e ....
[edit]
-i stands for in-place, no redirection at all required.
Read the man page, quite good reading.
[/edit]
Cheers,
Tink
Last edited by Tinkster; 06-29-2005 at 07:03 PM.
|
|
|
06-30-2005, 09:28 AM
|
#7
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
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 12:36 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|