I am running the script at the bottom as a one-liner but have expanded for ease of reading.
Assume the directory contains the following file: File (11).pdf
.
When the script executes, it simply repeats the file name three times. i.e.:
File (11).pdf
File (11).pdf
File (11).pdf
I am guessing it has something to do with a poorly crafted sed
line but am too much of a novice to find the problem.
If I am to write the following "simplified" script, it executes as expected.
IFS=$'\n'
for i in `ls *.pdf`
do
base=`printf $i | sed -E 's/^(.*)( \([0-9]{1,4}\))(\.pdf)$/\1/g'`
printf $base"\n"
done
RESULT
File
IFS=$'\n'
for i in `ls *.pdf`
do
count=`printf $i | sed -E 's/^(.*)( \([0-9]{1,4}\))(\.pdf)$/\2/g'`
printf $count"\n"
done
RESULT
(11)
Problem Script
But when I make the script slightly more "complex" it produces unexpected results.
IFS=$'\n'
for i in `ls *.pdf`
do
base=`printf $i | sed -E 's/^(.*)( \([0-9]{1,4}\)\|)(\.pdf)$/\1/g'`
count=`printf $i | sed -E 's/^(.*)( \([0-9]{1,4}\)\|)(\.pdf)$/\2/g'`
pp=`qpdf --show-npages $i`
printf $i"\n"
printf $base"\n"
printf $count"\n\n"
done
RESULTS
File (11).pdf
File (11).pdf
File (11).pdf
Where am I going wrong? Thanks!
Actual one-liner:
IFS=$'\n'; for i in `ls *.pdf`; do base=`printf $i | sed -E 's/^(.*)( \([0-9]{1,4}\)\|)(\.pdf)$/\1/g'`; count=`printf $i | sed -E 's/^(.*)( \([0-9]{1,4}\)\|)(\.pdf)$/\2/g'`; pp=`qpdf --show-npages $i`; printf $i"\n"; printf $base"\n"; printf $count"\n\n"; done
for i in *.pdf; …
. (2) Quote right. (3) Do not use data as a format inprintf
; the format gets interpreted. Useprintf '%s\n' "$i"
if you really need to. In Bash it's easier to use a here string.sed
commands in the simplified snippets are different than thesed
commands in the "complex" script (additional\|
).\|
and when adding it in it also fails. Thanks also for the better coding usingprintf '%s\' "$i"
. I have made changes to the solution. For those viewing this in the future, I found no solution to usingsed
. Instead, I found a solution (below) usingperl
.