LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-24-2009, 10:26 PM   #1
guessity
Member
 
Registered: Dec 2009
Posts: 41

Rep: Reputation: 15
Unhappy 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?
 
Old 12-24-2009, 11:06 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Do you want to do it with a program (in which case, in which language?) or in an editor (in which case, which editor?).
 
Old 12-24-2009, 11:36 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ awk '/<author>/{o=$0}/<content>/{$0=$0"\n"o}1' file
 
Old 12-24-2009, 11:50 PM   #4
xaler
LQ Newbie
 
Registered: May 2009
Location: the zero world
Distribution: F11, U9.04, Gentoo
Posts: 20

Rep: Reputation: 1
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)
 
Old 12-24-2009, 11:57 PM   #5
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
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 View Post
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. (
 
Old 12-25-2009, 12:29 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by guessity View Post
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
 
Old 12-25-2009, 12:38 AM   #7
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
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 View Post
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
 
Old 12-25-2009, 01:06 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by guessity
Any idea where I went wrong?
man mv and see how its used.
 
Old 12-25-2009, 01:09 AM   #9
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
man mv and see how its used.
I didnt get you.

I am a noob and learning. Have to do it for my project. (
 
Old 12-25-2009, 01:23 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
Old 12-25-2009, 01:25 AM   #11
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
I figured it out. Sorry for being so dumb. Thx for all your help.
 
  


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
How to copy/paste a line in nano editor smp0219 Linux - Newbie 10 08-06-2019 03:24 PM
LXer: xclip Does Copy-and-Paste on the Linux Command Line LXer Syndicated Linux News 0 07-03-2009 01:00 AM
How to copy-paste on command line? jameshao Linux - General 3 02-02-2009 01:14 AM
How do you copy and paste a line in VI? Thaidog Linux - General 3 05-08-2005 05:46 AM
Copy Paste under command line? Stingreen Linux - General 7 02-10-2002 06:46 PM

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

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