我有一个 .ods 文件。我需要在所述文件中搜索关键字,并需要结果反映整行,而不仅仅是包含关键字的单元格。 这是有问题的文件:
RO JOB REPAIR TIME
224576 1 DASH HARNESS 26.3
224577 5 APIM FLASH 1.5
224578 3 SUNROOF 11.9
224579 9 OIL CHANGE 0.3
我在这里找到了一个名为 odfgrep 的脚本,效果很好,但返回的结果只是包含关键字的单元格:
./odfgrep DASH FEB23.ods
DASH HARNESS
我想看到的是整行,最好有标题:
./odfgrep DASH FEB23.ods
RO JOB REPAIR TIME
224576 1 DASH HARNESS 26.3
旁注,我打算将其与 zentity 脚本合并。
我过去遇到过人们不理解我的问题的问题。如果这还不够清楚,请不要关闭,给我机会修复它,并请提供一些关于不清楚的地方的指示。谢谢。
#! /bin/bash
### grep Libreoffice files (temporarily converted to text)
### adapted for LibreOffice from https://www.linux-magazine.com/Issues/2018/213/Tutorials-odfgrep/(offset)/3
### note: like original odfgrep, can only be used in a directory in which one can create (and delete) files (and a subdir)
OPTIONS=$@
ODFOPTIONS=`echo $@ | sed -e 's/\.\(odt\|odp\|ods\)\b/\.\1\.odfgrep\.txt/g'`
### explicit initialization
FILES2REMOVE=""
### working subdir
WORKDIR=.odfgrep
### don't mess with it if pre-existing, let the user clean it up
if [ -d "$WORKDIR" ]
then
echo $WORKDIR already exists
exit 1
fi
### try creating it
if ! mkdir $WORKDIR
then
echo $WORKDIR cannot be created
exit 1
fi
for FF in $OPTIONS
do
if [ -f "$FF" ]
then
case "$FF" in
*.odt|*odp*|*ods)
### odt2txt not needed any more, but extension cannot be specified, let's put the result in our subdir
libreoffice --convert-to txt $FF --outdir $WORKDIR
FFBASE=`echo $FF | sed -e 's/\.od[pst]//'`
### then move it to odfgrep expected filename with special extension
mv $WORKDIR/$FFBASE.txt $FF.odfgrep.txt
FILES2REMOVE="$FILES2REMOVE $FF.odfgrep.txt"
;;
*) # non-ODF file found, nothing to do
;;
esac
fi
done
### redirection of error output for case of non-existing file
grep $ODFOPTIONS 2>&1 | sed -e 's/\.odfgrep\.txt//'
### extraneous brackets and slashes removed
if [ -n "$FILES2REMOVE" ]
then
rm $FILES2REMOVE
fi
### finally, remove the empty working subdir
rmdir $WORKDIR
exit