0
sed "s/\([0-9]\)-\([0-9]\)/\1\2/g" file

Can someone break it down and explain what it will do when run.

Thanks.

Pullingmyhairout
  • 725
  • 1
  • 5
  • 6
  • 1
    http://rick.measham.id.au/paste/explain.pl – Dror Mar 24 '16 at 01:16
  • 2
    See: [The Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/3776858) – Cyrus Mar 24 '16 at 01:20
  • Thanks, But I've already looked through numerous guides. I just can't get my head around this expression. – Pullingmyhairout Mar 24 '16 at 01:26
  • 1
    [Take a look](http://rick.measham.id.au/paste/explain.pl?regex=%28[0-9]%29-%28[0-9]%29\1\2). – Cyrus Mar 24 '16 at 01:47
  • 1
    That regex is incredibly simple (it only uses two operators: parentheses for grouping, and square brackets for character matching). Can't you say which part of it is confusing you? Are you confused because `sed` requires you to escape the parentheses that are used for grouping, while most other regular expression engines don't? That's because `sed` uses Basic Regular Expressions by default, where only some of the simplest operators can be used without escaping. – Barmar Mar 24 '16 at 02:43
  • 1
    If you ask a better question, we can give a better answer that clears up your confusion. If we don't know what you don't understand about it, it's harder for us to address it. – Barmar Mar 24 '16 at 02:44
  • yeah. What do you mean by escaping the parantheses. I don't understand what it will substitute and whether it will replace the number with another number or not. – Pullingmyhairout Mar 24 '16 at 02:52

1 Answers1

2

Your sed command removes a hyphen(ie -) in between two numbers.

Suppose the contents of the file are

10-2
20-20
9-13

Doing sed "s/\([0-9]\)-\([0-9]\)/\1\2/g" file will give you

102
2020
913

Deciphering the regex :

[] - used for a range
\(..\) - used for grouping so that you can use \1 for first match \2 for second and so on.

sjsam
  • 21,411
  • 5
  • 55
  • 102