LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to concatanate two files (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-concatanate-two-files-4175479797/)

vj776 10-06-2013 10:46 AM

How to concatanate two files
 
Hi i have just started learning Linux.
Please help me by answering below question

file1

ABCDEF
GHIJKLM
NOPQRST
UVWXYZ

file2
12345
6789

i want to have file3 as

ABCDEF
GHIJKLM
12345
6789
NOPQRST
UVWXYZ
Regards
Vijay

colucix 10-06-2013 11:03 AM

Do you want to insert the content of file 2 after the second line of file 1? Here are three solutions using sed, awk and pure bash respectively:
Code:

sed '2rfile2' file1
Code:

awk '!(NR % 3){while ( getline line < "file2" ) print line}1' file1
Code:

while read line
do
  echo "$line"
  if [[ $((++c)) -eq 2 ]]
  then
    cat file2
  fi
done < file1

Hope this helps.


All times are GMT -5. The time now is 02:17 PM.