Home > front end >  Is there a better and cleaner way to remove strings using AWK
Is there a better and cleaner way to remove strings using AWK

Time:03-29

I'm wondering Is there a better and cleaner way to remove strings at beginning and last of each line in a file using AWK only?

Here's what I got so far

cat results.txt | awk '{gsub("https://", "") ;print}' | tr -d ":443"

File: results.txt

https://www.google.com:443
https://www.tiktok.com:443
https://www.instagram.com:443


To get the result

www.google.com
www.tiktok.com
www.instagram.com

CodePudding user response:

Use / and : as field separators and print fourth column:

awk -F '[/:]' '{print $4}' results.txt

Or use https:// and : as field separators and print second column:

awk -F 'https://|:' '{print $2}' results.txt

Output:

www.google.com
www.tiktok.com
www.instagram.com

CodePudding user response:

If it's a list of URLs like that, you could take advantage of the fact that the field separator in awk can be a regular expression:

awk -F':(//)?' '{print $2}'

This says that your field seperator is ": optionally followed by //", which would split each line into:

[$1] http
[$2] www.google.com
[$3] 443

And then we print out only field $2.

  • Related