LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-17-2007, 08:00 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
Shell scripting: How to add characters at the end of the line


How do I add new characters to the end of a line?

For example if I do:
Code:
echo testing > test
echo testing >> test
I get:
Code:
testing
testing
I want it to be:
Code:
testingtesting
Basically I'm writing a script working with a CSV file. If a certain condition is met, I need to put at the end of the line an asterisk.

Last edited by Micro420; 05-17-2007 at 08:02 PM.
 
Old 05-17-2007, 08:43 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
You could try not putting an end-of-line in your first echo. To keep the option open for doing it again, donít put an end-of-line in any subsequent echoís.

E.g.,
Code:
echo -n testing > test
echo -n testing >> test
Alternatively, you can use sed (which will work even when you want to add characters to a line in the middle of the file). If you need help with that, just post.

Last edited by osor; 05-17-2007 at 09:51 PM. Reason: typo
 
Old 05-17-2007, 09:14 PM   #3
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Perfect! Exactly what I needed!!!!!!!!! Thank you! I'll try the echo -n first. I'll ask again if I need help with sed

Last edited by Micro420; 05-17-2007 at 09:16 PM.
 
Old 05-17-2007, 10:44 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
another way is using printf
 
Old 05-18-2007, 01:03 AM   #5
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Awesome! Good to know about printf. It's always good to be able to do something more than one ways. I will try my script tomorrow on these CSV files at work.
 
Old 05-18-2007, 01:10 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Just an FYI. The '-n' option may not be present for some unix users. So printf tends to be used instead to make scripts more portable.
 
Old 05-18-2007, 01:33 AM   #7
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
In general, are there any significant differences between using printf and echo? What are the advantages and disadvantages besides certain Unix shells not understanding echo -n?
 
Old 05-18-2007, 01:56 AM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
For your specific qn, prob not a lot, but obviously printf gives you nice field formatting/control.
You may need this to construct a properly formed CSV, eg put quote marks around any data field that contains a comma.
Also, if you ever have to change the separator eg use tabs.
 
  


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
Shell scripting :: comment N line in vi editor mahabooba Programming 4 07-28-2006 05:58 AM
command line options for firefox for shell scripting. dr_zayus69 Programming 1 05-25-2005 11:36 AM
line addressing with grep (shell scripting) j2dizzo Linux - General 13 03-03-2004 09:36 AM
shell scripting (special characters) synapse Mandriva 4 02-18-2004 06:05 AM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM


All times are GMT -5. The time now is 04:42 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