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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-24-2009, 10:26 PM
|
#1
|
Member
Registered: Dec 2009
Posts: 41
Rep:
|
How to copy-paste part of line within the file?
Hi,
i am a noob to linux and need some help.
I am having a file called 2009.txt and the part of the content is -
Code:
<articles>
<article>
<date>2009-12-13</date>
<time>22:11:58</time>
<author>DPA</author>
<headline>
About 100 climate protesters detained for Copenhagen march
</headline>
<cats>International</cats>
<subcats>Crime/Disaster/Accident</subcats>
<content>
<p align='justify'>
Copenhagen, Dec 13 Danish police
I basically want to copy and paste the word "DPA" which is within <author>DPA</author> to the the <content> line.
The Author could be anything and not necessarily DPA so is there a way to do it in Linux?
|
|
|
12-24-2009, 11:06 PM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Do you want to do it with a program (in which case, in which language?) or in an editor (in which case, which editor?).
|
|
|
12-24-2009, 11:36 PM
|
#3
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
$ awk '/<author>/{o=$0}/<content>/{$0=$0"\n"o}1' file
|
|
|
12-24-2009, 11:50 PM
|
#4
|
LQ Newbie
Registered: May 2009
Location: the zero world
Distribution: F11, U9.04, Gentoo
Posts: 20
Rep:
|
in an editor called vi, start by vi <filename> --> place cursor below D of DPA --> <ESC> Key once (not necessarily) --> type "yw" (without quotes) which copies one word --> goto content line and place cursor where you want and type "p" (without quotes)
|
|
|
12-24-2009, 11:57 PM
|
#5
|
Member
Registered: Dec 2009
Posts: 41
Original Poster
Rep:
|
any idea how do I save(modify) the change in the same file? Its only displaying me the results.
Is it possible to copy only the text within <author> </author> part? I dont want to copy <author> word.
Quote:
Originally Posted by ghostdog74
Code:
$ awk '/<author>/{o=$0}/<content>/{$0=$0"\n"o}1' file
|
I am looking for either command or script. I dont want to do it manually because i have tons of files to fix it.  (
|
|
|
12-25-2009, 12:29 AM
|
#6
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by guessity
any idea how do I save(modify) the change in the same file? Its only displaying me the results.
|
use the redirection operator to redirect to a temp file, then use mv to rename it back to original
Quote:
Is it possible to copy only the text within <author> </author> part? I dont want to copy <author> word.
|
Code:
awk '/<author>/{gsub(/<author>|<\/author>/,"");o=$0}/<content>/{$0=$0"\n"o}1' file
|
|
|
12-25-2009, 12:38 AM
|
#7
|
Member
Registered: Dec 2009
Posts: 41
Original Poster
Rep:
|
Hi,
Thx, I used this script to make the change -
Code:
find *.txt -type f | while read file
do
awk '/<author>/{gsub(/<author>|<\/author>/,"");o=$0}/<content>/{$0=$0"\n"o}1' $file >$file.$$
mv $file.$$ >$file
done
I ended up with this error -
Code:
mv: missing destination file operand after `20099.txt.26848'
Try `mv --help' for more information.
mv: missing destination file operand after `2009.txt.26848'
Try `mv --help' for more information.
Any idea where I went wrong?
Quote:
Originally Posted by ghostdog74
use the redirection operator to redirect to a temp file, then use mv to rename it back to original
Code:
awk '/<author>/{gsub(/<author>|<\/author>/,"");o=$0}/<content>/{$0=$0"\n"o}1' file
|
|
|
|
12-25-2009, 01:06 AM
|
#8
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by guessity
Any idea where I went wrong?
|
man mv and see how its used.
|
|
|
12-25-2009, 01:09 AM
|
#9
|
Member
Registered: Dec 2009
Posts: 41
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
man mv and see how its used.
|
I didnt get you.
I am a noob and learning. Have to do it for my project.  (
|
|
|
12-25-2009, 01:23 AM
|
#10
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
the problem is with your mv command. type man mv on your command prompt. then read about how its used. You will not find ">" there.
|
|
1 members found this post helpful.
|
12-25-2009, 01:25 AM
|
#11
|
Member
Registered: Dec 2009
Posts: 41
Original Poster
Rep:
|
I figured it out. Sorry for being so dumb. Thx for all your help.
|
|
|
All times are GMT -5. The time now is 10:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|