Reputation: 375
I want to print the line till pattern is matched but including the pattern
for example:
I want to use ".com" as match string and just print http://google.com , ignoring rest of the line.
awk or grep would be helpful.
I tried codes from posts but didnt get exactly that i wanted.
request your help
Upvotes: 0
Views: 328
Reputation: 37258
Another approach. Assuming that your goal is to harvesting top-level domains that all end in .com
, then you can just delete everything at the end, including .com
and replace it with only .com
, i.e.
echo "http://google.com/search
1
2" | awk '/\.com/{sub(/\.com.*/, ".com", $0) ;print $0}'
output
http://google.com
I've included extra lines of input to show that only lines containing .com
are processed.
If you want to print out all lines of input, remove the initial match to reg-ex, i.e.
echo "http://google.com/search
1
2" | awk '{sub(/\.com.*/, ".com", $0) ;print $0}'
output 2
http://google.com
1
2
IHTH
Upvotes: 0
Reputation: 41446
Here is an awk
echo "http://google.com/search" | awk -F/ -vOFS=/ '/\.com/ {$NF="";sub(/\/$/,"");print}'
http://google.com
Upvotes: 0
Reputation: 784938
Use grep -o
to output only matched string:
s='http://google.com/search'
grep -o '.*\.com' <<< "$s"
http://google.com
Upvotes: 2