
Eu tenho um arquivo de log para o qual estou escrevendo um script, portanto ele mostra apenas determinados campos de saída. A última parte que preciso é encurtar os URLs para que a linha seja interrompida quando eles clicarem em ".com", ".edu", ".org" e assim por diante. Existe uma maneira de fazer isso com grep? Devo procurar outros comandos?
A saída de exemplo é:
student1234 "GET https://www.noname.com:443/login"
student4567 "GET http:// www.noip.edu:80/start/noname"
student8901 "GET http:// www.testing.org:80/search/change"
O que eu preciso é:
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org
Responder1
Tantas opções, escolha a que você gosta.
Usando grep
:
grep -o '^[^:]\+:[^:]\+' file.txt
usando cut
:
cut -d: -f1-2 file.txt
usando awk
:
awk -F: '{ print $1$2 }' file.txt
usando sed
:
sed 's/^\([^:]\+:[^:]\+\).*/\1/' file.txt
usando shell:
while IFS=: read -r i j k; do echo "$i$j"; done <file.txt
usando perl
:
perl -pe 's/^([^:]+:[^:]+).*/$1/' file.txt
Exemplo:
$ grep -o '^[^:]\+:[^:]\+' file.txt
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org
$ cut -d: -f1-2 file.txt
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org
$ awk -F: '{ print $1$2 }' file.txt
student1234 "GET https//www.noname.com
student4567 "GET http// www.noip.edu
student8901 "GET http// www.testing.org
$ sed 's/^\([^:]\+:[^:]\+\).*/\1/' file.txt
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org
$ while IFS=: read -r i j k; do echo "$i$j"; done <file.txt
student1234 "GET https//www.noname.com
student4567 "GET http// www.noip.edu
student8901 "GET http// www.testing.org
$ perl -pe 's/^([^:]+:[^:]+).*/$1/' file.txt
student1234 "GET https://www.noname.com
student4567 "GET http:// www.noip.edu
student8901 "GET http:// www.testing.org