Using SED to Remove Anything but a Pattern

Viewed 91

I have a bunch of . pdf file names. For example:

901201_HKW_RNT_HW21_136_137_DE_442_Freigabe_DE_CLX.pdf

and i am trying to remove everything but this pattern XXX_XXX where X is always a digit. The result should be:

136_137

So far i did the opposite .. manage to match the pattern by using :

set NoSpacesString to do shell script "echo " & quoted form of insideName & " |  sed 's/([0-9][0-9][0-9]_[0-9][0-9][0-9])//'"

My goal is to set NoSpaceString to 136_137

Little bit of help please. Thank you !

P.S. The rest of the code is in AppleScript if this matters

2 Answers

Fixing sed command...

You can use

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

See the online demo

Details

  • -n - suppresses the default line output
  • s/.*\([0-9]\{3\}_[0-9]\{3\}\).*/\1/ - finds the .*\([0-9]\{3\}_[0-9]\{3\}\).* pattern that matches
    • .* - any zero or more chars
    • \([0-9]\{3\}_[0-9]\{3\}\) - Group 1 (the \1 in the RHS refers to this group value): three digits, _, three digits
    • .* - any zero or more chars
  • p - prints the result of the substitution only.

The regex above is a POSIX BRE compliant pattern. The same can be written in POSIX ERE:

sed -En 's/.*([0-9]{3}_[0-9]{3}).*/\1/p'

Final AppleScript code

set noSpacesString to do shell script "sed -En 's/.*([0-9]{3}_[0-9]{3}).*/\\1/p' <<<" & insideName's quoted form

This might work for you (GNU sed):

sed -E '/\n/{P;D};s/[0-9]{3}_[0-9]{3}/\n&\n/;D' file

This solution will print all occurrences of the pattern on a separate line.

The initial command is dependant on what follows.

The second command replaces the desired pattern prepending and appending newlines either side.

The D command removes up to the first newline, but as the pattern space is not empty, restarts the sed cycle (without append the next line).

Now the initial command comes into play. The front of the line is printed and then deleted along with its appended newline.

Again, the sed cycle is restarted as if the line had never been presented but minus any characters up to and including the first desired pattern.

This flip-flop pattern of control is repeated until nothing is left and then repeated on subsequent lines until the end of the file.

Here is a copy of the debug log for a suitable one line input containing two representations of the desired pattern:

SED PROGRAM:
  /\n/ {
    P
    D
  }
  s/[0-9]{3}_[0-9]{3}/
&
/
    D
INPUT:   'file' line 1
PATTERN: aaa123_456bbb123_456ccc
COMMAND: /\n/ {
COMMAND: }
COMMAND: s/[0-9]{3}_[0-9]{3}/
&
/
MATCHED REGEX REGISTERS
  regex[0] = 3-10 '123_456'
PATTERN: aaa\n123_456\nbbb123_456ccc
MATCHED REGEX REGISTERS
  regex[0] = 0-3 'aaa'
PATTERN: \n123_456\nbbb123_456ccc
COMMAND: D
PATTERN: 123_456\nbbb123_456ccc
COMMAND: /\n/ {
COMMAND:   P
123_456
COMMAND:   D
PATTERN: bbb123_456ccc
COMMAND:   /\n/ {
COMMAND:   }
COMMAND:   s/[0-9]{3}_[0-9]{3}/
&
/
MATCHED REGEX REGISTERS
  regex[0] = 3-10 '123_456'
PATTERN: bbb\n123_456\nccc
MATCHED REGEX REGISTERS
  regex[0] = 0-3 'bbb'
PATTERN: \n123_456\nccc
COMMAND:   D
PATTERN: 123_456\nccc
COMMAND:   /\n/ {
COMMAND:     P
123_456
COMMAND:     D
PATTERN: ccc
COMMAND:     /\n/ {
COMMAND:     }
COMMAND:     s/[0-9]{3}_[0-9]{3}/
&
/
PATTERN: ccc
MATCHED REGEX REGISTERS
  regex[0] = 0-3 'ccc'
PATTERN: 
COMMAND:     D
Related