Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a series of folders containing three files that I want to concatenate as part of a script.

The three files are named file.html1 file.html2 file.html3. I want to concatenate them into file.html

I'm using this loop:

find . -name "file.html" -print0 | while IFS= read -r -d '' n; do
  perl -i.bak -0pe  's/<\/h2>\n(.*?\n)*/<\/h2>\n/' "$n"  #extract 1st part of file.html and create .bak
  perl -i -0pe  's/(.*\n)*.*?<\/h2>\n//' "$n.bak"  #extract 2nd part of file.html
  rename 's/\.html/\.html1/' $n
  rename 's/\.bak/3/' $n.bak
  cat $n1 $n2 $n3 >> $n
done

This script splits the original file.html into file.html1 and file.html3. file.html2 was produced earlier in the script.

The problem is with the cat command. How can I concatenate them in this case and then delete the three fragments? The language is not important.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

OK, I see the problem now, you just need to quote either the variable or the string (1,2 etc). Otherwise $n1 is taken to be the variable name:

find . -name "file.html" -print0 | while IFS= read -r -d '' n; do
  perl -i.bak -0pe  's/<\/h2>\n(.*?\n)*/<\/h2>\n/' "$n"  #extract 1st part of file.html and create .bak
  perl -i -0pe  's/(.*\n)*.*?<\/h2>\n//' "$n.bak"  #extract 2nd part of file.html
  rename 's/\.html/\.html1/' $n
  rename 's/\.bak/3/' $n.bak
  cat "$n"1 "$n"2 "$n"3 >> $n
done

You can also use cat $n"1" $n"2" $n"3" >> $n but this will break if the file names contain spaces.

share|improve this answer
1  
@ToDo no worries, just remember to make your questions as explicit as possible, it makes it much easier to answer them in a useful way. –  terdon May 20 '13 at 14:08
    
Alternatively cat "${n}1" … if someone prefers that for aesthetic reasons: The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name (Bash manual) – cc @ToDo –  slhck May 20 '13 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.