Arunjith
Arunjith

Reputation: 985

Merging files one to many

file1.txt

10

file2.txt

05--Nov--2010--Friday 23:24:57,06--Nov--2010--Saturday  8:23:34

06--Nov--2010--Saturday  8:23:34,06--Nov--2010--Saturday  9:56:22

I want to create a thread file as below[ Using shell script].

file3.txt

10,05--Nov--2010--Friday 23:24:57,06--Nov--2010--Saturday  8:23:34

10,06--Nov--2010--Saturday  8:23:34,06--Nov--2010--Saturday  9:56:22

Upvotes: 2

Views: 129

Answers (4)

Dennis Williamson
Dennis Williamson

Reputation: 359955

Here's an alternative version of glenn jackman's answer:

awk -v pf=file1.txt 'BEGIN{getline p<pf;OFS=","} {print p,$0}' file2.txt > file3.txt

The main difference is if file1.txt has more than one line, mine will use the first line and his will use the last line.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246764

awk 'NR==FNR {prefix=$0; next} {print prefix "," $0}' file1.txt file2.txt > file3.txt

The awk variable NR is the current line number of all input lines, FNR is the line number of the current file: NR==FNR is only true for lines in the first file awk reads.

Upvotes: 1

ajreal
ajreal

Reputation: 47321

awk '{print '`cat file1.txt`' "," $0}' file2.txt > file3.txt

Or

line=`cat file1.txt`;
awk '{print '`echo $line`' "," $0}' file2.txt > file3.txt

Upvotes: 1

Reese Moore
Reese Moore

Reputation: 11640

#!/bin/bash
rm -f file3.txt
prefix=$(cat file1.txt)
for i in $(cat file2.txt)
do
    echo $prefix,$i >> file3.txt
done

Upvotes: 2

Related Questions