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
# 15  
Old 08-30-2012
Quote:
Originally Posted by Biplab
cat test
1 a
2 b
3 c
4 d
5 e
6 f
7 g


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
Hmm..you seem to have to made some mistake while running the command (may be changed the positions of the semicolons). Try with this and let me know:
Code:
sed '2,4H
6{
x
s/^\n//
p
x
}' test

This User Gave Thanks to elixir_sinari For This Post:
# 16  
Old 08-30-2012
awk

Quote:
Originally Posted by pamu
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

Nope mate Smilie

I think, you are trying to print all the original content and also print those content(6-10 lines) after the specified line(17th). If yes,
Then probably you have to remove the next,

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

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 17  
Old 08-30-2012
Got it Ranga...Smilie

Thanks a lotSmilie

My try on this..Smilie

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

# 18  
Old 08-30-2012
Much appreciated for your tries.
I think you have to finetune the script Smilie

Cheers,
Ranga Smilie
# 19  
Old 08-30-2012
Quote:
Originally Posted by elixir_sinari
Hmm..you seem to have to made some mistake while running the command (may be changed the positions of the semicolons). Try with this and let me know:
Code:
sed '2,4H
6{
x
s/^\n//
p
x
}' test

Yes, Its working fine now... Thanks Smilie
# 20  
Old 08-31-2012
Hi.

A solution using ed:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate scripted interactive editor, ed.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ed paste

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE
cp $FILE working

pl " Results, calculated vs desired:"
ed working 2>/dev/null <<'EOF'
2,4y
5x
w
q
EOF
if cmp working expected-output.txt
then
  paste working expected-output.txt
else
  pe " Error in calcualtion."
fi

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
ed GNU Ed 0.7
paste (GNU coreutils) 6.10

-----
 Input data file data1:
1 a
2 b
3 c
4 d
5 e
6 f
7 g

-----
 Results, calculated vs desired:
1 a	1 a
2 b	2 b
3 c	3 c
4 d	4 d
5 e	5 e
2 b	2 b
3 c	3 c
4 d	4 d
6 f	6 f
7 g	7 g

See man ed for details ... cheers, drl
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