D
D
Dmitry Sergeev2013-08-18 20:54:31
linux
Dmitry Sergeev, 2013-08-18 20:54:31

How to get ip and a specific substring from a string using grep and cut?

From the web server log file, you need to get pairs like IP: __utma. The lines in the file are standard, only through "|" cookies are saved.

80.247.101.110 - - [16/Aug/2013:06:58:23 +0400] "POST /edit HTTP/1.1" 404 327 "http://site.ru/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 YaBrowser/1.7.1364.17262 Safari/537.22" "-" | "__utma=230214667.2058679839.1371519930.1376615440.1376617932.52; __utmc=230214667; __utmz=230214667.1371519930.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Amet13, 2013-08-18
@JetMaster

Once such a booze has gone, then:

cat test.log | awk '{print $1,":",substr($0, index($0,"__utma"),64)}' | sed 's/ : __utma=/: /' | sed 's/;//'

I hope I understand you correctly?
At the exit:
80.247.101.110: 230214667.2058679839.1371519930.1376615440.1376617932.52

3
3vi1_0n3, 2013-08-18
@3vi1_0n3

If you can awk, then like this:

cat ./data.txt | awk '{print $1,":",substr($0, index($0,"__utma"),64)}'

T
tgz, 2013-08-19
@tgz

Here's how it's possible:

for line in $(cat log);
do
    ip=$(echo $line | grep -o "^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+")
    utma=$(echo $line |grep -o "__utma=\([0-9]\+\.\)\+[0-9]\+" | cut -d '=' -f2 )
    echo -en "$ip $utma\n"
done

Z
Zhep Ebrilov, 2013-08-20
@baragoon

cat log | awk -F"-" '{print $1,$4}' | awk '{print $1":",$4}' | sed 's/"__utma=//g' | sed 's/;$//'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question