1

I want to read the contents of a text file and check for the filenames with extension .txt and find merge those .txt files.Is there a way I could do this using bash? For example, if the text file contains, file1.txt, file2.txt I want to read the strings with .txt extension and find merge those files which is in another location.

I tried the below,

txt_file="/tmp/Muzi/tomerge.txt"
while read -r line;do
    echo $line
done <"$txt_file"

But, this prints out the complete text file and I am completely new using bash.

1
  • It looks like this, ./MyProject/src/file1.java ./MyProject/src/file2.java It is one file per line Commented Feb 5, 2018 at 8:54

1 Answer 1

1

There is a good deal of assuming involved, but... if I understood your question, you would have a tomerge.txt where on some lines a filename would appear, one per line, ending in .txt. If that is the case (and the filenames do not contain spaces) you can:

cat $(grep '[.]txt$' tomerge.txt)

It's not bash only (uses cat), it concatenates files corresponding to all lines ending in .txt we've collected from tomerge.txt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.