How to insert lines from a file to another file after every n lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert lines from a file to another file after every n lines?
# 1  
Old 07-02-2018
How to insert lines from a file to another file after every n lines?

I have two files file1 and file2

cat file1
Code:
1
2
3
4
6
7
8
9 
10

cat file 2
Code:
a 
b 
c


desired output
Code:
a
1
2
3
b
4
5
6
c
8
9
10


any ideas, i tried with awk and sed commands but it didn't worked.

Last edited by vgersh99; 07-02-2018 at 06:36 PM.. Reason: code tags, please!
# 2  
Old 07-02-2018
Please explain what you are trying to do in more detail. And, show us what you have tried with awk. (I don't know how you would do this in sed since sed doesn't have any commands to perform arithmetic operations.

Please also tell us what operating system and shell you're using.
# 3  
Old 07-03-2018
You could use paste like this:

Code:
paste -d\\n file2 - - - < file1

# 4  
Old 07-03-2018
In the output, where does the 5 come from? Where does the 7 go? Please give a clear specification of the logics to be applied.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Possible to insert a few lines of code into a file at a specific point?

Hi Folks - How would I go about inserting a chunk of lines (3) into a specific portion of a file? The background is I have a script (non shell) that it executed daily, however on Sundays, I uncomment a section of code so that piece can be run as well. So I was hoping to write a piece of... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

2. UNIX for Dummies Questions & Answers

sed command to Insert a line before the last four lines of the file

By using sed command, How to insert a new line before the last four lines of the file. Old Line Old Line NEW LINE! Old Line Old Line Old Line Old Line (8 Replies)
Discussion started by: wridler
8 Replies

3. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

4. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

5. Shell Programming and Scripting

split row into lines and insert file name

I have a directory with several hundred files. The file format is a space delimited row with an unknown number of columns: A B C D E F G ... I need to turn this format File1 A File1 B File2 A File3 A File3 B File3 C ... I can use grep to display the filename next to each row of... (2 Replies)
Discussion started by: newreverie
2 Replies

6. Shell Programming and Scripting

To Read a File and Insert a part of the lines into the database

Hi Guys I need to have a shell script which reads a log file and insert a part of each line into the database. Some sample lines in the file are as below. 20091112091359 MED_RQACK : user_data=60173054304,100232120,20091112091359,;ask_status=0;ask_reason=OK;msg_id=20091112091319... (5 Replies)
Discussion started by: Somanadh
5 Replies

7. Shell Programming and Scripting

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies

8. Shell Programming and Scripting

insert multiple lines into a file

Hi all, I've got some problems with editing a big configuration file .. its about 2k lines long.. anyway what I need is to place certain text to certain line number.. lets say I need to place "Something" on line 980 .. "something" else on line number 1500 and so on without tempering the rest of... (12 Replies)
Discussion started by: c0mrade
12 Replies

9. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

10. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies
Login or Register to Ask a Question