LinuxQuestions.org
Help answer threads with 0 replies.
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 10-31-2008, 02:32 PM   #1
tmbrwolf53
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Rep: Reputation: 0
Unhappy Need help appending text to lines in a file using vi editor


I'm somewhat new to Linux and very new to the vi editor. Here is my issue:
I have a file that I want to append text to the end of each line within the file. The text to be added is the same.
How do I setup a command to mass append the text to each line? I have played with substitution but have not been able to append data at the end.

Here is an example. I have a file that looks like this:

du -sh us1234
du -sh us5678
du -sh us9abc
du -sh usefgh
du -sh usijgl
du -sh usmnop
du -sh usqrst

I want to append the following to each line:
" >> /tmp/output.txt"
So each line will look like:

du -sh us1234 >> /tmp/output.txt
du -sh us5678 >> /tmp/output.txt
du -sh us9abc >> /tmp/output.txt
du -sh usefgh >> /tmp/output.txt
du -sh usijgl >> /tmp/output.txt
du -sh usmnop >> /tmp/output.txt
du -sh usqrst >> /tmp/output.txt

How do I accomplish this with a single command?
 
Old 10-31-2008, 02:38 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Before:
Quote:
du -sh us1234
du -sh us5678
du -sh us9abc
du -sh usefgh
du -sh usijgl
du -sh usmnop
du -sh usqrst
Command:
Code:
:1,$s/$/ >> \/tmp\/output\.txt/
After:
Quote:
du -sh us1234 >> /tmp/output.txt
du -sh us5678 >> /tmp/output.txt
du -sh us9abc >> /tmp/output.txt
du -sh usefgh >> /tmp/output.txt
du -sh usijgl >> /tmp/output.txt
du -sh usmnop >> /tmp/output.txt
du -sh usqrst >> /tmp/output.txt
Let's break it down:
1. <Esc>, : Go into command mode
2. 1,$ Execute this command on every line between 1 and End of file ("$")
3. s/$/ >> \/tmp\/output\.txt/ The command is "substitute" ("s/X/Y/")
4. /$/ >> \/tmp\/output\.txt/ This means:
a) substitute "end of line" ($)
b) for " >> /tmp/output.txt"
c) Escape the "/" and "." metacharacter (using "\")

'Hope that helps .. PSM

Last edited by paulsm4; 10-31-2008 at 02:43 PM.
 
Old 10-31-2008, 02:50 PM   #3
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 give vi a miss and use sed ;}

Code:
sed -i 's@$@>> /tmp/output.txt@' file
Cheers,
Tink
 
Old 10-31-2008, 03:25 PM   #4
tmbrwolf53
LQ Newbie
 
Registered: Sep 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you paulsm4 and tinkster. Both worked
 
  


Reply

Tags
editor, vi


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
Join lines in text file vidyashankara Linux - General 10 12-21-2009 03:17 PM
Appending ends of lines in sed Agentrooker Programming 8 08-07-2008 11:19 PM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
adding text to lines in a file tpreitano Linux - General 2 10-04-2005 09:30 AM

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

All times are GMT -5. The time now is 12:43 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