user2049184
user2049184

Reputation:

How can I search ods file for keywords and show entire row, not just the cell that contains keyword?

I have an .ods file. I need to search said file for keyword and need the result to reflect the entire row, not just the cell containing the keyword. Here is the file in question:

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

I found a script called odfgrep here that works well, but the results returned are only the cell containing the keyword:

./odfgrep DASH FEB23.ods
DASH HARNESS

What I would like to see, is the entire row, preferably with the header:

./odfgrep DASH FEB23.ods
RO     JOB  REPAIR          TIME
224576   1  DASH HARNESS    26.3

Side note, I intend to incorporate this with a zentity script. I've had issues in the past with people not understanding my question. If this is not clear enough, please do not close, give me a chance to fix it, and please offer some pointers as to where it is unclear. Thanks.

Upvotes: 0

Views: 542

Answers (1)

Thierry E
Thierry E

Reputation: 1

The below adaptation of odfgrep shows the full line, separating cells with comma (in your example you may grep on "\(REPAIR\|DASH\)" then you may pipe to sed or other to have a different separator)

#!/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

Upvotes: 0

Related Questions