KSH or AWK for fetching the first and the last line based on time.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH or AWK for fetching the first and the last line based on time.
# 1  
Old 09-12-2012
KSH or AWK for fetching the first and the last line based on time.

Dears could anybody please help me with the below task as I am a newbie to programming.

I have a text file with 4 columns (priv_ip,time,pub_ip).
eg:
Code:
10.160.101.160,0708044510,22.203.195.151
10.160.101.160,0708044645,22.203.195.151
10.160.101.160,0708050410,22.203.195.151
10.160.101.161,0707073952,22.203.199.152
10.160.101.161,0707074936,22.203.199.152
10.160.101.161,0707075457,22.203.199.152

I am just trying to get the first and the last line with the same priv_ip and pub_ip.

Please HELP i am stuck...!!!!!

Last edited by Franklin52; 09-12-2012 at 06:49 AM.. Reason: Please use code tags for data and code samples, thanks
# 2  
Old 09-12-2012
Quote:
Originally Posted by BrownBob
I have a text file with 4 columns (priv_ip,time,pub_ip).
I can count 3. May be I count bad Smilie.
Quote:
I am just trying to get the first and the last line with the same priv_ip and pub_ip.
What's the expected output?
# 3  
Old 09-12-2012
Try...
Code:
$ cat file1
10.160.101.160,0708044510,22.203.195.151
10.160.101.160,0708044645,22.203.195.151
10.160.101.160,0708050410,22.203.195.151
10.160.101.161,0707073952,22.203.199.152
10.160.101.161,0707074936,22.203.199.152
10.160.101.161,0707075457,22.203.199.152

$ awk -F, '$1!=p1&&$3!=p3{if(p1)print p0;print;p1=$1;p3=$3}{p0=$0}END{print}' file1 > file2

$ cat file2
10.160.101.160,0708044510,22.203.195.151
10.160.101.160,0708050410,22.203.195.151
10.160.101.161,0707073952,22.203.199.152
10.160.101.161,0707075457,22.203.199.152

$

# 4  
Old 09-12-2012
Dear Ygor.. Thanks for the prompt response....

There could be more lines between the first and the last required record. not only 3.. but yes the expected output is what you got....There are some single records could those be printed as well....

perfecto... it worked....... Thanks a lottttttttttttttttttttttttt..... you are a genious...

Example:
Code:
10.160.101.160,0707073711,22.203.203.200
10.160.101.160,0707075132,22.203.210.249
10.160.101.160,0707085436,22.203.210.249
10.160.101.160,0707091712,22.203.221.176
10.160.101.160,0707091811,22.203.221.176
10.160.101.160,0707091845,22.203.221.176
10.160.101.160,0707092111,22.203.208.18
10.160.101.160,0707094653,22.203.240.84
10.160.101.160,0707094753,22.203.240.84
10.160.101.160,0707094824,22.203.240.84
10.160.101.160,0707101747,22.203.240.84
10.160.101.161,0707104159,22.203.201.19
10.160.101.161,0707141428,22.203.223.22
10.160.101.161,0707141728,22.203.225.28
10.160.101.161,0707155114,22.203.224.14
10.160.101.161,0707155315,22.203.224.14
10.160.101.161,0707155324,22.203.224.14
10.160.101.161,0707161956,22.203.224.14

Expected Output:
Code:
10.160.101.160,0707073711,22.203.203.200
10.160.101.160,0707075132,22.203.210.249
10.160.101.160,0707085436,22.203.210.249
10.160.101.160,0707091712,22.203.221.176
10.160.101.160,0707091845,22.203.221.176
10.160.101.160,0707092111,22.203.208.18
10.160.101.160,0707094653,22.203.240.84
10.160.101.160,0707101747,22.203.240.84
10.160.101.161,0707104159,22.203.201.19
10.160.101.161,0707141728,22.203.225.28
10.160.101.161,0707155114,22.203.224.14
10.160.101.161,0707161956,22.203.224.14

Please help!!!!


God bless u!!!!

---------- Post updated at 03:09 PM ---------- Previous update was at 02:51 PM ----------

It should compare both the private and public IP just the first and the last time record.. if there should be any single record they should be printed as it is....... Please helppppppppppp!!!!

Last edited by Franklin52; 09-13-2012 at 05:22 AM.. Reason: Code tags
# 5  
Old 09-12-2012
Try...
Code:
awk -F, '$1!=p1||$3!=p3{if(p1&&p0!=p)print p0;print;p1=$1;p3=$3;p=$0}{p0=$0}END{if(p0!=p)print}' file1 > file2

# 6  
Old 09-13-2012
It worked... Thanks a lot Ygor.... Your help is greatly appreciated.

God bless u!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line based on condition in awk

In the following tab-delimited input, I am checking $7 for the keyword intronic. If that keyword is found then $2 is split by the . in each line and if the string after the digits or the +/- is >10, then that line is deleted. This will always be the case for intronic. If $7 is exonic then nothing... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Fetching a line matching a pattern

Hi Gurus, I have a file as follows (Sample shown below but the list is very huge) SCHEDULE WS1#JS1 RUNCYCLE1 : WS1#JOB1 WS1#JOB2 FOLLOWS JOB1 END SCHEDULE WS2#JS1 RUNCYCLE2 : WS1#JOB3 WS1#JOB1 FOLLOWS JOB3 WS2#JOB1 (10 Replies)
Discussion started by: jayadanabalan
10 Replies

3. Shell Programming and Scripting

Awk: time intervals based on epoch time

I have a list of epoch times delimited by "-" as follows: 1335078000 - 1335176700 1335340800 - 1335527400 1335771300 - 1335945600 1336201200 - 1336218000 The corresponding dates are: 20120422 1000 - 20120423 1325 20120425 1100 - 20120427 1450 20120430 1035 - 20120502 1100 ... (3 Replies)
Discussion started by: alex2005
3 Replies

4. Shell Programming and Scripting

Fetching values in CSV file based on column name

input.csv: Field1,Field2,Field3,Field4,Field4 abc ,123 ,xyz ,000 ,pqr mno ,123 ,dfr ,111 ,bbb output: Field2,Field4 123 ,000 123 ,111 how to fetch the values of Field4 where Field2='123' I don't want to fetch the values based on column position. Instead want to... (10 Replies)
Discussion started by: bharathbangalor
10 Replies

5. Shell Programming and Scripting

Fetching record based on Uniq Key from huge file.

Hi i want to fetch 100k record from a file which is looking like as below. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ... (17 Replies)
Discussion started by: lathigara
17 Replies

6. Shell Programming and Scripting

KSH or Awk first and last line based on column 2

Example: 10.160.101.160,0707073711,22.203.203.200 10.160.101.160,0707075132,22.203.210.249 10.160.101.160,0707085436,22.203.210.249 10.160.101.160,0707091712,22.203.221.176 10.160.101.160,0707091811,22.203.221.176 10.160.101.160,0707091845,22.203.221.176... (1 Reply)
Discussion started by: BrownBob
1 Replies

7. Shell Programming and Scripting

ksh: how to extract strings from each line based on a condition

Hi , I'm a newbie.Never worked on Unix before. I want a shell script to perform the following: I want to extract strings from each line ,based on the type of line(Nameline,Subline) and output it to another file.Below is a sample format. 2010-12-21 14:00"1"Nameline"Midterm"First Name:Jane ... (4 Replies)
Discussion started by: angie1234
4 Replies

8. Shell Programming and Scripting

Logic for file fetching based on date

Dear friends, I receive the following files into a FTP location on a daily basis -rw-r----- 1 guest ftp1 5021 Aug 19 09:03 CHECK_TEST_Extracts_20080818210000.zip -rw-r----- 1 guest ftp1 2437 Aug 20 05:15 CHECK_TEST_Extracts_20080819210000.zip -rw-r----- 1 guest ... (2 Replies)
Discussion started by: sureshg_sampat
2 Replies

9. UNIX for Dummies Questions & Answers

Average in awk based on time

Hi I am looking for an awk script which can compute the average of the last column based on the date and time. The file looks: site1,"2000-01-01 00:00:00", "2000-01-01 00:59:00",0.013 site2,"2000-02-01 01:00:00", "2000-02-01 01:59:00",0.035 site1,"2000-02-01 02:00:00", "2000-02-01... (15 Replies)
Discussion started by: kathy wang
15 Replies

10. Shell Programming and Scripting

Remove lines, Sorted with Time based columns using AWK & SORT

Hi having a file as follows MediaErr.log 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:12:16 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:22:47 84 Server1 Policy1 Schedule1 master1 05/08/2008 03:41:26 84 Server1 Policy1 ... (1 Reply)
Discussion started by: karthikn7974
1 Replies
Login or Register to Ask a Question