Text :
this *is the* string.
this word i want.
Output :
this *is the* string
this *is the* word i want
How can I copy "is the" from "this is the string" and paste it between "this word" using either the sed
or awk
command?
It's easier to use awk
for a solution to this kind of problem, and since you tagged the question with awk
in addition to sed
, here's an awk
answer:
awk 'NR==1{data=$2 " " $3}NR==2{$2=data " " $2}{print}'
Missing from your question was guidance how to identify the items to copy, and how you want to identify the position to paste. So, in my answer I took the liberty of assuming you were scanning a first line for its second and third words, with intent to paste them after the first word of the second line.
awk
by replacing NR==1
with just NR%2
(that's a modulus 2 operation) and all even lines by replacing NR==2
with !NR%2
. Also, I edited your question to make it more clear, so please also mark that accepted if it is correct.
Commented
Feb 15, 2018 at 5:47