Grep starting from a specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep starting from a specific position
# 1  
Old 07-08-2011
Grep starting from a specific position

Hello people,
I'm scratch my head to find a solution to my problem, I'm absolutely sure this is very simple!!! Smilie

I'm using the tcpdump to show on the screen in real time the UCP traffic:
Code:
tcpdump -l -i bond1 -s 1514 -nntttt -A src or dst 192.168.1.5 and port 10000 | egrep "/51/"

The output will be something like:
Code:
a..'....P.o....P..E.....01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........
a..'....P.....nP..fE....02/00248/O/51/001234567889/1234567890//1//7///////0907110916.........
a..'....P......P..CF....03/00244/O/51/001234567889/1234567890//1//7///////0907110916.........

Now, the first chars (coming from TCP stack) is always different, but have always the same lenght (24 chars).

Is there a way to remove that chars adding a filter directly to the primary command??
Like starting output from 25th char and show on the screen something like: ?
Code:
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........
02/00248/O/51/001234567889/1234567890//1//7///////0907110916.........
03/00244/O/51/001234567889/1234567890//1//7///////0907110916.........

And also, is it possible to have a CR between each lines, like:
Code:
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........

02/00248/O/51/001234567889/1234567890//1//7///////0907110916.........

03/00244/O/51/001234567889/1234567890//1//7///////0907110916.........

Thanks!

Last edited by Lord Spectre; 07-08-2011 at 04:39 AM..
# 2  
Old 07-08-2011
Code:
 
$ cut -c25- test
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........

Code:
 
$ cut -c25- test | nawk '{printf("%s\n\n",$0)}'
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........
 
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........
 
01/00246/O/51/001234567889/1234567890//1//7///////0907110916.........

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-08-2011
Thanks for fast reply itkamaraj,
I already know the simple command cut, but how can I implement it in the tcpdump command?

As already told, the output will be shown @ the screen in real time, so I need to "cut" the output in real time! Smilie

EDIT: Got it!
Code:
tcpdump -l -i bond1 -s 1514 -nntttt -A src or dst 192.168.1.5 and port 10000 | cut -c25- | grep "51/"

But I cannot show the CR between the output lines!
I tried the following without success (no output shown):
Code:
tcpdump -l -i bond1 -s 1514 -nntttt -A src or dst 192.168.1.5 and port 10000 | cut -c25- | grep "51/" | nawk '{printf("%s\n\n",$0)}'

or
Code:
tcpdump -l -i bond1 -s 1514 -nntttt -A src or dst 192.168.1.5 and  port 10000 | cut -c25- | nawk  '{printf("%s\n\n",$0)}' | grep "51/"


Last edited by Lord Spectre; 07-08-2011 at 04:56 AM..
# 4  
Old 07-08-2011
Can you replace grep (in post# 1) with the below sed and try
Code:
sed '/51/s/.\{24\}\(.*\)/\1\n/'

# 5  
Old 07-08-2011
Quote:
Originally Posted by michaelrozar17
Can you replace grep (in post# 1) with the below sed and try
Code:
sed '/51/s/.\{24\}\(.*\)/\1\n/'

Nope! This is what I seen on the screen:

Code:
2011-07-08 09:56:07.118713 IP 192.168.1.5.10000 > 192.168.1.5.5000: . ack 938 win 62907
E..( .@.{...
a.Z
a..'....Rl.....P.............
2011-07-08 09:56:12.769254 IP 192.168.1.5.5000 > 192.168.1.5.10000: P 938:1334(396) ack 960 win 63784
E...:.@.@...
a..
a.Z..'......Rl.P..(1E...50/00394/O/53/192.168.1.55000/1234565/////////////2234234324/0/000/080711100001/3//E/////////////EF.
2011-07-08 09:56:12.836946 IP 192.168.1.5.10000 > 192.168.1.5.5000: P 960:982(22) ack 1334 win 64240
E..>!.@.{..L
a.Z
a..'....Rl.... P....(...50/00020/R/53/A///9B.
2011-07-08 09:56:12.876289 IP 192.168.1.5.5000 >192.168.1.5.10000: . ack 982 win 63784
E..(:.@.@..~

# 6  
Old 07-08-2011
Ok. Maybe there is difference in output you got and posted or there is different version of sed/machine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

3. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

4. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

5. Shell Programming and Scripting

Find the starting position in a file

I have a file called "INPUT" which takes the following format MNT-BANK-NUMBERO:006,00:N MNT-100-ACCOUNT-NUMBERO:018,00:N MNT-1000-DESCRIPTIONO:045:C . . . Now i got to find the displacements of the account numbers of each field of a file. For the field MNT-BANK-NUMBERO:006,00:N, the... (4 Replies)
Discussion started by: bobby1015
4 Replies

6. Shell Programming and Scripting

Remove the spaces at the end of a line starting from a fixed position

I want to remove the trailing spaces at the end of each line starting from a particular position(using ksh script). For example, in the attached file, I want to remove all the spaces starting from the position 430 till the end. The space has to be removed only from the 430th position no matter in... (3 Replies)
Discussion started by: Suryaaravindh
3 Replies

7. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

8. Shell Programming and Scripting

perform some operation on a specific coulmn starting from a specific line

I have a txt file having rows and coulmns, i want to perform some operation on a specific coulmn starting from a specific line. eg: 50.000000 1 1 1 1000.00000 1000.00000 50000.000 19 19 3.69797533E-07 871.66394 ... (3 Replies)
Discussion started by: shashi792
3 Replies

9. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

10. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies
Login or Register to Ask a Question