Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy matching lines to a second file

Tags:

sed

awk

I need to copy all lines in a file matching a pattern to a second file.

In detail: I have a sql dump and want to create a second sql file which includes all commands for tables whose name matches dx_postings, dx_postings_archive, and so on. The pattern should be dx_postings.

Any ideas? I'd prefer an awk or sed solution.

like image 658
ChrJantz Avatar asked Feb 19 '23 01:02

ChrJantz


2 Answers

The sed solution:

sed -ne '/pattern/ p' infile >outfile
like image 154
Birei Avatar answered Feb 23 '23 12:02

Birei


Ok, see this :

awk '/pattern/' FILE > NEWFILE

more specific :

awk '/^(DROP|LOCK) .*dx_postings/' file.sql > newfile.sql

If you have INSERT or CREATE statements, this is more tricky because there's more than one lines.

like image 20
Gilles Quenot Avatar answered Feb 23 '23 13:02

Gilles Quenot