1

I have several text files in a single folder, and I would like to merge only certain sequential files into one long one (where one file comes immediately after the next).

For example, I want to merge all files starting with test (so test1.txt, test2.txt, test3.txt, and so on) into one file named file.out, like so:

Stuff from test1
Stuff from test2
...
Stuff from testN

I tried cat * > file.out, but that merges everything, which is not what I want. How can I do this?

Kaz Wolfe
  • 33,802
  • 20
  • 111
  • 168
  • 2
    Possible duplicate of [How can I merge files on a line by line basis?](http://askubuntu.com/questions/616166/how-can-i-merge-files-on-a-line-by-line-basis) – Sergiy Kolodyazhnyy Jan 15 '17 at 06:16
  • See linked duplicate. It's the job for `paste` command – Sergiy Kolodyazhnyy Jan 15 '17 at 06:16
  • If all of your files have a prefix, you can use `cat test* > out.file`. – Kaz Wolfe Jan 15 '17 at 06:17
  • @Serg That question seems to be about merging *lines* inside files, not about merging whole files. – Jacob Vlijm Jan 15 '17 at 08:16
  • @Nikhil Kalghatgi not ver clear though. "like test1.txt test2.txt etc" needs to be very specific to define what files to merge in what order. – Jacob Vlijm Jan 15 '17 at 08:17
  • @JacobVlijm Well, what text files are made out of ? Lines. And all solutions provided on that question are about merging whole files, so I don't see where you got the idea about merging lines alone. Of course, I'll wait for OP to clarify and if it's not what they need , but until then my vote will remain. – Sergiy Kolodyazhnyy Jan 15 '17 at 09:34
  • @Serg merging `abc` + `def` into `adbecf` is quite different from `abcdef`. Exactly like the accepted answer in the linked dup reflects (the first output). This question asks for the second output. – Jacob Vlijm Jan 15 '17 at 09:56
  • 1
    @JacobVlijm well, why don't we wait for OP to clarify. If it's not what they want , i.e. "merge" files, I'll remove my vote. Otherwise, it stays. As it is, the question can be closed for being unclear ,too. – Sergiy Kolodyazhnyy Jan 15 '17 at 09:59
  • @KazWolfe cat test* > out.file worked. this is what i wanted. Thanks a lot – Nikhil Kalghatgi Jan 15 '17 at 15:19

1 Answers1

4
  • ways of using wildcards for cat
cat test[1-2].txt>out.txt  //matches any character in the set
cat test[!34].txt>out.txt  //matches any character not in the set
cat test?.txt>out.txt  //for a single character
cat test*>out.txt    // for any strings 
Akhil Varma
  • 391
  • 1
  • 2
  • 9