user690462
user690462

Reputation: 129

Combine file names and content

I got several files like this:

First file is named XXX

1
2
3

Second file is named YYY

4
5
6

I would like to write content and the file names to a separate file that would look like this:

1 XXX
2 XXX
3 XXX
4 YYY
5 YYY
6 YYY

Can someone suggest a way to do this?

Upvotes: 0

Views: 208

Answers (2)

kurumi
kurumi

Reputation: 25609

awk '{print $0,FILENAME}' file1 file2

Or Ruby(1.9+)

$ ruby -ne 'puts "#{$_.chomp} #{ARGF.filename}"' file1 file2

Upvotes: 2

ennuikiller
ennuikiller

Reputation: 46975

Without further explanation of what you actually need this should work:

for file in $(ls)
do
  echo -n $file >> outfile
  cat $file >> outfile
done

Upvotes: 0

Related Questions