Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract two different columns separated by different delimiters into the same line

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

like image 259
anmonu Avatar asked Dec 06 '25 08:12

anmonu


1 Answers

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
like image 103
fedorqui 'SO stop harming' Avatar answered Dec 08 '25 23:12

fedorqui 'SO stop harming'