0

I'm trying to get the Bash script to read a text file and print out all the lines in it. I've successfully accomplished that by doing:

while read line; do
   $echo line
done < main.txt

However, I want it to ignore any lines in between /* and */. If I have a text file that looks like this:

hi
hru
/* 
look at my multiline comment function OoOoOoOh 
*/
good wbu
im fine

It will print out:

hi
hru
good wbu
im fine
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1BL1ZZARD
  • 225
  • 2
  • 14
  • 1
    are the `/*` and `*/` always on lines by themselves, or could they show up in lines intermixed with text you want to keep vs discard, eg, can something like `keep this text /* but ignore this text` and `ignore this text */ but keep this text` occur? please update the question with these additional details; also, what code have you tried to implement your filter requirements? – markp-fuso Mar 17 '22 at 19:54
  • You could use `sed` to preprocess the file first, to strip out the comments. – Tim Roberts Mar 17 '22 at 19:55
  • @markp-fuso They could show the way i did, or intermixed, yes – 1BL1ZZARD Mar 17 '22 at 19:57
  • you should state that in the question as well as provide an example – markp-fuso Mar 17 '22 at 20:07

1 Answers1

0

If you don't need while loop, here is a one-liner:

$ sed -r ':a;$!{N;ba};s|/\*[^*]*\*+([^/*][^*]*\*+)*/\n||' main.txt
hi
hru
good wbu
im fine

If insisting on while:

while read line; do
  echo "$line"
done < <(sed -r ':a;$!{N;ba};s|/\*[^*]*\*+([^/*][^*]*\*+)*/\n||' main.txt)
hi
hru
good wbu
im fine
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jarek
  • 782
  • 5
  • 16
  • Sorry, forgot to add: sed taken from https://stackoverflow.com/questions/13061785/remove-multi-line-comments – Jarek Mar 17 '22 at 20:02
  • doesn't work if `/*` or `*/` show up in the middle of a line with both 'keep' and 'discard' text – markp-fuso Mar 17 '22 at 20:09