LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-16-2012, 11:36 PM   #1
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Rep: Reputation: Disabled
copy file and append string in the process


Hi,

I use this command:

Code:
cp input-file.ext /tmp/tmp.ext && echo -e '\nfinishing line' >> /tmp/tmp.ext
to append a line to a file and copy it to temp folder without changing input file.

Is there better way to do the same?
 
Old 01-17-2012, 12:00 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, possibly
Code:
# instead of
... echo -e '\nfinishing line' ...

# you can just do
... echo "finishing line" ...
ie you'll get a newline anyway that way, unless you actually wanted a blank line inserted as well?
 
Old 01-17-2012, 12:27 AM   #3
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
OK, thanks, that's slight improvement

I was thinking more if there is some command so that I could append line while copying file

It's nothing critical, I'm just curious if I'm missing some more elegant way, as I'm new to Linux and I imagine there is terminal command for any imaginable scenario
 
Old 01-17-2012, 12:40 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
If all you want is a non-traditional linux-shell-trick kind of command, then you can use sed instead of cp. For instance:

Code:
sed '$ a finishing line' input-file.ext > /tmp/tmp.ext
EDIT:
I guess I should explain what's happening. I don't know how familiar you are with sed (if at all). So please excuse me if you know any of this already.

The sed command modifies a stream of text. It does so by looking for matching patterns and executing commands when a pattern is matched. In this particular case, sed is told to look for the end of the stream of text (represented by the '$'), append text when found (represented by the 'a'), and the text to append (represented by the 'finishing line').

The sed command reads the stream of text from input-file.ext. The normal behavior of sed is to print the modified stream to the screen/stdout. The output redirector ('>') tells the shell to send sed's output to the file /tmp/tmp.ext instead.

Keep in mind, the above sed command is very basic. There's much more sed can do, but it might take a while for you to become familiar with the patterns/commands.

Last edited by Dark_Helmet; 01-17-2012 at 12:50 AM.
 
Old 01-17-2012, 12:50 AM   #5
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks I was looking for that kind of simplification
sed seems so flexible...
 
Old 01-17-2012, 02:26 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Or another way:
Code:
(cat test2;echo "finishing line")>test3
 
Old 01-17-2012, 02:06 PM   #7
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
And don't miss the awk way!
Code:
awk '1;END{print "finishing line"}' input-file.ext > /tmp/tmp.ext
As you can see there are a lot of ways to accomplish the same task in *nix: it's a matter of taste, style, performance, compatibility, etc. etc.
 
Old 01-18-2012, 09:13 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by chrism01 View Post
Well, possibly
Code:
# instead of
... echo -e '\nfinishing line' ...

# you can just do
... echo "finishing line" ...
ie you'll get a newline anyway that way, unless you actually wanted a blank line inserted as well?
Actually both versions suffer from the same problem. echo, on its own, only inserts a newline after the string, not before it.

So if the file you're modifying does not have a final newline, then the first command above will supply it before inserting the text. But if it does have a newline already, then you'll end up with an extra blank line.

The second command does the opposite, of course. It will come out properly if there's already a newline, but if not then the new string will be concatenated with the final line of text.

To safely avoid this problem you should ensure that any trailing newlines are removed from the text before the operation, and then supply your own.

Here's one way, using command substitution and printf.
Code:
printf '%s\n%s\n' "$(<input-file.ext)" "finishing line" > newfile
One of the side-effects of $(..) is that it removes trailing newlines from the substituted text if it finds one. And bash lets you use < inside of them to redirect the contents of a file directly (in other shells you can use cat). Finally, quoting the whole thing means printf sees the entire contents of the file (minus trailing newlines) as a single input argument.

I probably wouldn't use it if the file is exceptionally big though, as the whole thing is, I believe, temporarily copied to shell memory. Use the previously supplied sed or awk commands in that case.
 
  


Reply



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
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-27-2011 11:13 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
How to append a string to a file name? anandkj Linux - Newbie 2 01-10-2008 08:10 PM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 07:17 PM
How can I append text to a string in a file HGeneAnthony Linux - Newbie 4 03-01-2007 12:16 PM

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

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

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