Consider a tee export of a MySQL query.
SELECT * FROM mytable;
+----------+-------+----------+-----------+-------+------+--------+
| label1 | lbl2 | label3 | label4 | lbl5 | lbl6 | label7 |
+----------+-------+----------+-----------+-------+------+--------+
| ABCDEFGH | YNNYY | 0.001596 | 0.161152 | 2 | 1 | a |
| ABCDEFGH | YNNYY | 0.001404 | 0.162774 | 3 | 1 | a |
*
*
*
| ABCDEFGH | YNNYY | 0.001286 | 10.941642 | 5999 | 1 | a |
| ABCDEFGH | YNNYY | 0.001315 | 10.942950 | 6000 | 1 | a |
+----------+-------+----------+-----------+-------+------+--------+
9995 rows in set (0.04 sec)
I would like to process this mysqlqtee.txt file through sed or perl, filtering the actual data rows only.
I can tell sed or perl to: “Comment out every line starting with the static text of ‘| AB’, please!”
sed -i '.old' 's/\(^\| AB.*\)/#\1/g' mysqlqtee.txt
perl -pi.old -e 's/(^\| AB.*)/#$1/g' mysqlqtee.txt
These get me:
SELECT * FROM mytable ORDER BY timecode;
+----------+-------+----------+-----------+-------+------+--------+
| label1 | lbl2 | label3 | label4 | lbl5 | lbl6 | label7 |
+----------+-------+----------+-----------+-------+------+--------+
#| ABCDEFGH | YNNYY | 0.001596 | 0.161152 | 2 | 1 | a |
#| ABCDEFGH | YNNYY | 0.001404 | 0.162774 | 3 | 1 | a |
*
*
*
#| ABCDEFGH | YNNYY | 0.001286 | 10.941642 | 5999 | 1 | a |
#| ABCDEFGH | YNNYY | 0.001315 | 10.942950 | 6000 | 1 | a |
+----------+-------+----------+-----------+-------+------+--------+
9995 rows in set (0.04 sec)
nicely commenting out all the actual data rows and leave every other line untouched.
What I am so far unable to tell perl or sed is to: “Comment out every line starting with anything OTHER THAN the static text of ‘| AB’, please!”
Which would get me:
#SELECT * FROM mytable ORDER BY timecode;
#+----------+-------+----------+-----------+-------+------+--------+
#| label1 | lbl2 | label3 | label4 | lbl5 | lbl6 | label7 |
#+----------+-------+----------+-----------+-------+------+--------+
| ABCDEFGH | YNNYY | 0.001596 | 0.161152 | 2 | 1 | a |
| ABCDEFGH | YNNYY | 0.001404 | 0.162774 | 3 | 1 | a |
*
*
*
| ABCDEFGH | YNNYY | 0.001286 | 10.941642 | 5999 | 1 | a |
| ABCDEFGH | YNNYY | 0.001315 | 10.942950 | 6000 | 1 | a |
#+----------+-------+----------+-----------+-------+------+--------+
#9995 rows in set (0.04 sec)
I don’t seem to be able to translate the “IT DOESN’T START WITH” part to regexp.
The dual use of ^ meaning NOT and LINE_START at the same time causes trouble.
I can negate the starting letter with s/^[^\|]/, but that also leaves the header row out.
I managed to do this in perl, using an IF statement.
But it still disturbs me way too much that I couldn’t do it with a single s///g.
Can this be done that way?
How do I translate the “IT DOESN’T START WITH” part to regexp? Either sed or perl solution is fine!
You can negate a regex with
sedby putting!after it like so:Output