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
# 8  
Old 08-30-2012
Quote:
Originally Posted by elixir_sinari
If using sed is an option, try:
Code:
sed '2,4H;6{;x;s/^\n//;p;x;}' file

Could you please explain how this works? I'm curious
# 9  
Old 08-30-2012
You should know the concept of pattern and hold spaces to understand this. You should also know the following sed sub-commands:
Code:
H --> append contents of pattern space to hold space
x --> exchange/swap contents of pattern space and hold space

So, in brief,
2,4H --> for lines from 2 to 4, append the pattern space to hold space.
6{;x;s/^\n//;p;x;} --> at line 6, swap the pattern and hold spaces (at this juncture, hold space looks like \n2 b\n3 c\n4 d) to hold line 6. Delete the first new-line in this new pattern space so that you do not get a blank line in the output. Then, print this pattern space. Last, swap pattern and hold spaces again so that line 6 is printed.
This User Gave Thanks to elixir_sinari For This Post:
# 10  
Old 08-30-2012
Hello Siniri,

Code is not working properly for me... could you please explain the code??

Thanks,
Biplab
# 11  
Old 08-30-2012
Quote:
Originally Posted by Biplab
Code is not working properly for me...
What does that mean? Could you post the output you got by running the command on your sample file?

Quote:
Originally Posted by Biplab
could you please explain the code??
I've already done that.
# 12  
Old 08-30-2012
Quote:
Originally Posted by elixir_sinari
What does that mean? Could you post the output you got by running the command on your sample file?
Code:
cat test
1 a
2 b
3 c
4 d
5 e
6 f
7 g


Code:
sed '2,4H;6{;x;s/^\n//;p;x;}' test

1 a
2 b
2 b
3 c
3 c
2 b
3 c
4 d
4 d
2 b
3 c
4 d
6 f
2 b
3 c
4 d
7 g


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-30-2012 at 11:08 AM.. Reason: code tags
# 13  
Old 08-30-2012
awk

Hi,

Check this out,

Code:
awk 'NR>=6 && NR <=10{if(s){s=s RS $0;}else{s=$0}next;}NR==18{print s;}1' file

Cheers,
Ranga Smilie
# 14  
Old 08-30-2012
Quote:
Originally Posted by rangarasan
Hi,

Check this out,

Code:
awk 'NR>=6 && NR <=10{if(s){s=s RS $0;}else{s=$0}next;}NR==18{print s;}1' file

Cheers,
Ranga Smilie
Hi Ranga,

Just one small addition...

Code:
awk 'NR>=6 && NR <=10{if(s){s=s RS $0;print;}else{s=$0}next;}NR==18{print s;}1' file

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