Hi,
I want to know how I can copy line 6th to 10th and paste it after 17th line in same file.
Thanks,
Biplab
Hi,
I want to know how I can copy line 6th to 10th and paste it after 17th line in same file.
Thanks,
Biplab
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?
I want to say that...
if
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
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..
Homework ?
Asked in a Interview of top MNC company...
If using sed
is an option, try:
sed '2,4H;6{;x;s/^\n//;p;x;}' file
Is it batch or interactive (sed/awk stuff agains pure vi...) ?
Could you please explain how this works? I'm curious
You should know the concept of pattern and hold spaces to understand this. You should also know the following sed
sub-commands:
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.
Hello Siniri,
Code is not working properly for me... could you please explain the code??
Thanks,
Biplab
What does that mean? Could you post the output you got by running the command on your sample file?
I've already done that.
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
Hi,
Check this out,
awk 'NR>=6 && NR <=10{if(s){s=s RS $0;}else{s=$0}next;}NR==18{print s;}1' file
Cheers,
Ranga
Hi Ranga,
Just one small addition...
awk 'NR>=6 && NR <=10{if(s){s=s RS $0;print;}else{s=$0}next;}NR==18{print s;}1' file
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:
sed '2,4H
6{
x
s/^\n//
p
x
}' test
Nope mate
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,
awk 'NR>=6 && NR <=10{if(s){s=s RS $0;}else{s=$0}next;}NR==18{print s;}1' file1
Cheers,
Ranga
Got it Ranga...
Thanks a lot:)
My try on this..
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
Much appreciated for your tries.
I think you have to finetune the script
Cheers,
Ranga
Yes, Its working fine now... Thanks
Hi.
A solution using ed:
#!/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:
% ./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