print the last line of an recurring pattern on the 3rd field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print the last line of an recurring pattern on the 3rd field
# 1  
Old 01-11-2011
print the last line of an recurring pattern on the 3rd field

How can i awk/sed to print the last line of an recurring pattern on the 3rd field?

Input lines:
Code:
123456.1 12 1357911 11111.1 01
123456.2 12 1357911 11111.2 02
123456.3 12 1357911 11111.3 03
123456.4 12 1357911 11111.4 04
123456.5 12 1357911 11111.5 05
246810.1 12 1357911 22222.1 01
246810.2 12 1357911 22222.2 02
246810.3 12 1357911 22222.3 03
789101.1 12 1357911 33333.1 01
789101.2 12 1357911 33333.2 02
789101.3 12 1357911 33333.3 03
789101.4 12 1357911 33333.4 04
789101.5 12 1357911 33333.5 05

Desired output:
Code:
123456.5 12 1357911 11111.5 05
246810.3 12 1357911 22222.3 03
789101.5 12 1357911 33333.5 05

Please assist and thanking you in advance.

---------- Post updated at 04:29 PM ---------- Previous update was at 04:19 PM ----------

i think i found the answer on this link - change to $3 instead of $1
https://www.unix.com/shell-programmin...eld-match.html

.. test in progress..
# 2  
Old 01-11-2011
By third field, you mean the 1's, 2's and 3's but not the fraction, apparently. Do we need to preserve order of groups? Are they always in the right order? For instance, sort -u preserves the first for any key value, so you can reverse the lines (tail -r) and key sort on "the third field", but it will re-sort by the third field. Well, probably harder in sed, using regex to find fields:
Code:
sed '
  :loop
  $b
  N
  s/^[^.]*\.[^.]* \([^.]*\)\..*\n\([^.]*\.[^.]* \1\..*\)/\2/
  t loop
  P
  s/.*\n//
  t loop
 '

Narrative: Set a branch target named loop, if at EOF branch to end (print and exit), get the next line, if the key fields are the same discard the first line and go to loop, print the first line, discard it and branch to loop. This regex keys on '.' mostly, ' ' occasionally, exploiting your data structure to avoid a lot of repeated regex patterns to count fields.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 01-11-2011
test completed and working as expected per mod below:

Code:
awk '$3 != prevIDX && FNR != 1 { print prev } { prevIDX=$3; prev=$0 } END { print prev }'

# 4  
Old 01-11-2011
Code:
 awk -F"." '{u[$1]=$0}END{for(i in u) print u[i]}'

This User Gave Thanks to JavaHater For This Post:
# 5  
Old 01-12-2011
Where did you say $0 was the key or . the delimiter??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print field after pattern in all lines

data: hello--hello1--hello2--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars# I want to be able to print all the values that are found between the patterns "#growncars#" and the next "#growncars#" on the same line. so the output should be: ... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

5. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

6. Shell Programming and Scripting

awk to compare 2nd and 3rd field and print the differences

need a one liner to compare 2nd and 3rd field and print values that are not matched in 2nd field Input col 2 col 3 1.1.1.1 11.11.11.11 8.8.8.8 0.0.0.0 3.3.3.3 2.2.2.2 7.7.7.7 3.3.3.3 5.5.5.5 1.1.1.1 4.4.4.4 6.6.6.6 9.9.9.9 output 7.7.7.7 ... (12 Replies)
Discussion started by: chidori
12 Replies

7. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

8. Shell Programming and Scripting

only print line if 3rd field is 01

Similar question... I have a space delimited text file and I want to only print the lines where the 3rd word/field/column is equal to "01" awk '{if $3 = "01" print $0}' something like this. I meant to say: only print line IF 3rd field is 01 (2 Replies)
Discussion started by: ajp7701
2 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

How to print 3rd to last line of file?

Hi, I have a ksh script I would like to modify. What I need it to do is look at an ever changing log file and print the 3rd to last line. Is there a command that will display this? I can not use line numbers because the file is always growing. Thanks for any help (2 Replies)
Discussion started by: NivekRaz
2 Replies
Login or Register to Ask a Question