1

I have a file called DB_create.sql which has this line

CREATE DATABASE testrepo;

I want to extract only testrepo from this. So I've tried

cat DB_create.sql | awk '{print $3}'

This gives me testrepo;

I need only testrepo. How do I get this ?

1
  • I don’t think AWK is the most suitable tool here. I’d use sed: sed -n '/^CREATE DATABASE \(.*\);$/s//\1/p' DB_create.sql. But even that’s brittle because it doesn’t understand SQL, it just looks for verbatim character-by-character match. This can be fine in a pinch but a robust solution requires an SQL parser. Commented Apr 21, 2021 at 10:44

4 Answers 4

3

With your shown samples, please try following.

awk -F'[ ;]' '{print $(NF-1)}' DB_create.sql

OR

awk -F'[ ;]' '{print $3}' DB_create.sql

OR without setting any field separators try:

awk '{sub(/;$/,"");print $3}'  DB_create.sql

Simple explanation would be: making field separator as space OR semi colon and then printing 2nd last field($NF-1) which is required by OP here. Also you need not to use cat command with awk because awk can read Input_file by itself.

Sign up to request clarification or add additional context in comments.

Comments

3

Using gnu awk, you can set record separator as ; + line break:

awk -v RS=';\r?\n' '{print $3}' file.sql

testrepo

Or using any POSIX awk, just do a call to sub to strip trailing ;:

awk '{sub(/;$/, "", $3); print $3}' file.sql

testrepo

Comments

1

You can use

awk -F'[;[:space:]]+' '{print $3}' DB_create.sql

where the field separator is set to a [;[:space:]]+ regex that matches one or more occurrences of ; or/and whitespace chars. Then, Field 3 will contain the string you need without the semi-colon.

More pattern details:

  • [ - start of a bracket expression
    • ; - a ; char
    • [:space:] - any whitespace char
  • ] - end of the bracket expression
  • + - a POSIX ERE one or more occurrences quantifier.

See the online demo.

Comments

1

Use your own code but adding the function sub():

cat DB_create.sql | awk '{sub(/;$/, "",$3);print $3}'

Although it's better not using cat. Here you can see why: Comparison of cat pipe awk operation to awk command on a file

So better this way:

awk '{sub(/;$/, "",$3);print $3}' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.