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 pipe the output of s3 cp to unzip?
shell scripting - Extract seperated fields - field list from create field list part
grep the log from only START to the end in shell
ksh error “syntax error at line 5: `(' unexpected”
Unset variables and move on to the next line if condition not met
Re-writing a .bat in .sh [duplicate]
Unix Korn shell retrieve the latest file name with space
Docker CMD evaluation with ENTRYPOINT
How do I run the Hetionet v1.0 docker container?
Shell Error while trying to perform a IF statement
Is there a way to convert all the columns (space separated) of a file into rows?
Remove junk characters from a utf-8 file in Unix
How to send a binary (ebcdic format) file with variable blocks (each record has variable length), from a unix system to a unix mainframe through NDM?
Delete first and last character from from each line of a txt file
Shell script for cron to send an email if the command produces any output
Teamcity with Subversion post commit script on windows

Categories

HOME
uml
fabricjs
turing-machines
nvd3.js
android-studio-2.2
alexa-skills-kit
flex
serialization
socket.io
sublimetext
entity-framework-core
carousel
soa
ado.net
tvos
azure-container-service
retrofit2
recordrtc
extjs4.2
jquery-ui-sortable
cmdb
flash-player
selectize.js
searchview
dynamic-sql
wolframalpha
git-extensions
breadth-first-search
kendo-mobile
href
pimcore
php-mysqlidb
data.stackexchange.com
beautifier
fable-f#
sinch
subclass
user-defined-functions
weather
record
reverse
getlasterror
wurfl
flatmap
diacritics
magento-1.9.3
trigonometry
odoo-website
skylink
dcmtk
attributeerror
escpos
placeholder
avcapturedevice
dynamic-linq
lampp
clarifai
robomongo
facebook-pixel
corruption
arq
pdf-conversion
janus
javafx-css
donations
change-tracking
pytables
winusb
qtplugin
hclust
tooleap
avspeechsynthesizer
sklearn-pandas
redis-cluster
pushbots
jaunt-api
jnlua
iokit
roadkill-wiki
flock
poppler
video-player
text-search
typemock
ocra
aspectj-maven-plugin
entity-framework-4.3
xmltype
webmatrix-3
browserify-shim
teaspoon
create.js
angular-carousel
kissfft
reflector
malformedurlexception
memset
kendo-dataviz
cnf
time.h
scmmanager
dolby-audio-api
mantle
asplinkbutton
garbage
cuda-gdb
appjs
jquery-address
expression-blend-4
promotion-code
mbeans
unattended-processing
luajava
commerceserver2007
timertask
load-time
binomial-heap
pagemethods
wordprocessingml
commercial-application

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App