LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2014, 11:53 AM   #1
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Rep: Reputation: Disabled
concatenate values from two text files


Hi,

I have two text files & i want to concatenate the first line of 1st text file with 1st line of second text file and second line of first text file with second line of second text file.

Can anyone please let me know .. how do i do that.

BR\\Vijay
 
Old 07-15-2014, 12:08 PM   #2
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 11.0
Posts: 131

Rep: Reputation: 113Reputation: 113
Quote:
Originally Posted by VijayB View Post
Hi,

I have two text files & i want to concatenate the first line of 1st text file with 1st line of second text file and second line of first text file with second line of second text file.

Can anyone please let me know .. how do i do that.

BR\\Vijay
there are several options here: http://unix.stackexchange.com/questi...s-of-two-files
 
1 members found this post helpful.
Old 07-16-2014, 01:52 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 622

Rep: Reputation: 136Reputation: 136
Run the Script
Code:
#!/bin/bash
i=1
while :
do
tail -n +$i first.txt | head -n 1 >> output.txt
tail -n +$i second.txt | head -n 1 >> output.txt
var=$(echo `wc -l < first.txt` + `wc -l < second.txt` | bc)
if [ $var -eq `wc -l < output.txt` ]
then
break
fi
i=`expr $i + 1`
done

Last edited by eklavya; 07-16-2014 at 02:49 AM.
 
Old 07-16-2014, 05:34 AM   #4
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 51
I think, we do not need to run large code script. If both files have equal number of lines, it can be done in single line.
Code:
awk 'BEGIN { OFS="\n"} FNR==NR { a[(FNR"")] = $0; next } { print a[(FNR"")], $0 }' first_file.txt second_file.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] concatenate text files... masavini Programming 3 05-16-2012 08:38 AM
[SOLVED] Variables in text file, how to get their values when printing out the text? idaham Linux - General 2 04-14-2010 04:28 AM
how to find some files and concatenate them? yumener Linux - Newbie 7 03-10-2009 04:41 PM
Script to concatenate several files docaia Linux - General 10 02-03-2008 02:59 PM
concatenate binary files???? justin19fl Linux - Newbie 6 05-14-2001 03:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration