Need a little help with bash line formatting...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a little help with bash line formatting...
# 1  
Old 02-02-2011
Need a little help with bash line formatting...

Hi there,

suppose I have a line that looks like this:

Quote:
13:47:54.830735 192.168.168.65.54487 85.214.141.246.80: 336
and I want it to look like that:

Quote:
13:47:54 192.168.168.65 85.214.141.246 336
The first line is already the output of a long expression with a lot of piping and formatting with awk etc., but now I'm somehow stuck with the rest. Can you give me a clue how to achieve it? Thanks for your help!

PS: The command that outputs the lines looks like that at the moment:

Code:
tcpdump -n | grep 192.168.168. | grep -v "length 0" | grep length | awk '{print $1,$3,$5,$(NF)}' | grep 192.168.168 | grep -v Request

# 2  
Old 02-02-2011
There are probably other (better) approaches to get the output.

Post some lines of the output of the tcpdump command and the desired output.
# 3  
Old 02-02-2011
Hi Franklin,

yes, I guess so Smilie Thanks for your reply.

Ok, this is an excerpt from the pure tcpdump output:

Code:
10:51:16.107188 IP 192.168.168.217.32792 > 85.214.102.109.3690: Flags [.], ack 117, win 92, options [nop,nop,TS val 1194354 ecr 2738222176], length 0
10:51:16.107386 IP 192.168.168.217.32792 > 85.214.102.109.3690: Flags [P.], seq 1:123, ack 117, win 92, options [nop,nop,TS val 1194354 ecr 2738222176], length 122
10:51:16.143221 IP 85.214.102.109.3690 > 192.168.168.217.32792: Flags [.], ack 123, win 46, options [nop,nop,TS val 2738222212 ecr 1194354], length 0
10:51:16.143276 IP 85.214.102.109.3690 > 192.168.168.217.32792: Flags [P.], seq 117:186, ack 123, win 46, options [nop,nop,TS val 2738222214 ecr 1194354], length 69
10:51:16.143768 IP 192.168.168.217.32792 > 85.214.102.109.3690: Flags [P.], seq 123:140, ack 186, win 92, options [nop,nop,TS val 1194363 ecr 2738222214], length 17
10:51:16.179296 IP 85.214.102.109.3690 > 192.168.168.217.32792: Flags [P.], seq 186:250, ack 140, win 46, options [nop,nop,TS val 2738222248 ecr 1194363], length 64
10:51:16.179598 IP 192.168.168.217.32792 > 85.214.102.109.3690: Flags [P.], seq 140:185, ack 250, win 92, options [nop,nop,TS val 1194372 ecr 2738222248], length 45
10:51:16.215463 IP 85.214.102.109.3690 > 192.168.168.217.32792: Flags [P.], seq 250:381, ack 185, win 46, options [nop,nop,TS val 2738222284 ecr 1194372], length 131
10:51:16.216983 IP 192.168.168.217.32792 > 85.214.102.109.3690: Flags [R.], seq 185, ack 381, win 108, options [nop,nop,TS val 1194381 ecr 2738222284], length 0
10:51:16.320624 IP 62.27.87.7.80 > 192.168.168.76.4076: Flags [.], seq 13663:15043, ack 0, win 63735, length 1380
10:51:16.324590 IP 62.27.87.7.80 > 192.168.168.76.4076: Flags [.], seq 15043:16423, ack 0, win 63735, length 1380
10:51:16.324706 IP 192.168.168.76.4076 > 62.27.87.7.80: Flags [.], ack 16423, win 65535, length 0
10:51:16.332627 IP 62.27.87.7.80 > 192.168.168.76.4076: Flags [.], seq 16423:17803, ack 0, win 63735, length 1380
10:51:16.340671 IP 62.27.87.7.80 > 192.168.168.76.4076: Flags [.], seq 17803:19183, ack 0, win 63735, length 1380

What I need is this (last line as example):

Code:
10:51:16 62.27.87.7.80 192.168.168.76 1380

The number of fields in a line is not constant as it depends on the type of packet.

What I need is the time (hh:mm:ss), the first IP, the second IP and the length.
# 4  
Old 02-02-2011
Try this:
Code:
tcpdump -n | 
awk '$NF != "0" && /192.168.168/ && !/Request/ {
  sub("\..*",x,$1)
  sub("\.[^.]*:$",x,$5)
  print $1, $3, $5, $NF
}'


Last edited by Franklin52; 02-02-2011 at 11:16 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issues formatting output of two commands in a single line.

I wish to generate output of two commands in the same line separated by a single white-space. Below is my command and output in the same line. ls -ltr fname1.out | awk '{$2=$4=$5=x; print}' | tr '\n' '\t' | tr -s ' '; cksum<fname1.out | cut -d' ' -f1 Output: -rw-r--r--. root Aug 26 16:57... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

4. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

5. Shell Programming and Scripting

Bash formatting data into columns

Hi guys, I'm trying to create a table of aggregated data using just bash commands. My data is in three columns, for example: 2014-01-01 testA 64 2014-01-01 testB 27 2014-02-01 testA 31 2014-02-02 testB 29 2014-02-02 testC 12 And the result I am looking for is: ... (4 Replies)
Discussion started by: mccmjc
4 Replies

6. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

7. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

8. Shell Programming and Scripting

Formatting a text file to get data in exact line by line

I have my data something like this SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430693 07/01/2009 05:16:40 FR SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430746 07/01/2009 05:18:05 FR I want the output as follows.... (1 Reply)
Discussion started by: rdhanek
1 Replies

9. UNIX for Dummies Questions & Answers

Formatting a line of data into columns

Hi all, I'm a little stuck with a data file I've been collecting data in. The file contains one field of data running continuously down the file and I can't work out how to format the data into three columns. This is a mock up of the file: Each r# is a random number and varies in length, this... (3 Replies)
Discussion started by: nistleloy
3 Replies

10. Shell Programming and Scripting

Need help in a line formatting

i have a script that check for a file called TNS.txt.I need little help in formatting TNS.txt 0001804026 what i want is whenever there is space after the digit it should be rejected or should not be taken into account. TNS.txt with space: 0001804026 wc -c 11 The below... (4 Replies)
Discussion started by: ali560045
4 Replies
Login or Register to Ask a Question