LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 08-03-2005, 02:50 PM   #1
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
bash help? append text to textfile entries


I need to write a bash script, perl would be ok as well that would do this:

I have a flat text file with thousands of entries such as this:

file1
file2
big_file
little_file
file

And so on.

What I need to do is append ".eps" at the end of these entries that are listed above, and like I said there are thousands in each file so I don't want to have to do it manually.

Any help or direction would be appreciated.

Thanks,

Eric
 
Old 08-03-2005, 02:54 PM   #2
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
One simple sed should do it.

sed -i -e "s@.*@&.eps@g" yourfile
 
Old 08-03-2005, 03:23 PM   #3
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by kjordan
One simple sed should do it.

sed -i -e "s@.*@&.eps@g" yourfile
That Kind of worked but now the entries look like this:

Aamco_Transmissions^M.eps
AAPS^M.eps
Aarons_Rental_Furniture^M.eps
AARP^M.eps
American_Bikers_Association^M.eps
Abbott^M.eps
ABC^M.eps

I don't know why the ^M has been added. But we can't have it there.
 
Old 08-03-2005, 03:36 PM   #4
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Was your original file made on Windows? If so, you'll need to convert it: http://www.lugbz.org/content/section...ommands14.html

Last edited by kjordan; 08-03-2005 at 03:40 PM.
 
Old 08-03-2005, 03:38 PM   #5
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
You should first try calling :
Code:
dos2unix filename
The ^M are there because the file were created in a Windows environment
 
Old 08-03-2005, 03:40 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think something similar this will do it:
sed s/^M/""/g

(i'm at work so i haven't tested it.)

not sure if the 'ctrl-M' are a unix new line (single ascii char) or is actually a carot (^) and an 'M'.

hope this works,
schneidz
 
Old 08-03-2005, 03:42 PM   #7
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Quote:
Originally posted by schneidz
i think something similar this will do it:
sed s/^M/""/g

(i'm at work so i haven't tested it.)

not sure if the 'ctrl-M' are a unix new line (single ascii char) or is actually a carot (^) and an 'M'.

hope this works,
schneidz
ctrl-v ctrl-m will do the ^M.
 
Old 08-03-2005, 06:09 PM   #8
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 15
Posts: 546

Rep: Reputation: 43
One method to convert dos text files into UNIX is

sed -e 's/.$//' mydos.txt > myunix.txt # this will replace (erase) the carriage return from the end of the lines.

For your script I would try

#!/bin/sh
for i in `cat myfile`
do
echo "$i.eps" >> myfile.eps
done

myfile of course is the name of the file you want to edit

Hope this helps
Ciao

Last edited by urka58; 08-03-2005 at 06:16 PM.
 
Old 08-07-2005, 06:01 AM   #9
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
You guys like to make work! Just use
Code:
sed 's/Ctl-VCtl-M*$/.eps/' yourfile >newfile
Ctl-V and Ctl-M are typed by holding down the control key and typing 'V' and then 'M'. It'll work even if there are no ^Ms in the file. You may also find the last line in the file has a ^Z in it - you can safely delete this.

Last edited by eddiebaby1023; 08-07-2005 at 06:03 AM.
 
Old 08-07-2005, 07:01 AM   #10
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Why don't use just dos2unix ? I used it succefully many times on text files and works great.

(just curious)
 
  


Reply



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
pipe output to append to a text file davee Linux - Newbie 5 03-22-2016 07:44 PM
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
BASH text executable blackbox Linux - Newbie 1 02-28-2005 06:49 AM
Text input in bash? pk21 Programming 12 06-05-2003 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration