Quote:
Originally Posted by tgarvin
Thanks for the quick response.
Your script does a good job of removing the leading and trailing dashes but I still need to keep certain leading and trailing dashes intact. What I really need is a count of the leading dashes and of the trailing dashes. Then I can pull out the smallest leading and trailing count and remove that number of dashes from each line in the file.
T
|
Well, that is what the script does. It counts the leading dashes and if this number is smaller than MIN_LEAD then MIN_LEAD
is set to the new smallest number of leading dashes. The same procedure is done for the trailing dashes.
The sed command then strips those minimal leading and trailing dashes from every line, i.e. at least one line will be stripped
of all its leading dashes and at least one line will be stripped of all its trailing dashes.
If you do not want 'naked' lines, e.g. each line has to have at least a certain amount of leading and trailing dashes
then you can simply take out the sed and instead have it like this:
Code:
#!/bin/bash
MIN_LEAD=-1
MIN_TRAIL=-1
CUR=0
while read line; do
CUR=$(expr "$line" : '-*')
if [[ $MIN_LEAD = -1 ]];then
MIN_LEAD=$CUR
elif (( CUR < MIN_LEAD ));then
MIN_LEAD=$CUR
fi
CUR=$(expr "$line" : '.*\([^-]-*$\)')
if [[ $MIN_TRAIL = -1 ]];then
MIN_TRAIL=$(( ${#CUR} - 1 ))
elif (( ${#CUR} - 1 < MIN_TRAIL ));then
MIN_TRAIL=$(( ${#CUR} - 1 ))
fi
done < file
echo "${MIN_LEAD}"
echo "${MIN_TRAIL}"
exit
This will output only the minimal amount leading/trailing dashes. You can also have it output the leading/trailing
dashes for every line:
Code:
#!/bin/bash
while read line; do
echo -n $(expr "$line" : '-*')
CUR=$(expr "$line" : '.*\([^-]-*$\)')
echo " $(( ${#CUR} - 1 ))"
done < file
First number will be the leading dashes, second one will count the trailing dashes.