Use for loop and cat commands in shell scripts to merge files in sequential _linux shell

Source: Internet
Author: User

Working directory There are Mydoc1.txt,mydoc2.txt...mydoc41.txt, would like to use the SED order of values sorted, and then use cat to merge these files, found not to achieve the desired effect, the merge order is as follows:

Copy Code code as follows:

LS-LF *.txt | Sed-n '/mydoc1/,/mydoc41/p ' | Xargs-i Cat {}> >mynew.txt

found that its command did not achieve the desired effect, and later found that the problem should appear in the SED order, it is still sorted by the number of digits, and there is no intelligent to sort by range, sed values command as follows:
Copy Code code as follows:

LS-LF *.txt | Sed-n '/mydoc1/,/mydoc41/p '

The command displays the results as follows:
Copy Code code as follows:

-rw-r--r--1 root 0 Oct 13:38 mydoc10.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc11.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc12.txt
-rw-r--r--1 root 3 Oct 14:48 mydoc13.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc14.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc15.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc16.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc17.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc18.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc19.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc1.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc20.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc21.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc22.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc23.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc24.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc25.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc26.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc27.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc28.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc29.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc2.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc30.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc31.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc32.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc33.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc34.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc35.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc36.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc37.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc38.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc39.txt
-rw-r--r--1 root 2 Oct 14:48 mydoc3.txt
-rw-r--r--1 root 0 Oct 13:38 mydoc40.txt
-rw-r--r--1 root 6 Oct 14:48 mydoc41.txt

So use the following command to implement the requirements: The command looks like this:
Copy Code code as follows:

For ((i=1;i<=41;i++)) do echo Mydoc$i.txt;done | Xargs-i Cat {} >> Mynew.txt

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.