LinuxQuestions.org
Visit Jeremy's Blog.
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 04-03-2011, 11:31 AM   #1
noony123
Member
 
Registered: Oct 2010
Posts: 167

Rep: Reputation: 0
How to concatenate rows of 2 files !


Hi all.

How can i concatenate rows of 2 files.

File1:
10.1.1.
10.1.2.
10.1.3.

File2:
.1
.2
.3

How want file3 to be like
10.1.1.1
10.1.2.2
10.1.3.3

Any help pls ?
 
Old 04-03-2011, 11:35 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Check the paste command. You have to specify a null delimiter using the -d option, since the default one is TAB. Also you have to remove the extra dot from the end of the first file or from the beginning of the second one. Suggestion using process substitution:
Code:
paste -d '' file_one <(cut -c2 file_two)

Last edited by colucix; 04-03-2011 at 11:36 AM.
 
1 members found this post helpful.
Old 04-04-2011, 12:15 PM   #3
bibiki
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Rep: Reputation: 0
only: 'paste file1 file2' (as long as the files are on your current directory) would do, too.
 
Old 04-04-2011, 12:58 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
@bibiki - and what about the extra period?
 
1 members found this post helpful.
Old 04-04-2011, 01:25 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The paste command uses a tab as default separator. The -d option is mandatory in this case.
Code:
$ paste file1 file2 | od -c
0000000   1   0   .   1   .   1   .  \t   .   1  \n   1   0   .   1   .
0000020   2   .  \t   .   2  \n   1   0   .   1   .   3   .  \t   .   3
0000040  \n
0000041
 
Old 04-04-2011, 01:35 PM   #6
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by grail View Post
@bibiki - and what about the extra period?
The following is one possible solution. It is by far not the most efficient, nor the most graceful possible solution.

Code:
foo$ cat file1
10.1.1.
10.1.2.
10.1.3.
foo$ cat file2
.1
.2
.3
foo$ paste -d '' file1 file2
10.1.1..1
10.1.2..2
10.1.3..3
foo$ paste -d '' file1 file2 | awk 'BEGIN {FS="."; OFS="."}; {print $1, $2, $3, $5}'
10.1.1.1
10.1.2.2
10.1.3.3
foo$
Enlightening comments welcomed
 
Old 04-04-2011, 02:03 PM   #7
Trickie
Member
 
Registered: Sep 2004
Posts: 38

Rep: Reputation: 23
I'm no Awk expert but the following works:-

awk -F "." '{OFS="."} {$4=$3}1' file1 > file3

Richard
 
0 members found this post helpful.
Old 04-04-2011, 04:31 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Telengard View Post
The following is one possible solution. It is by far not the most efficient, nor the most graceful possible solution.

foo$ paste -d '' file1 file2 | awk 'BEGIN {FS="."; OFS="."}; {print $1, $2, $3, $5}'

Enlightening comments welcomed
That's nice. But you might exploit all the potential of awk. For example:
Code:
awk '{getline var < "file2"; print $0 substr(var,2)}' file1
No need for the paste command, here.
 
1 members found this post helpful.
Old 04-04-2011, 09:51 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
@Trickie - you are only referencing one file ... so how is your code going to work?

colucix has the right idea (as usual ). Another alternative:
Code:
awk -F. '{var = $0; getline < "file2"; print var $NF}' file1
 
1 members found this post helpful.
Old 04-05-2011, 12:56 AM   #10
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Thumbs up

Quote:
Originally Posted by colucix View Post
you might exploit all the potential of awk.
My awk-fu is admittedly still quite weak. Must get stronger.


Quote:
awk '{getline var < "file2"; print $0 substr(var,2)}' file1
That's how input redirection works? Sweet!


Another alternative solution, inspired by my efforts to decode colucix's first post in this thread. This one doesn't use process substitution.

Code:
foo$ cut -c2 file2 | paste -d '' file1 -
10.1.1.1
10.1.2.2
10.1.3.3
foo$
Here's one using only Bash internal functions (meaning no programs from outside Bash itself). I couldn't think of any way to do it in a single loop, so I leave that as an exercise for the reader.

Code:
foo$ t="$IFS"; IFS=.; i=0; while read a b c; do l[++i]="$a.$b.$c"; done < file1; IFS="$t"; i=0; while read d; do echo "${l[++i]}$d"; done < file2
10.1.1.1
10.1.2.2
10.1.3.3
foo$
 
Old 04-05-2011, 03:12 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
Quote:
Originally Posted by Telengard
Here's one using only Bash internal functions
Your on
Code:
#!/bin/bash

exec 3<file1 4<file2

while read -ru3 LINE && read -ru4 END
do
    echo $LINE${END#.}
done

exec 3<&- 4<&-
 
1 members found this post helpful.
Old 04-05-2011, 04:41 AM   #12
Trickie
Member
 
Registered: Sep 2004
Posts: 38

Rep: Reputation: 23
@grail - my code does work; check it out. The rationale is that, from the example, it is obvious that "file 2" echoes the final column of "file 1", therefore, it is unnecessary to call "file 2" as the output from "file 2" is already embedded in "file 1".

Richard
 
0 members found this post helpful.
Old 04-05-2011, 08:44 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
@Trickie - ok ... I will concede that with your rationale this specific example works, but based on the fact that the OP says he has 2 files to be joined, it seemed obvious that this was a simple
example to help illustrate the point. Assuming your version, you could simply do:
Code:
awk -F. '$0=$0$3' file1
 
0 members found this post helpful.
Old 04-05-2011, 11:24 AM   #14
Trickie
Member
 
Registered: Sep 2004
Posts: 38

Rep: Reputation: 23
@grail - err, no you can't. There are two errors: it doesn't save into a third file, which "noony123" asked for and secondly, your code produces no "dot" as a field separator between the third and fourth fields, again, as "noony123" requested.

Richard
 
0 members found this post helpful.
Old 04-05-2011, 12:45 PM   #15
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
Quote:
it doesn't save into a third file
This would semantics and I suggested an alternative to your awk not necessarily the issue (which I already have given my solution for)
Quote:
your code produces no "dot" as a field separator between the third and fourth fields
So this means you ran it then? and are not using inferior copies such as mawk or nawk?
Based on the file1 data of:
Code:
10.1.1.
10.1.2.
10.1.3.
And running:
Code:
awk -F. '$0=$0$3' file1
I get:
Code:
10.1.1.1
10.1.2.2
10.1.3.3
As the original file already has a "dot" at the end, it seems to work just fine.
 
0 members found this post helpful.
  


Reply

Tags
awk, bash, paste


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 and labeling files wisnoskij Linux - Newbie 4 05-06-2010 07:58 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 PDF files? mykrob Linux - Software 5 11-07-2006 06:25 AM
concatenate binary files???? justin19fl Linux - Newbie 6 05-14-2001 03:13 PM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration