LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to copy-paste part of line within the file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-copy-paste-part-of-line-within-the-file-777971/)

guessity 12-24-2009 10:26 PM

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?

catkin 12-24-2009 11:06 PM

Do you want to do it with a program (in which case, in which language?) or in an editor (in which case, which editor?).

ghostdog74 12-24-2009 11:36 PM

Code:

$ awk '/<author>/{o=$0}/<content>/{$0=$0"\n"o}1' file

xaler 12-24-2009 11:50 PM

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)

guessity 12-24-2009 11:57 PM

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 (Post 3804075)
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. :((

ghostdog74 12-25-2009 12:29 AM

Quote:

Originally Posted by guessity (Post 3804089)
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

guessity 12-25-2009 12:38 AM

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 (Post 3804106)
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


ghostdog74 12-25-2009 01:06 AM

Quote:

Originally Posted by guessity
Any idea where I went wrong?

man mv and see how its used.

guessity 12-25-2009 01:09 AM

Quote:

Originally Posted by ghostdog74 (Post 3804125)
man mv and see how its used.

I didnt get you.

I am a noob and learning. Have to do it for my project. :((

ghostdog74 12-25-2009 01:23 AM

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.

guessity 12-25-2009 01:25 AM

I figured it out. Sorry for being so dumb. Thx for all your help.


All times are GMT -5. The time now is 01:46 AM.