I have a program that require all keywords to be in a single paragraph, most of the time, separated by commas
For example:
I have those terms
1-Term
1.1-Term
2-Term
3-Term
4-Term
That i collected and organized into groups and subgroups with Titles and subtitles
Title
-
1-Term
-
1.1-Term
-
2-Term
- Sub-Title
- 3-Term
- 4-Term
- Sub-Title
But then i want to turn them into:
1-Term, 1.1-Term, 2-Term, 3-Term, 4-Term
Removing certain marked words(Titles and sub-Titles), any Empty/Blank space, and Line breaks, while adding the commas between The Terms. I want to keep certain dashes “-”(like in words )
1-Term,1.1 -Term,2-Term,3-Term,4-Term


If you wanted a somewhat cruder approach using basically ubiquitous tools, you could do something like this:
$ grep '^ *-' /tmp/foo.txt | grep -v ': *$' | sed 's/ *- //' | tr '\n' ',' | sed s'/,$/\n/' Harry potter,Perfect Blue,Jurassic world,Jurassic Park,Jedi,Star wars,The clone wars,MCU,Gumball ,Flapjack,Steven Universe,Stars vs. the forces of Evil,Wordgril,FlapjackHere I’m first using
grep '^ *-'to get all lines starting with any amount of whitespace and a leading dash, then piping that togrep -v ': *$'to remove anything with a colon at the end (including those with whitespace after the colon), then usingtr '\n' ','to replace all newlines with commas, and thensed s'/,$/\n/'to replace the trailing comma with a newline again (although sed is finicky across platforms wrt newlines, so you may want to just replace it with an empty string instead).The above is hardly an efficient approach, but it does the job.
If you’re feeling a little old school (and some might say masochistic), you could so a similar crude parser with a perl oneliner. This would be more efficient compute wise, but it’s a bit of an acquired taste readability wise:
$ perl -ne 'chomp; push @a, $1 if /^\s*-\s*(.*[^:\s])\s*$/; END{print join(",", @a), "\n"}' /tmp/foo.txt Harry potter,Perfect Blue,Jurassic world,Jurassic Park,Jedi,Star wars,The clone wars,MCU,Gumball,Flapjack,Steven Universe,Stars vs. the forces of Evil,Wordgril,FlapjackHere
perl -nmakes perl look at each line individually,chompstrips off the trailing newline, we match for/^\s*-\s*(.*[^:\s])\s*$/(a string starting with a dash and ending with something not a colon) and append the content of the matching parenthesis to an implicitly declared array@a. Then we add anEND{}block which will be executed after all lines are parsed, where we print the array joined on,.