where grep comes from
Saturday, 20 January 2024 09:27 pmTonight I was fiddling around with a long text file. I needed to add an extra empty line after every line that had a vertical bar (|) character in it. At first I was thinking in terms of a macro -- find the next line with the vertical bar, go to the end of the line, insert a newline, and repeat to the end of the file. Since "sed" doesn't really use macros in the normal sense, I thought I'd look at the older, more primitive editor "ed". It can be fed commands that work like macros. I've never really used ed, so began reading a tutorial for it.
About a third of the way through the tutorial it gave an example of how to display all occurrences of lines that matched a regular expression. (If you don't know what a regular expression, or regex, is, it is a specialised, concise way of matching any particular text). The example used "re" to represent the regular expression. The command is:
g/re/p
Lightbulb moment!
For those unused to Linux and other Unix-like systems, there is an often-used tool to search for and list matching text in files. It has the very weird name of "grep". Not so weird now. :)
Oh, by the way, while reading the tutorial I suddenly realised I'd been thinking about the problem the wrong way. It was easily solved using sed:
sed -i '/ | /a \\n' textfile
-i means in-place. It replaces the input file with the output file.
' ' sed commands should be surrounded by single quotes.
/ / enclose the regular expression (in this case space, vertical bar, space).
a is the command to insert a line after the current one.
\ introduces the text to be inserted
\n is the special character for a newline
the textfile is the file operated on (it was actually called "eplist.txt")
One of the wonderful things about sed is that it repeats the command through the file for every occurrence of the search text. It is extremely fast, even on very large files.
About a third of the way through the tutorial it gave an example of how to display all occurrences of lines that matched a regular expression. (If you don't know what a regular expression, or regex, is, it is a specialised, concise way of matching any particular text). The example used "re" to represent the regular expression. The command is:
g/re/p
Lightbulb moment!
For those unused to Linux and other Unix-like systems, there is an often-used tool to search for and list matching text in files. It has the very weird name of "grep". Not so weird now. :)
Oh, by the way, while reading the tutorial I suddenly realised I'd been thinking about the problem the wrong way. It was easily solved using sed:
sed -i '/ | /a \\n' textfile
-i means in-place. It replaces the input file with the output file.
' ' sed commands should be surrounded by single quotes.
/ / enclose the regular expression (in this case space, vertical bar, space).
a is the command to insert a line after the current one.
\ introduces the text to be inserted
\n is the special character for a newline
the textfile is the file operated on (it was actually called "eplist.txt")
One of the wonderful things about sed is that it repeats the command through the file for every occurrence of the search text. It is extremely fast, even on very large files.
no subject
Date: 2024-01-20 11:39 am (UTC)It was easily solved
Yes...
sed -i '/ | /a \\n' textfile
Not easy.
I'm not great at command line, at all. I can *kinda* understand the syntax, but mostly not.
Sed is a totally new command on me.