LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help appending text to file, using variables (https://www.linuxquestions.org/questions/linux-newbie-8/help-appending-text-to-file-using-variables-4175429647/)

soupmagnet 09-29-2012 04:16 PM

Help appending text to file, using variables
 
I know how to append text to a file, but I need it appended to the same line as the original text and there are variables involved.

Code:

#!/bin/bash

dir=/path/to/directory
file=filename

#Normally I can use...

cat "/path/to/directory" > filename.xxx


#But I need to use variables

cat $dir > $filename".xxx"


#But that doesn't work because bash thinks I'm trying to cat the directory itself and not the path to the directory in text form.


#I've tried echo...

echo "$dir" >> $filename".xxx"

#But it always puts the text on a new line.



I've seen some tutorials using 'sed', but the text to be appended is always in single quotes, which will not allow expansion of the variables.

porkytech 09-29-2012 07:01 PM

echo is the command, not cat
 
your cat of a directory is fallacious... it shouldn't success...
cat just concats files.

And the newline appended can be suprimed with the -n flag

so
echo -n hi >> msg
echo -n " dude" >> msg
cat msg
[hi dude]

soupmagnet 09-29-2012 08:35 PM

Quote:

Originally Posted by porkytech (Post 4792850)
your cat of a directory is fallacious... it shouldn't success...
cat just concats files.

And the newline appended can be suprimed with the -n flag

so
echo -n hi >> msg
echo -n " dude" >> msg
cat msg
[hi dude]

I'm not trying to 'cat' a directory, only a string of text (that happens to be the path of a directory) that is also saved as a variable. I want to be able to call the variable and append that string to the end of the line on a single line file. I may be confused about the proper use of 'cat', but I've tried echo -n $dir >> $filename".xxx", and it was unsuccessful, as it still added the newline.

Here's how I have it written..

Code:

echo -n "  $dir".img >> filename".md5"
Instead of getting this...
Code:

09874508909870987985234...  /path/to/directory.img
I'm getting this...
Code:

09874508909870987985234...
  /path/to/directory.img


colucix 09-30-2012 02:46 AM

Until you use shell redirection the new piece of text will be appended to a new line. You need a editor, instead. And what's better than sed? :)
Code:

sed -i "s:$:  $dir:" $file

rknichols 09-30-2012 09:15 AM

deleted

porkytech 10-02-2012 02:07 PM

the newline was already there
 
Quote:

Originally Posted by soupmagnet (Post 4792915)
... but I've tried echo -n $dir >> $filename".xxx", and it was unsuccessful, as it still added the newline.

Here's how I have it written..

Code:

echo -n "  $dir".img >> filename".md5"
Instead of getting this...
Code:

09874508909870987985234...  /path/to/directory.img
I'm getting this...
Code:

09874508909870987985234...
  /path/to/directory.img


That's because the newline was already there.

You have to "sanitize" the md5 file (or any file):

a simple solution would be to take the whole file but the last line.
Write those lines as they are and the append the last line without newline character.

Code:

# copy file from $1 to $2, removing the ending newline, if any
#
takeoffnewline () {
  head -n -1 $1 > $2
  echo -n "`tail -1 $1`" >> $2
}

with this you can do the task

Code:

takeoffnewline mysum.md5 mysum2.md5
echo -n $path >> mysum2.md5



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