1

I need some help with sed. I have tried to look for information to do this but I couldn't find any specific and people hardly provide an explanation of the provided solution so this makes difficult to understand how sed works.

Basically I have files with the following format:

NAME_DIGITS_ddd-11s-21a-ds_DIGITS_DIGITS.xml

I want to copy the first DIGITS (e.g 00004574), which is the string between the first and second underscores.

Any idea of how to do this?

Considering that the first digits could be 0 what can I do to delete them?

I would appreciate any example for these two cases and an explanation would be really welcome too.

I have achieved the opposite: delete the characters between first and second underscores with: sed s/_[^_]*_/_/ but cannot see how to do what I really need.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
Fithos
  • 11
  • 1
  • 1
  • 2
  • 1
    Even after editing it's not entirely clear: do you want to copy all the DIGITS part exactly as written (e.g. 00001234), or do you also want to remove leading zeros (e.g. leaving just 1234)? – Chris Davies Aug 20 '15 at 09:59
  • Not a duplicate - the OP points out that they know how to match the string but do not know how to match the boundary points. – Chris Davies Aug 20 '15 at 13:24
  • Is it a necessary condition that the solution must use sed? – Chris Davies Aug 22 '15 at 17:05

4 Answers4

3
 awk -F "_" '{print $2}'

would do the trick !

Also if you really really want to use sed

sed 's/^[^_]*_\([^_]*\)_.*/\1/g'

Since sed does not have a non greedy match, we have to search for anything which is not _ between first and second underscores !

amisax
  • 3,025
2

You wrote that you have files with the following format: NAME_DIGITS_ddd-11s-21a-ds_DIGITS_DIGITS.xml and you want to copy (extract?) the first DIGITS, between the underscores.

Since your sections are cleanly separated by underscores you can use something like this:

echo NAME_DIGITS_ddd-11s-21a-ds_DIGITS_DIGITS.xml | cut -d_ -f2

The cut command extracts field 2 (-f2) using underscore as the specified delimiter (-d_)

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
0

Other sed

sed '/\n/!{s/_0*/\n/g;D;};P;d'

or

sed 's/_0*/\n/;/^[0-9]/!D;P;d'
Costas
  • 14,916
0

The command you want is:

sed -ne 's/^[^0-9_][^_]*_0*\([0-9]*\)_.*/\1/p'

Delete the 0* if you want the leading zeros to show up.

user3188445
  • 5,257