Help for "sed" command - need your help

Hi Friends
Need you help on this.
i used below line in my script for capturing some of the file names

ls -1 $filepath|sed 's/[^0-9]*\([0-9]\{8\}\).*/\1/g'|uniq|awk -vEX=$filepath '{print "zip -umD "$0".zip *"$0"*" }'| sh -s || true;

sample file name is PP05000120180627.134
and it selects -> 05000120.zip
how do i get selected this part of file name -- > 20180627.zip

Try:

sed -n 's/.*\([0-9]\{8\}\)\..*/\1/p'
1 Like

How about a single awk script?

ls | awk '
match ($0, /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\./)        {TMP[substr ($0, RSTART, RLENGTH-1)]++
                                                                }
END                                                             {for (t in TMP) print "zip -umD " t ".zip *" t "*" 
                                                                }
'
zip -umD 20180627.zip *20180627*

and pipe into sh . The looong regex is due to my mawk doesn't allow for bounds; if yours does, try [0-9]{8} as you did before.

1 Like

How about no script at all, only pure shell :slight_smile: :

ls -1 | while read FILE ; do
     FILE="${FILE#PP??????}"
     FILE="${FILE%.*}.zip"
     do_something "$FILE"
done

I hope this helps.

bakunin

The requestor needs to tell files matching his pattern from others, and remove duplicates.

EDIT: Mayhap like so:

ls *[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].* |
while read FILE
  do    FILE="${FILE%.*}"
        TMP="${FILE#${FILE%????????}}"
        [ ${ARR[$TMP]} ] || echo zip -umD ${TMP}.zip *${TMP}*
        ARR[$TMP]=1
  done
1 Like

Hi friend
Thanks for input
..
now i am using below expression

ls -1 $EX|sed 's/\([0-9]\{8\}\)\..*/\1/g'|uniq|awk -vEX=$EX '{print "zip -umD "$0".zip *"$0EX"*"}'|sh -s || true;

my file list as below
EJ05000120180627.134

but it finds .zip file. how do i remove that

zip warning: EJ05000120180627.zip not found or empty
  zip warning: name not matched:

Note:
ls -1 is unnecessary when feeding a pipe, since the output is not to a terminal . From man ls :

    -1      (The numeric digit ``one''.)  Force output to be one entry per line.  This is the default when output is not to a termi-
             nal.

So in this case ls -1 is the same as ls

1 Like

dear friends
related to this issue. now my fiels are not getting .zip. what could be the issue.

command is

find . -type f -exec zip {} \;