Page Break with AWK or SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Page Break with AWK or SED
# 1  
Old 06-17-2008
Page Break with AWK or SED

Hello All,

I am new to unix scripting, I have an urgent issue

Is it possible to do a group by on the last column and then a page break on the text file.
For example I have a text file, below is the example data

STRT 2154081~VA ~23606 ~TM14~8506~1485
STRT 2130893~VA ~23602 ~TM15~8602~1586
STRT 2154051~VA ~23602 ~TM15~8802~1588
STRT 2085746~VA ~23662 ~TM17~8362~1783
STRT 2121415~VA ~23662 ~TM17~8362~1783
STRT 52773~VA ~23662 ~TM17~8362~1783
STRT 87121~VA ~23662 ~TM17~8362~1783
STRT 166299~VA ~23185 ~TM41~9085~4190
STRT 167311~VA~23185 ~TM41~9085~4190
STRT 180099~VA ~23185 ~TM41~9085~4190
STRT 2007255~VA ~23185 ~TM41~9085~4190

When I print the report, I would like to have the data printed out in the below format.

STRT 2154081~VA ~23606 ~TM14~8506~1485 (page 1)

STRT 2130893~VA ~23602 ~TM15~8602~1586 (page 2)

STRT 2154051~VA ~23602 ~TM15~8802~1588 (page 3)

STRT 2085746~VA ~23662 ~TM17~8362~1783 (page 4)
STRT 2121415~VA ~23662 ~TM17~8362~1783
STRT 52773~VA ~23662 ~TM17~8362~1783
STRT 87121~VA ~23662 ~TM17~8362~1783

STRT 166299~VA ~23185 ~TM41~9085~4190 (page 5)
STRT 167311~VA~23185 ~TM41~9085~4190
STRT 180099~VA ~23185 ~TM41~9085~4190
STRT 2007255~VA ~23185 ~TM41~9085~4190


Thank you

Last edited by udaybo; 06-17-2008 at 03:46 PM..
# 2  
Old 06-17-2008
Try this:
Code:
gawk \
'{
if(NR == 1)
{
  prev = $NF
  print $0
}
else
{
  if($NF == prev)
    print $0
  else
  {
    print "\f"
    print $0
    prev = $NF
  }
}
}' file

# 3  
Old 06-17-2008
Thanks a lot Cooldude, you saved my day
Thanks again
# 4  
Old 06-18-2008
Or:

Code:
awk  '$0=_[$NF]++?$0:"\f"RS$0' file

# 5  
Old 06-18-2008
Thank you radoulov
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Page number with total number of pages, EG Page 1 of 5

So I've worked how to add page numbers based on regex. It's using the footer text. How do we get the total amount added so we have page number with the total number of pages? Desired output: Page No:1 of 5 Thanks in advance. (15 Replies)
Discussion started by: tugar
15 Replies

2. Shell Programming and Scripting

Page Break in a file for printing

Hi, We have 1lac records in source file and unix script will genarate around 1000 files. From target location the files are taking for printing on physical papers. the page size limitation : 256 Lines Can you please tell me how to insert the page break in a flat file for printer. (5 Replies)
Discussion started by: koti_rama
5 Replies

3. UNIX for Dummies Questions & Answers

Insert a break page after certain string using SED

Hi: I have 2 files: teststring.txt and a tempfile.txt teststring file contains: s/Primary Ins./\n1/g I'm trying to search for "Primary Ins." string in tempfile. For every "Primary Ins." string that is found, a new line is inserted and put in number 1. Then, write out the newfile... (7 Replies)
Discussion started by: newbeee
7 Replies

4. Shell Programming and Scripting

Page Break in large file

Hi, The shell script inserting the millions of rows into target flat file system and handling the line number for each line. We need a page break line after every 10,000 lines. is there any command to insert a page break line into target file. (3 Replies)
Discussion started by: koti_rama
3 Replies

5. Shell Programming and Scripting

sed with line break

<td> CIS </td>and I tried to sed 's/<td>\/nCIS\/n<\/td>/<td><\/td>' and sed 's/<td>\/rCIS\/r<\/td>/<td><\/td>' , but no joy. This is an html page that I need to clean. (4 Replies)
Discussion started by: dba_frog
4 Replies

6. Shell Programming and Scripting

sed: break before word if it's not last on the line

I've been trying this, and can't get it right. I want to put a line break before a word, but only if it's *not* the last word in the line. So if the break work was "fish," then... We want to fish tomorrow ...would become... We want to fish tomorrow ...but this line would remain... (3 Replies)
Discussion started by: estebandido
3 Replies

7. Shell Programming and Scripting

Break one line to many lines using awk

Break one line to many lines using awk The below code works but i want to implement without combining field 2 and 3 and then splitting i would like to do this in one command instead of writing multiple commands and creating multiple lines. nawk -F"|" '{print $1,$2SUBSEP$3}' OFS="|" file >... (16 Replies)
Discussion started by: pinnacle
16 Replies

8. Shell Programming and Scripting

help on page break

Hi, i have a file say samp.s which has 123 a b c d 123 e f g h 123 i j k l 123 m n o p 234 a b c d 234 e f g h 234 i j k l the first 3 characters in each line are considered the key values i have one more file temp.txt which has 123 234 i want to have a page break in... (5 Replies)
Discussion started by: Sheema
5 Replies

9. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies

10. Shell Programming and Scripting

Sed Help (Using expression - line break)

*Note, I thought I was in Shell Programming and Scripting Q&A and posted in a wrong forum. To avoid confusion and to inform people that I'm not trying to "spam" the forums, I'm adding this note up top* Hi Everyone thanks in advance for any input you can provide on the following question! I'm... (2 Replies)
Discussion started by: Janus
2 Replies
Login or Register to Ask a Question