LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-29-2012, 04:16 PM   #1
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Rep: Reputation: Disabled
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.

Last edited by soupmagnet; 09-29-2012 at 04:25 PM.
 
Old 09-29-2012, 07:01 PM   #2
porkytech
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
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]
 
Old 09-29-2012, 08:35 PM   #3
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by porkytech View Post
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

Last edited by soupmagnet; 09-29-2012 at 10:00 PM.
 
Old 09-30-2012, 02:46 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
1 members found this post helpful.
Old 09-30-2012, 09:15 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
deleted

Last edited by rknichols; 09-30-2012 at 09:21 AM. Reason: Got focused on a coding issue and overlooked the actual problem.
 
Old 10-02-2012, 02:07 PM   #6
porkytech
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Thumbs up the newline was already there

Quote:
Originally Posted by soupmagnet View Post
... 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

Last edited by porkytech; 10-02-2012 at 02:28 PM.
 
  


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
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
[SOLVED] Variables in text file, how to get their values when printing out the text? idaham Linux - General 2 04-14-2010 03:28 AM
Reading variables from a text file in C++ Mork2k4 Programming 7 09-09-2009 09:54 AM
need to write a shell script for appending text into each line of a file nandakv4 Programming 9 08-15-2009 03:32 AM
Need help appending text to lines in a file using vi editor tmbrwolf53 Linux - Newbie 3 10-31-2008 03:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:16 PM.

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