How to extract two different columns separated by different delimiters into one row
I have below dataset 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
output, which is mainly intended:
directory_name <whitespace> tcp://someurl
another_directory_name <whitespace> tcp://someotherurl
Below is the command I'm trying, but I get either url or directory name
cat the_file.txt|awk -F '/' '{print $5}'
cat the_file.txt|awk -F '=' '{print $2}'
could there be a way to run both of the above command at the same time and get the output on the same line? Significantly apciciated
+3
source to share
3 answers
Just save the first command and make a setting for the second: 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
+3
source to share
FROM 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 of -
^([^/]*/){4}
matches upto/
before a required field -
([^/]+)
required field -
.*=
to the last=
in the line -
\2
replace with required field, given patterns have a space beforetcp
, if not guaranteed, uses|^([^/]*/){4}([^/]+).*= *|\2 |
+1
source to share