3

I have below set of data in unix txt file:

/this/is/a/directory_name/which/i/want:listen= tcp://someurl
/this/is/a/another_directory_name/which/i/want:listen= tcp://someotherurl

the output which basically intended is:

directory_name <whitespace> tcp://someurl
another_directory_name <whitespace> tcp://someotherurl

Below is the command that I am trying but only getting either the url or either the directoryname

cat the_file.txt|awk -F '/' '{print $5}'
cat the_file.txt|awk -F '=' '{print $2}'

can there be a way to achive both of the above command simultaneously and get the output in the same line ? Much appriciated

fedorqui
  • 275,237
  • 103
  • 548
  • 598
anmonu
  • 169
  • 1
  • 13

3 Answers3

3

Just keep the first command and do a tweak for the second one: split the full text based on = and print the second resulting field:

$ awk -F'/' '{split($0, a,"="); print $5, a[2]}' file
directory_name  tcp://someurl
another_directory_name  tcp://someotherurl

Or the last one if there are many =s:

$ awk -F'/' '{n=split($0, a,"="); print $5, a[n]}' file
directory_name  tcp://someurl
another_directory_name  tcp://someotherurl
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 2
    `awk '{split($1,a,"/"); print a[5], $NF}' file`, even, to live with the default `FS`. – James Brown Apr 13 '17 at 09:54
  • 1
    Many Thanks for this Tweak @fedorqui especially for the second one. It is going to ease my work so much :) Also the split parameter can it take care of multiple delimiters viz more than 2. – anmonu Apr 13 '17 at 09:57
  • @Anurodh yes, `split()` can take multiple delimiters. However, you may also consider [using awk multiple delimiters in FS](http://stackoverflow.com/a/26960733/1983854). But it just depends on the context you are working on, without further details it is hard to tell. – fedorqui Apr 13 '17 at 10:01
  • @fedorqui Nope, there is `//` in $NF (tried that :). – James Brown Apr 13 '17 at 10:01
  • 1
    @JamesBrown I like [your first suggestion](http://stackoverflow.com/questions/43388422/how-to-extract-two-different-columns-separated-by-different-delimiters-into-the/43388503?noredirect=1#comment73839071_43388503), good one! As per your second comment, I'd like to mention I was referring to multiple delimiters in awk in general, not that it could be used for this specific problem. – fedorqui Apr 13 '17 at 10:10
1

With sed

$ sed -E 's|^([^/]*/){4}([^/]+).*=|\2|' ip.txt 
directory_name tcp://someurl
another_directory_name tcp://someotherurl
  • -E use ERE, some versions will require -r instead
  • ^([^/]*/){4} matches upto / just before required field
  • ([^/]+) the required field
  • .*= upto last = in the line
  • \2 replace with required field, there is space before tcp in given samples, if this is not guaranteed, use s|^([^/]*/){4}([^/]+).*= *|\2 |
Graham
  • 7,431
  • 18
  • 59
  • 84
Sundeep
  • 23,246
  • 2
  • 28
  • 103
0
$ awk '{split($1,d,"/"); print d[5], $2}' file
directory_name tcp://someurl
another_directory_name tcp://someotherurl
Ed Morton
  • 188,023
  • 17
  • 78
  • 185