-   Programming (
-   -   Shell scripting: How to add characters at the end of the line (

Micro420 05-17-2007 08:00 PM

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:

echo testing > test
echo testing >> test

I get:


I want it to be:

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.

osor 05-17-2007 08:43 PM

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.


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.

Micro420 05-17-2007 09:14 PM

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

ghostdog74 05-17-2007 10:44 PM

another way is using printf

Micro420 05-18-2007 01:03 AM

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.

jschiwal 05-18-2007 01:10 AM

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.

Micro420 05-18-2007 01:33 AM

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?

chrism01 05-18-2007 01:56 AM

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.

All times are GMT -5. The time now is 03:29 AM.