M
M
MasterCopipaster2020-12-28 17:30:46
terminal emulator
MasterCopipaster, 2020-12-28 17:30:46

How to find lines by filter in large files?

Hello, tell me how to write a command in linux
. I have a file that cannot be read in RAM 23GB, this is essentially a sql dump (text)
I need to display the numbers of all lines where the expression DROP TABLE IF EXISTS occurs

example:
in the file 100% there is such a line i I'm trying to do this:
DROP TABLE IF EXISTS `crosses`;

cat < dump.sql | wc -l | grep -i 'DROP TABLE IF EXISTS' | awk {'print $1'}

But I get an empty result (I can't find the string) and I've also been like this
cat < dump.sql | wc -l | grep -i 'DROP TABLE IF EXISTS'


Tell me how can I get the line numbers from the file where the substring DROP TABLE IF EXISTS occurs?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2020-12-28
@MasterCopipaster

It's kind of weird you're doing this...
cat dump.sql | grep 'DROP TABLE IF EXISTS' -n
will output the line number and the line itself

G
ge, 2021-01-06
@gedev

Let me explain what was wrong in your team. It looks like you don't quite understand the pipeline ( | ) principle.

cat < dump.sql | wc -l | grep -i 'DROP TABLE IF EXISTS' | awk {'print $1'}

Here cat < dump.sql does what it's supposed to do - print the entire dump to STDOUT, but cat doesn't need output redirection ( < ), so it's better to write cat dump.sql .
The dump line is then passed to STDIN by wc -l . At the output, a line like 12345 is sent to STDOUT . That is, it is simply the number of rows in the original dump. But next on line 12345 you are trying to find occurrences of DROP TABLE IF EXISTS with grep . Already here you get an empty line, and awk shows the result of working on an empty line - a new empty line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question