Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to process some text exported from lecture slides using pdf2text. The bullet points of some slides show up like this:

title for the list
-
-
-
a bullet point text
another bullet point text
yet another bullet point text
- nested bullet point
- another nested bullet point
- yet another nested bullet point
title for the next list

I would to concatenate these into a correct (markdown) list like this:

title for the first list

-   a bullet point text
-   another bullet point text
-   yet another bullet point text
    -   nested bullet point
    -   another nested bullet point
    -   yet another nested bullet point

title for the next list
share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Just did it using bash script

#!/bin/bash
c=0
[[ $# -eq 0 ]] && { echo "Error: Please Specify Input file" >&2; exit 1; }

while read line
do
        if [[ $line = "-" ]]; then
                (( c++ ))
                if [[ $c -eq 1 ]]; then
                    echo ""
                fi
        elif [[ $line != "" ]] && [[ $c -ne 0 ]]; then
                echo "-   ${line}"
                (( c-- ))
                if [[ $c -eq 0 ]]; then
                    echo ""
                fi
        elif [[ $line =~ "- " ]] && [[ $c -ne 0 ]]; then
                echo "    $line"
        else
                echo "$line"
        fi
done < $1

Tested and working with input example.

share|improve this answer
    
Yes, this almost works as I intended, thanks. I made an edit with the changes I needed. –  Bengt Mar 6 '13 at 19:59
add comment

Cred to @Rahul but a modified version:

#!/bin/bash

if [[ -z "$1" || ! -f "$1" ]]; then
    printf "Usage: %s <FILE>\n" "$(basename $0)"
    exit 1
fi

c=0
eoli=0
pad=4

while read line
do
        if [[ "$line" = "-" ]]; then
                 (( c++ ))
        elif (( c > 0 )); then
                echo "- $line"
                ! (( --c )) && eoli=1
        elif ((eoli)) && [[ "$line" =~ ^-\  ]]; then
                printf "%-*s%s\n" $pad "" "$line"
        else
                eoli=0
                echo "$line"
        fi
done < "$1"

Using awk:

#!/usr/bin/awk -f

BEGIN {
    c=0
    eoli=0
    pad=4
};

{
    if (/^-$/) { 
        ++c 
    } else if (c > 0) {
        printf "- %s\n", $0
        eoli = (--c == 0)
    } else if (eoli && /^- /) {
        printf  "%*s%s\n", pad, "", $0
    } else {
        eoli=0
        print $0
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.