1

I have a following scenario to do in a text file.

I have a large text file which gets generated from my server and includes the following information.

a)Lots of garbage data in ASCII with some ip addresses of computers connected to the server.

b)IP address is not written in whole instead it is like this sfafaffs@192@sdsfa@168@dfs8989090sdffsff0.09sf@90@90dsaaQW@1@. That is ip is in enclosed in the @tags

My goal is to make a text file which will print the ip with corresponding counter variable showing occurrences of particular ip.

How to approach to solve this problem.please also guide me don't post solution since that will limit my abilites. for example if next time I am faced with other problem related with this what would I do?So please advise me what should be strategy to solve these types of problems?

2
  • just to clarify: what kind of solution are you looking for? specific language? Mar 28, 2014 at 18:54
  • @Jacob I am on ubuntu and there are various tools like sed emach,awk,grep and vi editor.Which tool by learning will make my life simpler for any problem like this in future
    – Edison
    Mar 28, 2014 at 18:55

1 Answer 1

2

In this excercise grep, sed, and awk will be your friends.

Here's a hint for the first problem: Parsing the IP from the line of junk

This will work if each IP is on it own line, surrounded by garbage, with @ signs on each side of the numbers.

'test' is just a file I pasted your example into

grep -nPo '@\d{1,3}@' test | sed 's/@//g'

grep: -n prepends the line number, P allows you to use Perl regex, o return only the matched part.

the regex matched any group of digits of length 1-3 surrounded by @ signs

Sed: strips away the @ sign by replacing them with nothing

Your output will we be:

1:192
1:168
1:90
1:1

Where 1 is the line number. I left it on there because you can use awk to find pieces with matching line numbers and concatenate them to make an IP address.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .