wanna to add in newlines in begin and end of multiple files
I wish to add in one line before the end of file and add in two lines in the beginning of files. I tried this to add in the line before the end of file. But the output is weird.
#!/bin/bash j=1 for i in *.html; do if [[ $j -lt 13 ]]; then echo \n "</DOC>/" >>cc_${j}.html j=$(($j+1)); done It looks like </DOC>/ would be following the last line.But what I want is make </DOC>/ the last line. Anyone knows the mistakes of my code? Also, Could I add in lines in front of and in the end of files in the same time? Would this cause errors? Thanks |
Use the sed, Luke
Quote:
sed) for text editing, and not bash, since it is more efficient. The syntax may take a bit of head scratching, but is well worth it. Here is an example: Code:
#!/bin/sh bgoodr |
Occurs error when I try out....
Since I need do this for a series of files, I tried
#################################### #!/bin/bash j=1 for i in *.html; do if [[ $j -lt 13 ]]; then cat > T_1_${j}.html <<EOF; line 1 line 2 line 3 line 4 < T_1_${j}.html sed ' 1i\ <DOC>\ <DOCNO>yichu_chen_1_${j}</DOCNO> $i</DOC>\ fi j=$(($j+1)) done ###################################### But errors occurs that syntax error: unexpected end of file and sometimes I also got problems like unexpected EOF while looking for matching `'' can't figure out what kind of mistakes I made..... |
sed
Hi,
I had a similar problem with sed and an XML file with tags like "<text>". Unexpected end of file, I don't know what it was, I only know that I could only use VI, using the same expression as in sed - worked. Must be something with the tags, or the way the file is terminated. Regards, Richard |
Quote:
I believe that you have two syntax errors: The first is that you didn't terminate the here-document expression that starts with: Code:
cat > T_1_${j}.html <<EOF; Code:
some_command <<XYZ Code:
cat >/tmp/somefile1 <<E1 Code:
< T_1_${j}.html sed ' Code:
${j} Code:
'"${j}"' So after my hacking on your script, I ended up with this: Code:
#!/bin/bash bg |
Hi,
I tried to fixed my code based on your suggestion.Your code is more like create new files and write in new lines in front of and in the end of it. However, I need to perform this to some existing files and their names can't be specific right now.(I used specific name is because I want to try out the code.) So, I think I don't need the cat part and just do the sed part. But somehow the change would appear in the terminal windows but would not write into the files.And I still can't figure out how to operate files when I don't know their names so I just tried the belowed code. The T_1_1 to T_1_7 are existing files. #!/bin/bash j=1 for i in *.html; do if [[ $j -lt 7 ]]; then < T_1_${j}.html sed ' 1i\ <DOC>\ <DOCNO>T_1_'"${j}"'</DOCNO> $i</DOC>\ ' fi j=$(($j+1)); done |
Quote:
Code:
#!/bin/bash indent it consistently (yeah, I know that sed sections can't be indented). bg |
does
Code:
echo; echo; cat file.txt; echo |
Yep!
|
All times are GMT -5. The time now is 04:22 PM. |