LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-05-2009, 12:36 PM   #1
mierdatuti
Member
 
Registered: Aug 2008
Posts: 64

Rep: Reputation: 15
[solved] join 3 files


Hi,

I have 2 files, for example:

fileA

hello I'm
My name is
.....


file B

David and you?
Pepe, what it's
.......

And I would like to have:

file C

1#hello I'm#David and you?
2#My name is#Pepe, what it's
...............


Could you say me how do it in bash!?

Many thanks,!

Last edited by mierdatuti; 01-07-2009 at 02:20 PM.
 
Old 01-05-2009, 12:47 PM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
http://tldp.org/LDP/Bash-Beginners-Guide/html/
 
Old 01-05-2009, 12:49 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Have you looked at the "join" command?

You may also need SED to insert the "#" characters.
 
Old 01-06-2009, 09:53 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

One could probably do it all in bash, but I suggest paste as the main command, as in:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate paste, awk.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) paste awk
set -o nounset
echo

FILE1=data1
FILE2=data2

echo " Data file $FILE1:"
cat $FILE1

echo
echo " Data file $FILE2:"
cat $FILE2

echo
echo " Results:"
paste -d'#' $FILE1 $FILE2 |
awk '{ print NR "#" $0 }'

exit 0
Producing:
Code:
$ ./s1

(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
paste (coreutils) 5.2.1
GNU Awk 3.1.4

 Data file data1:
hello I'm
My name is

 Data file data2:
David and you?
Pepe, what it's

 Results:
1#hello I'm#David and you?
2#My name is#Pepe, what it's
 
Old 01-06-2009, 10:19 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There are an almost unlimited number of ways to do this. Since you asked "in bash", here's a pure bash solution which doesn't rely on external programs at all. For almost all practical purposes this is not going to be the best method, but it's instructive about how the shell handles files.

Code:
#!/bin/bash

end=0
while [ $end -eq 0 ]; do
    Aend=0
    Bend=0
    read -u3 fileAline || Aend=1
    read -u4 fileBline || Bend=1
    if [ $Aend -eq 1 ] && [ $Bend -eq 1 ]; then
        end=1
    else
        echo "#$fileAline#$fileBline#"
    fi
done 3< fileA 4< fileB > fileC
Of course there are a great many variations of this. The version above is what spung to mind.

Specific things to note:
  1. You can re-direct multiple files into a while loop using the "n<" syntax, where n is an integer file descriptor. 0, 1 and 2 are already used by the shell for stdin, stdout and stderr, so start your custom file descriptors at 3.
  2. You can read some a numbered file descriptor using the -u option to the read command.
  3. By putting || between two commands, the second one will only be executed if the return value of the first command is non-0. In the case of the read statements, read returns non-0 if the specified file descriptor is at the end of the file. This is documented in the bash manual page.
 
Old 01-06-2009, 11:13 AM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
OP should at least thank the people here for doing his homework for him.
 
Old 01-06-2009, 11:28 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by jiml8 View Post
OP should at least thank the people here for doing his homework for him.
Posting history does not match the typical MO for a "homework troll".

Yes, he/she should thank us---and has in the past.
 
Old 01-07-2009, 02:19 PM   #8
mierdatuti
Member
 
Registered: Aug 2008
Posts: 64

Original Poster
Rep: Reputation: 15
Many thanks to all.
 
  


Reply



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
Join ogg files Caysho Linux - General 2 08-18-2010 01:10 AM
Join 2 .flv files? 2eXtreme Linux - Desktop 3 07-09-2010 07:15 PM
Join two mpeg2/4 ts files gjagadish Linux - Software 1 09-08-2006 02:41 AM
join realvideo files? wijnands Linux - Newbie 1 06-28-2004 05:20 PM
join multiple files onewhoknows Linux - Software 10 06-01-2004 06:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 PM.

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
Open Source Consulting | Domain Registration