Computing.Net > Forums > Unix > script to combine data from 2 files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script to combine data from 2 files

Reply to Message Icon

Name: badger
Date: April 6, 2008 at 13:58:57 Pacific
OS: Mac OSX
CPU/Ram: N/A
Product: N/A
Comment:

Hi,
I'm trying to combine columns of data from two text files. I'd like to take every line of column 2 from text file A and every line of column 3 from text file B. Then write every combination of lines in the two columns to a new text file. So for instance if there were 4 lines in each column from the old files, then the new file would contain 4x4 = 16 lines, each with 2 columns. Any advice or help on this would be greatly
appreciated.

b



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: April 6, 2008 at 18:08:53 Pacific
Reply:

show sample input and output


0

Response Number 2
Name: FishMonger
Date: April 6, 2008 at 21:13:07 Pacific
Reply:

Who's going to get the credit for your homework assignment if we do it for you? If you want to get an "A" in the class, then you should ask your instructor to provide you with the answers, that way you know you'll be submitting the answer that he/she wants.


0

Response Number 3
Name: ghostdog
Date: April 6, 2008 at 23:15:06 Pacific
Reply:

wow, fm, you are in a bad mood today :)


0

Response Number 4
Name: badger
Date: April 7, 2008 at 06:11:30 Pacific
Reply:

Here is a sample of what I'd like to do. Files A and B are both space delimited text files.

File A:

1 event1
2 event2
3 event3
4 event4

File B:

Smp 2007 Station1
Smp 2007 Station2
Smp 2007 Station3
Smp 2007 Station4

Output text file:

event1 station1
event2 station1
event3 station1
event4 station1
event1 station2
event2 station2
event3 station2
event4 station2
event1 station3
event2 station3
event3 station3
event4 station3
event1 station4
event2 station4
event3 station4
event4 station4

Thank you for any help. Also Fishmonger this is for my research, not for a class. I hope to learn from you guys.

b


0

Response Number 5
Name: lankrypt0
Date: April 7, 2008 at 06:33:36 Pacific
Reply:

#!/usr/bin/ksh
for var in $(awk '{print $2}' < filea);do
for var2 in $(awk '{print $3}' < fileb);do
print $var $var2
done
done


0

Related Posts

See More



Response Number 6
Name: lankrypt0
Date: April 7, 2008 at 06:35:59 Pacific
Reply:

Forgot the explanation. Basically you are saying for every second word one every line in fila (line2) and every third word on every line in fileb (line3) print the results you find (line4).


0

Response Number 7
Name: badger
Date: April 7, 2008 at 07:07:22 Pacific
Reply:

Thanks that's a huge help.

b


0

Response Number 8
Name: ghostdog
Date: April 7, 2008 at 07:19:56 Pacific
Reply:

well, there's no need to use awk that many times :)


NR==FNR{
a[FNR]=$2
c=FNR
next
}
{
for(i=1;i<=c;i++) {
print a[i],$3
}
}

save the above script as script.awk
on command line:


# awk -f script.awk file1 file2
event1 Station1
event2 Station1
event3 Station1
event4 Station1
event1 Station2
event2 Station2
event3 Station2
event4 Station2
event1 Station3
event2 Station3
event3 Station3
event4 Station3
event1 Station4
event2 Station4
event3 Station4
event4 Station4


0

Response Number 9
Name: lankrypt0
Date: April 7, 2008 at 08:47:22 Pacific
Reply:

ghostdog: i really need to learn awk better =)


0

Response Number 10
Name: r41n30w
Date: August 12, 2008 at 21:02:39 Pacific
Reply:

Hi,

I also need to combine data 2 txt file become 1 txt file..but not using command prompt copy.
example :
text file 1
row 1 : a,b,c,,e,f,,h
row 2 : 1,2,3,,5,6,,8

text file 2
row 1: ,,,d,,,g,
row 2: ,,,4,,,7,

I need in 1 txt file :
row 1 : a,b,c,d,e,f,g,h
row 2 : 1,2,3,4,5,6,7,8

anyone can help me ?
thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: script to combine data from 2 files

Script to delete lines from a file www.computing.net/answers/unix/script-to-delete-lines-from-a-file/7408.html

How to get data from mylog.log www.computing.net/answers/unix/how-to-get-data-from-myloglog/3805.html

Script to Remove lines from a file www.computing.net/answers/unix/script-to-remove-lines-from-a-file/5268.html