Copy some line and paste it after some line in same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy some line and paste it after some line in same file
# 1  
Old 08-30-2012
Copy some line and paste it after some line in same file

Hi,

I want to know how I can copy line 6th to 10th and paste it after 17th line in same file.

Thanks,
Biplab
# 2  
Old 08-30-2012
Does copy mean to overwrite line 10 with line 6 or insert it there so that line 10 becomes line 11? For line 17, what should be appended there, line 6 or 10?
# 3  
Old 08-30-2012
I want to say that...
if
Code:
1 a
2 b
3 c
4 d
5 e
6 f
7 g

now if i copy from 2nd line to 4th line and paste it before 6th line.. o/p will be
Code:
1 a
2 b
3 c
4 d
5 e
2 b
3 c
4 d
6 f
7 g

I want to know the command to do that..

Moderator's Comments:
Mod Comment edit by bakunin: Please view this code tag video for how to use code tags when posting code and data.
# 4  
Old 08-30-2012
Homework ?
# 5  
Old 08-30-2012
Asked in a Interview of top MNC company...Smilie
# 6  
Old 08-30-2012
If using sed is an option, try:
Code:
sed '2,4H;6{;x;s/^\n//;p;x;}' file

# 7  
Old 08-30-2012
Is it batch or interactive (sed/awk stuff agains pure vi...) ?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string in a file and paste next line in a specific way

Hello, I know there are many questions and replies regarding grep command. What I would like to do is a bit different. File A: hello world welcome to my page this is my test site how are you I am fine, thank you where have you been I was in hospital really hope you are fine now Thanks,... (10 Replies)
Discussion started by: baris35
10 Replies

2. Shell Programming and Scripting

Copy Column Value Of Next Line Into Current Line

Hello All, I am looking for help to achieve the following: Here is the data set 1757890237|42|55570025|1468796400|0 1757890237|32|55570025|1471474800|0 1757890237|54|55570025|1474153200|1476745200 1757890237|34|55570026|1468796400|0 1757890237|44|55570026|1471474800|0... (7 Replies)
Discussion started by: angshuman
7 Replies

3. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

4. Shell Programming and Scripting

Vi Editor - How to paste the line concatenated with current line

I generally use yy to copy a line and then p to paste the line at end of current line. But is there a way to paste the copied line in concatenation with the current line with out going to next line. (3 Replies)
Discussion started by: paragkalra
3 Replies

5. Shell Programming and Scripting

Read and copy xml line by line and preserve tab?

I'm trying to read an xml file and copy it line by line to another file and want to preserve the tabs. What i'm trying to do is if I get to a certain line in the xml, I'm going to check to see if the next line is specifically what I want. If it's not, then I want to insert a single line of text... (4 Replies)
Discussion started by: DeuceLee
4 Replies

6. UNIX for Dummies Questions & Answers

copy a line from one file to another

I like to know what unix command i can use to append the second line of file #1 to the end of file #2 so that it performs the following. Thanks in advance. The purpose of this is to keep track of another log file (from crontab) that i have which the log content is flushed every 5 minutes so that... (5 Replies)
Discussion started by: shingpui
5 Replies

7. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies

8. Shell Programming and Scripting

How to copy and paste line in shell script

Hi I want to grep for a line and copy and paste that line. for Example ---- file abc.txt ---- host=atlx1 sid=atld1 mail=abc@abc.com host=atlx2 sid=atld2 mail=xyz@abc.com host=atlx3 sid=atld3 mail=def@abc.com host=atlx4 sid=atld4 mail=mno@abc.com --- end of file abc.txt ---- Now I... (16 Replies)
Discussion started by: upsrk
16 Replies

9. Shell Programming and Scripting

copy and paste a specific line

Hi I am having some trouble cut and paste a file based on the content of another file. I have a file called draft. I need to cut and paste its content to another file based on the content of a file called proc.txt The content of proc.txt is like the following:... (7 Replies)
Discussion started by: tiger99
7 Replies
Login or Register to Ask a Question