Score:-2

Search a pattern word from multiple files and concatenate whole lines in a new file

cn flag

I have several data files. For example

sales.txt (the original file has 13 columns)

Date Unit $$ employee# abc def jkl mno pqr
Dec1 10 500 2 1 2 3 4 5
Dec2 12 450 3 6 7 8 9 10
Dec3 13 550 3 11 12 13 14 15
Dec4 15 600 3 16 17 18 19 20
...
...

supply.txt (original one has 3 columns)

Date Unit cost source1 source2
Dec1 10 300 5 5
Dec2 12 350 5 7
Dec3 13 350 7 6
Dec4 15 400 6 9
...
...

And I have 3 other files which have 2 columns in each. What I am trying to do is match the date in all these files and copy the lines in a single file. The code I am using for this is:

# count the number of rows in the file 
file="sales.txt"
N=$(awk -F, '{rows++} END{print rows}' $file)

for row in $(eval echo {2..$N})
do 
 pattern=$(awk -v r=$row 'FNR==r {print $1}' $file);
 paste <(awk -v line=$row 'FNR==line{print $0}' $file) 
   <(awk -v line=$row 'FNR==line{$1=""; print $0}' supply.txt)
   <(awk -v line=$row 'FNR==line{print $2}' otherfile1.txt)
   <(awk -v line=$row 'FNR==line{print $2}' otherfile2.txt)
   <(awk -v line=$row 'FNR==line{print $2}' otherfile3.txt) >> account.txt
done

It is working in copying and finding the lines but the format in the output file is something like this:

Dec1 10 500 2 1 2 3 4 5 10 300 5 5
1 # comes from otherfile1
2 # comes from otherfile2
3 # comes from otherfile3
Dec2 12 450 3 6 7 8 9 10 12 350 5 7
4
5
6
...
...

I want them to be on the same line. I have tried many techniques found in this forum but none of them has worked for me. Please help

djdomi avatar
za flag
please flag this question for movement to stackoverflow.com, we do not support programming
cn flag
I just realized it... Is there any way to transport to stackoverflow.com without retyping? Thank you for pointing out
djdomi avatar
za flag
flag your own question, for community reasons and scroll a bit down belongs to a other site, shortly said yes its possible
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.