Sam
Sam

Reputation: 2331

Pasting many files together in some sort of loop, paste command

I want to do something like

for i in (1..101)
do
     paste file${i}
done

But this would be 100 separate commands instead of pasting 100 files together

So that I don't have to do

paste file1 file2 file3 file4 ....... file101

Thank-you

Upvotes: 1

Views: 1205

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753585

To paste 101 files numbered from 1 to 101, you can use the brace expansion feature of Bash and Korn Shell.

paste file{1..101} > output.file

Beware: the brace expansion notation is brittle — it does not take much to break it — and not very flexible (for example, the range can't be specified by variable).

You could also consider using seq:

paste $(seq -f 'file%.0f' 1 101) > output.file

That works as long as the generated names don't contain spaces or other similar characters.

Upvotes: 0

Arpan
Arpan

Reputation: 353

Instead of running the command in the loop, you could concatenate the strings in the loop and then run the command at the end.

STR=""
for i in (1..101)
do
    STR=$STR"file"$i" "
done
paste $STR        

Upvotes: 3

Related Questions