0xMH
0xMH

Reputation: 2132

How to use sed to identify patterns in multiple lines

So i want to convert numbered list from a one specific form to another.

from this form:

1. numberedlist
2. one
3. two

to this form:

# numberedlist
# one
# two

I make another changes on that file using sed. hence I want to use sed to identify the first form whenever it happens and without limits meaning even if the numberedlist contains more than 3 items.

My thoughts about doing that are:

first: invoke a line containing the pattern (which is a number > a dot > a space.... '[[:digit:]]\. ') to the Pattern Space

second: invoke the next line to the pattern Space and check if it contains '[[:digit:]]\. ' or not. and keep doing that until the next line don't conation the Regex.. then replace it with #s. but yes, I don't know if that's even possible.

This is what I'm using now:

s/[[:digit:]]\. /# /

but yes I know It's not very efficient. I'm still kind of new to the sed world so any help appreciated. Thanks.

Edit: sorry for the confusion guys, my goal is change just the list index to #. any numbers started from 1. to just #. I just want to convert numbered lists into markdown for autonumbering engine.

I can't use something like s/[[:digit:]]\. /# / cause it will change any acurence of the pattern even if It isn't a list.

Upvotes: 2

Views: 134

Answers (2)

dawg
dawg

Reputation: 103744

Just use awk:

$ echo "1. numberedlist
2. one
3. two
55. fifty-five" | awk '{$1="#"} 1'
# numberedlist
# one
# two
# fifty-five

If you want to detect only lines that start with digits:

$ echo "1. numberedlist
2. one
3. two
look ma no digit line
55. fifty-five" | awk '/^[[:digit:]]+\. /{$1="#"} 1'
# numberedlist
# one
# two
look ma no digit line
# fifty-five

But, if you want sed:

$ echo "1. numberedlist
2. one
3. two
look ma no digit line
55. fifty-five" | sed 's/^[[:digit:]][[:digit:]]*\. /# /'
# numberedlist
# one
# two
look ma no digit line
# fifty-five

Upvotes: 1

markp-fuso
markp-fuso

Reputation: 34034

One possible sed solution:

sed -r 's/^[[:digit:]]+\. /# /g' <inputfile>
  • -r : treat search pattern as an extended regex
  • /^[[:digit]]+\. /# /g : look for lines that start with 1 or more digits followed by a period and a space, and if found replace with a # followed by a space
  • leave all other lines as they are (ie, don't make any changes)

For example:

$ cat datfile
1. numberedlist
2. one
3. two
where in the world is waldo
10. pickles
15. jam
# I'm just a comment
sky blue
100. bash
101. ksh
102. csh
72.don't touch this
# rubber ducky

And a test run of our sed script:

$ sed -r 's/^[[:digit:]]+\. /# /g' datfile
# numberedlist
# one
# two
where in the world is waldo
# pickles
# jam
# I'm just a comment
sky blue
# bash
# ksh
# csh
72.don't touch this
# rubber ducky

Upvotes: 1

Related Questions