Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-09-2011, 07:57 AM   #16
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983

Your original question was how to avoid appending to a new line, you know, which is why we gave you lines that don't.
f="$(<$fileName)" ; echo "${f%$'\n'} $appendThis" >$fileName
                                ^   ^             ^
                               #1  #2            #3
#1) This parameter substitution pattern removes a single newline from the end of the variable text if it exists. Remember that newline is just an ascii character (0x0A--LineFeed) like any other, just a non-printing one, so text manipulations can affect it as well.

But, mea culpa, I've discovered the parameter substitution isn't actually necessary, because it turns out that the $(..) process substitution used to set f automatically removes any trailing newlines anyway. So echo "$f $appendThis" works just the same.

If you want to be able to have it remove newlines by default, but also be able to voluntarily append one if you want, insert a newline at the beginning of the replacement variable instead.
#2) The echo command prints the contents of $f, then a space, then the contents of $appendThis. Simply remove the space to have them concatenated directly together; "$f$appendThis".

The sed command used a similar technique; the replacement string was pre-appended by a space so that it wouldn't concatenate directly with the original text. I pointed that out in my first post.

Note that you can also add spaces to the beginning of the replacement variable instead if you want them to appear only on demand.

#3) > redirects the output to the specified file, overwriting anything that existed previously. Compare to >> which appends text to the current file instead.

Pretty much every shell tool you use will print to stdout and not modify the original by default. You need to use a redirect, or a tool's built-in ability to output to file or edit the original if it exists, like sed's -i, to print back to a file. Sometimes you need to print the output to a temporary file first, then replace the original with it afterwards (this is actually what sed is doing when you use -i).

Finally, as for the size of the file, it's mostly limited by the amount of RAM you have. I don't think bash has any hard limit for string variables, so you should be good up to several megabytes at least. It's really only a concern with super-large (gigabyte-sized) files that can overwhelm the system's memory capacity.

Last edited by David the H.; 04-10-2011 at 12:38 PM. Reason: fixed error and substituted a more appropriate link


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
Command for appending a text at the start of every line and save it another file ssgupta Linux - Newbie 5 03-10-2011 08:35 AM
shell script- appending line by line metis Programming 5 06-21-2010 10:53 PM
need to write a shell script for appending text into each line of a file nandakv4 Programming 9 08-15-2009 03:32 AM
C++ Line Comparisons and Appending in another file vk10 Programming 3 09-26-2008 07:50 PM
appending line at the last line of a file .. vks_agra Linux - General 13 06-20-2006 07:51 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:35 PM.

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