2

I have a string

hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/

and what I want is just

hdfs://ussbssn01.us.xyz.com//

i have tried awk, sed but I did not get what I was expecting because the match string // was repeating. Is there a way to get my desired output?

2
  • What would you want if the input string was hdfs://ussbssn01.us.xyz.com//data//ip/list/usa/?
    – John1024
    Aug 18 '16 at 6:52
  • All I want is just hdfs://ussbssn01.us.xyz.com// Aug 18 '16 at 6:54
3

To obtain the first two strings with their match strings:

$ s=hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/
$ echo "$s" | awk -F// '{print $1 FS $2 FS}'
hdfs://ussbssn01.us.xyz.com//

How it works:

  • -F//

    This tells awk to use // as the field separator.

  • print $1 FS $2 FS

    This tells awk to print the first field, a field separator, the second field, and another field separator.

Alternative

To obtain everything up to and including the last of the match strings:

$ echo "$s" | awk -F// '{$NF=""} 1' OFS=//
hdfs://ussbssn01.us.xyz.com//

How it works:

  • -F//

    This tells awk to use // as the field separator for input.

  • $NF=""

    This tells awk to replace the last field with the empty string.

  • 1

    This is awk's cryptic shorthand for print-the-record.

  • OFS=//

    This tells awk to use // as the field separator for output.

1
  • @AlexRajKaliamoorthy Explanation added. As per your clarification, you want the first solution, not the one labeled "alternative."
    – John1024
    Aug 18 '16 at 6:56
1

with grep

$ grep -o '^.*//.*//' <<< "hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/"
hdfs://ussbssn01.us.xyz.com//

$ grep -oP '^.*?//.*?//' <<< "hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/"
hdfs://ussbssn01.us.xyz.com//

$ grep -oP '^.*?com//' <<< "hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/"
hdfs://ussbssn01.us.xyz.com//
  • The first one works if there are only two sets of //
  • The second one extracts upto the second set of //
  • The third one works if required string ends with com//

Edit:

As @ilkkachu points out, [^/]* (zero or more characters, other than /) can be used instead of .*? in the second case. Example:

$ grep -o '^[^/]*//[^/]*//' <<< "hdfs://ussbssn01.us.xyz.com//data/ip/list/usa//"
hdfs://ussbssn01.us.xyz.com//
1
  • 1
    You could use [^/]* instead of .*?, to not require Perl regexes, since the matched part will not have slashes if it's a host name as it seems to be.
    – ilkkachu
    Aug 18 '16 at 14:54
0

If you have the string in a shell variable, var, in ksh or bash:

$ var="hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/"
$ printf "%s\n" "${var%//*}"
hdfs://ussbssn01.us.xyz.com

The ${var%suffix} variable substitution will trim suffix off the value of var.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.