Re: insert new line in files
I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); write a script: #!/usr/local/bin/bash (a=0 while [ $a -lt 36 ];do read line echo $line a=$[a+1] done echo include('/usr/home/www/imp-sites/default_inventory.php'); while read line;do echo $line done) $1 /tmp/$$ mv -f /tmp/$$ $1 run it over each file ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
On Friday 06 February 2009, Adam Vande More wrote: Progress is being made as it works in the test now with the \\ however I'm running into more things I don't understand in regards to what I need to escape in my input string. Whether to use \ or \\ will depend on your shell. You can avoid dependence on the shell by using a sed script, e.g. curlew:/tmp% cat test.txt 1 2 3 4 5 6 curlew:/tmp% cat test.sed #! /usr/bin/sed -f 5i\ test curlew:/tmp% ./test.sed test.txt 1 2 3 4 test 5 6 -- Mike Clarke ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Wojciech Puchar wrote: I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); write a script: #!/usr/local/bin/bash (a=0 while [ $a -lt 36 ];do read line echo $line a=$[a+1] done echo include('/usr/home/www/imp-sites/default_inventory.php'); while read line;do echo $line done) $1 /tmp/$$ mv -f /tmp/$$ $1 run it over each file I would avoid this method because it is extremely inefficient, especially with large files. Shell is very slow for loops like that because it is purely interpretive. If you wrote the same thing in Perl it would run way faster just because it does a compile on load. (Besides, you still have to show the 'find' invocation to run it recursively, as requested.) Steve's sed solution is probably not bad as well. -Will ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
insert new line in files
I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); to be put into file X at line 37 where file X appears in ./subdir1, .subdir2 etc. There are many subdirs or I'd just do it by hand. I've done stuff like this before with the rpl script and it works well as long as there aren't any special characters in the strings. So I assumed I finally hit the point where I'm forced to learn something like sed or awk and tried some examples with sed but I can't figure out what I'm doing wrong. I get results like this: sed '5i\test' test.txt sed: 1: 5i\test: extra characters after \ at the end of i command Is sed the right tool for this? If so, any good primers for BSD sed? ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
In the last episode (Feb 06), Adam Vande More said: I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); to be put into file X at line 37 where file X appears in ./subdir1, .subdir2 etc. There are many subdirs or I'd just do it by hand. I've done stuff like this before with the rpl script and it works well as long as there aren't any special characters in the strings. So I assumed I finally hit the point where I'm forced to learn something like sed or awk and tried some examples with sed but I can't figure out what I'm doing wrong. I get results like this: sed '5i\test' test.txt sed: 1: 5i\test: extra characters after \ at the end of i command You want: sed -e '5i\ test' test.txt i.e. a linebreak after the backslash. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Dan Nelson wrote: In the last episode (Feb 06), Adam Vande More said: I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); to be put into file X at line 37 where file X appears in ./subdir1, .subdir2 etc. There are many subdirs or I'd just do it by hand. I've done stuff like this before with the rpl script and it works well as long as there aren't any special characters in the strings. So I assumed I finally hit the point where I'm forced to learn something like sed or awk and tried some examples with sed but I can't figure out what I'm doing wrong. I get results like this: sed '5i\test' test.txt sed: 1: 5i\test: extra characters after \ at the end of i command You want: sed -e '5i\ test' test.txt i.e. a linebreak after the backslash. I had actually tried that too: sed -e '5i\ ? test' text.txt sed: 1: 5i test : command i expects \ followed by text ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Adam Vande More wrote: Dan Nelson wrote: You want: sed -e '5i\ test' test.txt i.e. a linebreak after the backslash. I had actually tried that too: sed -e '5i\ ? test' text.txt sed: 1: 5i test : command i expects \ followed by text Try: # sed -e 5i\\ ? test text.txt Note the double-quotes and two \\. I just ran into this today ;) Steve ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
In the last episode (Feb 06), Adam Vande More said: Dan Nelson wrote: In the last episode (Feb 06), Adam Vande More said: I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); to be put into file X at line 37 where file X appears in ./subdir1, .subdir2 etc. There are many subdirs or I'd just do it by hand. I've done stuff like this before with the rpl script and it works well as long as there aren't any special characters in the strings. So I assumed I finally hit the point where I'm forced to learn something like sed or awk and tried some examples with sed but I can't figure out what I'm doing wrong. I get results like this: sed '5i\test' test.txt sed: 1: 5i\test: extra characters after \ at the end of i command You want: sed -e '5i\ test' test.txt i.e. a linebreak after the backslash. I had actually tried that too: sed -e '5i\ ? test' text.txt sed: 1: 5i test : command i expects \ followed by text I don't see a backslash in the error message, which means something ate it. Are you running this command from something other than the commandline or a plain sh script? If you're calling this from another scripting language (via system() or popen() or something similar), you probably have to double the backslash so whatever's parsing it out passes one through to sed. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Dan Nelson wrote: I had actually tried that too: sed -e '5i\ ? test' text.txt sed: 1: 5i test : command i expects \ followed by text I don't see a backslash in the error message, which means something ate it. Are you running this command from something other than the commandline or a plain sh script? If you're calling this from another scripting language (via system() or popen() or something similar), you probably have to double the backslash so whatever's parsing it out passes one through to sed. This is being executed from stock tcsh Progress is being made as it works in the test now with the \\ however I'm running into more things I don't understand in regards to what I need to escape in my input string. sed -e '5i\\ include(\'/usr/home/www/imp-sites/default_inventory.php\');' test.txt Unmatched '. I also tried escaping ( ) . / to no avail. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Adam Vande More wrote: Dan Nelson wrote: I had actually tried that too: sed -e '5i\ ? test' text.txt sed: 1: 5i test : command i expects \ followed by text I don't see a backslash in the error message, which means something ate it. Are you running this command from something other than the commandline or a plain sh script? If you're calling this from another scripting language (via system() or popen() or something similar), you probably have to double the backslash so whatever's parsing it out passes one through to sed. This is being executed from stock tcsh Progress is being made as it works in the test now with the \\ however I'm running into more things I don't understand in regards to what I need to escape in my input string. sed -e '5i\\ include(\'/usr/home/www/imp-sites/default_inventory.php\');' test.txt Unmatched '. I also tried escaping ( ) . / to no avail. nevermind I see I have to \\ that as well. Okay now I'm going to try to find a way to do this with find and xargs ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Adam Vande More wrote: Dan Nelson wrote: I had actually tried that too: sed -e '5i\ ? test' text.txt sed: 1: 5i test : command i expects \ followed by text I don't see a backslash in the error message, which means something ate it. Are you running this command from something other than the commandline or a plain sh script? If you're calling this from another scripting language (via system() or popen() or something similar), you probably have to double the backslash so whatever's parsing it out passes one through to sed. This is being executed from stock tcsh Progress is being made as it works in the test now with the \\ however I'm running into more things I don't understand in regards to what I need to escape in my input string. sed -e '5i\\ include(\'/usr/home/www/imp-sites/default_inventory.php\');' test.txt Unmatched '. I also tried escaping ( ) . / to no avail. I don't know for sure under tcsh, but did you try double quotes as I suggested? Using them may prevent the normally special characters from being interpolated. If it doesn't work, then hopefully escaping them will. Steve ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Adam Vande More wrote: I also tried escaping ( ) . / to no avail. nevermind I see I have to \\ that as well. Okay now I'm going to try to find a way to do this with find and xargs IMHO, this has become a job for Perl :) Steve ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Just as a possible starting point... On Fri, 06 Feb 2009 22:50:38 +, Adam Vande More amvandem...@gmail.com wrote: I want to insert a new line of text at a certain position in certain files recursively under a directory. More specifically, I want text like this: include('/usr/home/www/imp-sites/default_inventory.php'); to be put into file X at line 37 where file X appears in ./subdir1, .subdir2 etc. There are many subdirs or I'd just do it by hand. [...] Is sed the right tool for this? If so, any good primers for BSD sed? I'd suggest awk. If you have already a mechanism to handle each of the files that need alteration, an awk command could be this: awk '{ print $0; if(NR == 37) printf(include('/usr/home/www/imp-sites/default_inventory.php');\n); }' file It may be neccessary to have a look at the multiple ' (awk skript enclosure, include() parameter enclusure). -- Polytropon From Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: insert new line in files
Steve Bertrand wrote: Adam Vande More wrote: I also tried escaping ( ) . / to no avail. nevermind I see I have to \\ that as well. Okay now I'm going to try to find a way to do this with find and xargs IMHO, this has become a job for Perl :) Steve Thanks for help everyone. My final command was this: skynet1# find . -name 'filename.php' | xargs sed -i.old -e '37a\\ include(/usr/home/www/imp-sites/default_inventory.php);' I used double quotes because I couldn't finger out how to the single quote. Good enough for me. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org