Copy line to specific line number in a file

22 views
Skip to first unread message

alig...@gmail.com

unread,
Jan 4, 2021, 10:02:26 AM1/4/21
to Linux Users Group
Hi,
I want to copy a line from a file to a specific line number in a file.

I copy line 3 from w.dat file to test.dat file using 

sed -n 3P w.dat >> test.dat

but it copy line 3 to the end of test.dat file, I want copy line 3 to line number 5 in the test.dat file, how can I do that? 

Robert Citek

unread,
Jan 4, 2021, 10:51:31 AM1/4/21
to linuxusersgroup
Without example data, it's not completely clear what you are trying to do.  But this might be one of many ways to accomplish what you are trying to do:

$ { head -4 test.dat ; sed -n 3p w.dat ; tail -n +5 test.dat ; } | cat -n
     1 b1
     2 b2
     3 b3
     4 b4
     5 a3
     6 b5
     7 b6
     8 b7
     9 b8
    10 b9
    11 b10

Sample data:

$ paste w.dat test.dat  | cat -n
     1 a1 b1
     2 a2 b2
     3 a3 b3
     4 a4 b4
     5 a5 b5
     6 a6 b6
     7 a7 b7
     8 a8 b8
     9 a9 b9
    10 a10 b10

Regards,
- Robert

alig...@gmail.com

unread,
Jan 4, 2021, 12:44:09 PM1/4/21
to Linux Users Group
Hi Robert,

I want to copy line 5  from a file to line 3 in another file

Thanks in advance

Robert Citek

unread,
Jan 4, 2021, 1:03:40 PM1/4/21
to linuxusersgroup
Hmm.  That sounds similar, but not identical to your original post. Can you clarify your question by giving an example, using a small dataset for both input and expected output? - Robert

--
--
You received this message because you are subscribed to the Linux Users Group.
To post a message, send email to linuxus...@googlegroups.com
To unsubscribe, send email to linuxusersgro...@googlegroups.com
For more options, visit our group at http://groups.google.com/group/linuxusersgroup
References can be found at: http://goo.gl/anqri
Please remember to abide by our list rules (http://tinyurl.com/LUG-Rules)
---
You received this message because you are subscribed to the Google Groups "Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to linuxusersgro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/linuxusersgroup/39b6524a-fa47-4a91-8c66-93bd19393e62n%40googlegroups.com.

alig...@gmail.com

unread,
Jan 4, 2021, 1:07:05 PM1/4/21
to Linux Users Group
file 1 contains

da1
da2
da3

file 2 contains 

cx1
cx2
cx3
cx4

I want to copy da2 after cx2, I want output will be

cx1
cx2
da2
cx3
cx4

Robert Citek

unread,
Jan 4, 2021, 1:31:44 PM1/4/21
to linuxusersgroup

Input files:

$ cat file1
da1
da2
da3

$ cat file2
cx1
cx2
cx3
cx4

Pick just the lines you want from each file:

$ head -2 file2
cx1
cx2

$ sed -n 2p file1
da2

$ tail +3 file2
cx3
cx4

Stitch the commands together with braces to achieve your output:

$ {
  head -2 file2
  sed -n 2p file1
  tail +3 file2

}
cx1
cx2
da2
cx3
cx4


Regards,
- Robert

alig...@gmail.com

unread,
Jan 4, 2021, 2:17:56 PM1/4/21
to Linux Users Group
Thanks for your help, but tail +3 appears an error as

$ tail: cannot open '+3' for reading: No such file or directory

Robert Citek

unread,
Jan 4, 2021, 2:32:37 PM1/4/21
to linuxusersgroup
Your version of tail may require a '-n' option, that is:

tail -n +3 file2

Regards,
- Robert


alig...@gmail.com

unread,
Jan 5, 2021, 3:24:03 AM1/5/21
to Linux Users Group
Thank you Robert
Message has been deleted

Robert Citek

unread,
Jan 5, 2021, 11:29:46 AM1/5/21
to linuxusersgroup
Have a look at the syntax for a 'for' loop.

$ help for
for: for NAME [in WORDS ... ;] do COMMANDS; done
    The `for' loop executes a sequence of commands for each member in a
    list of items.  If `in WORDS ...;' is not present, then `in "$@"' is
    assumed.  For each element in WORDS, NAME is set to that element, and
    the COMMANDS are executed.

Regards,
- Robert


On Tue, Jan 5, 2021 at 1:48 AM alig...@gmail.com <alig...@gmail.com> wrote:
Dear Robert,
I want to make iteration with line number as 
 
for j in 1 2 3
sed -n $jp file.dat
done

but the following error appears

$ syntax error near unexpected token `sed'

I tried to put space between $j and p but not works.

how I can do it?
Reply all
Reply to author
Forward
0 new messages