LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-29-2005, 02:00 PM   #1
osio
Member
 
Registered: Jun 2005
Posts: 70

Rep: Reputation: 15
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?
 
Old 06-29-2005, 03:01 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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.
 
Old 06-29-2005, 03:20 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
If your sed is version 4.x use the -i flag.
 
Old 06-29-2005, 06:34 PM   #4
osio
Member
 
Registered: Jun 2005
Posts: 70

Original Poster
Rep: Reputation: 15
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.
 
Old 06-29-2005, 06:58 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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.
 
Old 06-30-2005, 03:09 AM   #6
osio
Member
 
Registered: Jun 2005
Posts: 70

Original Poster
Rep: Reputation: 15
#!/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
 
Old 06-30-2005, 09:28 AM   #7
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
oh wow, that is a handy thing to know.

Tinkster, your sed sorcerer. I always see you giving good tips on sed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration