Extracting name between two delimiters via awk

I have a string like this

<user>@<server>:<port>:/foo/bar

and I would like to extract the user, server, port and directory.

The user can easily be extracted by

echo <string> | awk -F"@" '{print $1;}'

But the server lies within two different delimeters. Is this possible via awk?

3 Answers

You can combine two cut commands to extract the server name:

echo <string> | cut -d":" -f1 | cut -d"@" -f2

Explanation:

  • echo <string> | use the string as input
  • cut -d":" -f1 | set field delimiter to : and extract the first field (<user>@<server>)
  • cut -d"@" -f2 set filed delimiter to @ and extract the secon field (<server>)
2

Yes it is possible - using a regular expression for the delimiter

$ echo '<user>@<server>:<port>:/foo/bar' | awk -F'@|:' '{print $1; print $2; print $3;print $4;}'
<user>
<server>
<port>
/foo/bar

or

$ echo '<user>@<server>:<port>:/foo/bar' | awk -F'[@:]' '{print $1; print $2; print $3;print $4;}'
<user>
<server>
<port>
/foo/bar

How about only,

grep -Eoi "[a-z/]{1,}" <<< "<user>@<server>:<port>:/foo/bar"
user
Server
port
/foo/bar

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like