Reputation: 3284
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 ?
Upvotes: 1
Views: 1134
Reputation: 1126
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
Upvotes: 1
Reputation: 785146
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
Upvotes: 3
Reputation: 626826
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.
Upvotes: 1
Reputation: 133518
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.
Upvotes: 3