shell


sed command doesn't work in shell script


I have a shell script that looks like the following:
#!/bin/bash
for file in $1/*.html; do
echo "Working on $file ..."
# delete headers in html files.
sed -n '1,9d' $file
# delete the last 4 lines of the file.
sed -n -e :a -e '1,4!{P;N;D;};N;ba'
# ant runs regex task on all apt files
ant -Dtarget.file=$file
# all .html are changed to .md
mv $file `echo $file | sed 's/\(.*\.\)html/\1md/'` ;
done
but the script hangs on the first sed command and I have to force exit. I'm wondering if there's anything wrong about the way I've set this up?
If I remove the first two sed commands, I can run the other parts of the script, including that final one with a sed command in it.
I think you'll find it's hanging on the second sed, where you don't provide a file name to work on. In other words, it will take its standard input from the terminal.
You won't get output from the first sed since -n prevents the default printing of the pattern space. If you're trying to change the actual file rather than output the changes to standard output, you need the -i inplace editor.
If you can't use the inplace option, you'll have to do something like:
sed -n '1,9d' ${file} >${file}.tmp0
sed -n -e :a -e '1,4!{P;N;D;};N;ba' ${file}.tmp0 >${file}
rm -f ${file}.tmp0
This uses a temporary file for making the changes except for the last change, which overwrites the original file. Keep in mind that this changes the original - if that's not what you want, you can change the second sed to output to ${file}.tmp2 but you'll have to adjust your subsequent commands to take that into account.
If you really want to delete the first nine and last four lines of a file, you can also use something like:
head --lines=-4 oldfile.txt | tail --lines=+10 >newfile.txt
provided you have a [rd]ecent implementation of head/tail. The head prints all but the last four lines and the tail starts printing at line ten.
What command are you running? If nothing matches the file glob, then it will physically insert the unexpanded string there.
For example, try this:
$ for i in foo/*.bar; do echo $i; done
foo/*.bar
So your sed script is bombing out because the file name it is receiving doesn't exist, which means it will act as if no file argument was specified (ie stdin).
Change the first line to:
#!/bin/bash -x
This will show you each command as bash executes it, after all expansions have taken place.
My guess is that $1/*.html isn't matching any files; hence the sed line evaluates to just sed -n '1,9d' and so sed is trying to read from stdin

Related Links

How to use else in Shell Scripting?
How to export files from SVN to remote FTP server
in shell how to combine rows between two strings into one row
How to Build a maven project using script file?
How to execute SWI-prolog command from shell?
Weird C-shell return code for evaluating 1-1
hadoop.sh cannot locate/access TEZ jars: No such file or directory error
Redirecting file descriptors
Extract words from a List in C Shell
What is the function of shell
tmux with C-shell (tcsh) alias are lost
Chaining 4 unix commands together and breaking the command to additional lines
Redirecting stderr
cat: not found when I execute ksh script
My command prompt terminal won't change?
Cygwin can't execute shell script

Categories

HOME
asp.net-web-api
merge
redux-form
mysql-workbench
serialization
r-googlesheets
regular-language
python-multithreading
boto3
siddhi
keytool
firefox-addon
video-editing
jmp
connector
user-experience
undertow
dpdk
joptionpane
epsilon
short-url
direct3d11
game-theory
maze
slidetoggle
gpio
android-sharedpreferences
wcag
exchangewebservices
twiml
hard-drive
openwhisk
navigator
roblox
hivemq
reason
ods
linq-to-objects
magento-1.9.3
preloader
subclipse
install.packages
file-sharing
strftime
magento-2.0.7
spring-integration-sftp
otp
check-mk
sandcastle
materialized-path-pattern
slackware
resourcemanager
dup
embedding
tracker-enabled-dbcontext
gcloud-ruby
ikiwiki
createprocess
yajsw
nullable
halcon
character-replacement
roadkill-wiki
android-launcher
coda
rpart
mod-proxy
uncompress
sha512
botan
bufferedinputstream
rrule
xmltype
grouping-sets
theos
beatsmusic
boost-program-options
mintty
reflector
jjaql
memset
koala
gtm-oauth2
away3d
windows-phone-8-sdk
inappsettingskit
process-explorer
fb.ui
apache-abdera
redis-py
extreme-programming
nokia-s40
role
mysql-error-1205
quotation-marks
cloudfiles
timertask
undefined-index
isapi-extension
sqlobject
product-management

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App