![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multi-line output to single line | LinuxRacr | Shell Programming and Scripting | 7 | 02-26-2008 10:05 AM |
| Multi File processing | orahi001 | Shell Programming and Scripting | 2 | 01-10-2008 04:49 PM |
| AWK Multi-Line Records Numbering Problem | RacerX | Shell Programming and Scripting | 3 | 11-01-2007 10:44 AM |
| Making multi line output appear on one line | djsal | Shell Programming and Scripting | 1 | 10-07-2004 07:21 PM |
| Multi line deletes | rehoboth | UNIX for Dummies Questions & Answers | 5 | 10-18-2002 04:39 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Not sure which lines you are wanting to print, but there is a trick with awk. You can reset the value of NR anytime you want.
So a program like this awk 'NR > 2 && (NR % 2 == 0 ){ print} /^$/{NR=0}' <textfile> # /^$/ represent the blank line got me output that looked like this: FEB 20-98 VLY 1041 2094 8 8 8 8 8 8 NB SMITH GW : - : - :LAPS : 8 : 40 : - : - : 1 JAN 7-98 VLY 1030 2064 6 3 3 4 4 3 2071 NB SMITH GW : - : - :LAPS : 6 : 40 : - : - : 1 MAR 9-98 VLY $500 1024 2060 5 2 3 4 2 2 2063 1900 SMITH GW : - : - :LAPS : 8 : 36 : - : - : 1 MAR 13-98 VLY $1,350 1004 2022 2 3 2 2 1 1 2022 1130 SARAMA GW : - : - :LAPS : 7 : 31 : - : - : 2 MAR 27-98 VLY $675 1013 2020 4 1 1 1 1 2 2020 *1750 SMITH GW : - : - :LAPS : 8 : 60 : - : - : 2 MAY 1-98 VLY $1,500 0594 1594 2 1 1 1 1 1 1594 *9500 SMITH GW : - : - :LAPS : 7 : 70 : - : - : 9 MAY 9-98 HILL $2,000 0581 1570 1 6 5 4 2 1 1570 *2200 SMITH GW : - : - :LAPS : 7 : 60 : - : - : 8 MAY 15-98 HILL 1003 2011 7 8 8 8 8 8 *265 SMITH GW : - : - :LAPS : 8 : 75 : - : - : 9 MAY 21-98 HILL 0593 2010 7 8 8 8 8 8 9550 SMITH GW : - : - :LAPS : 9 : 46 : - : - : 2 FEB 22-98 VLY $675 1013 2020 4 1 1 1 1 2 2020 *1750 SMITH GW : - : - :LAPS : 8 : 60 : - : - : 3 APR 3-98 VLY $675 1001 2011 3 2 2 3 3 2 2011 *1550 SMITH GW : - : - :LAPS : 6 : 45 : - : - : 5 MAR 8-99 VLY $500 1024 2060 5 2 3 4 2 2 2063 1900 SMITH GW : - : - :LAPS : 8 : 36 : - : - : 10 MAR 13-99 VLY $1,350 1004 2022 2 3 2 2 1 1 2022 1130 SMITH GW : - : - :LAPS : 7 : 31 : - : - : 7 AUG 25-98 RIDC 1011 2011 9 4 3< 3< 7 7 2024 820 MILLER TF : - : - :MILE : 9 : 69 : - : - : 6 |
|
|||||
|
Thanks for the reply! Your solution got exactly the lines i wanted to pick out!
I believe i should be able to solve the rest of my problem on my own using some type of regex for "(CONT)" on the first line and an if-else statement. If i can't figure it out, i'll be back with another question ![]() Thanks again for your help, that trick with resetting the NR is a good one to know as i was clueless and kept fiddling with the settings for the FS and RS which was getting me no-where fast. Your solution is simply elegant. |
|
|||||
|
Is there any way to get the info lined up in columns using printf? I've tried a few things but it never seems to come out right; maybe the data is just too funky to get it to line-up?
So, given INPUT like: FEB 20-98 VLY 1041 2094 8 8 8 8 8 8 NB SMITH GW : - : - :LAPS : 8 : 40 : - : - : 1 JAN 7-98 VLY 1030 2064 6 3 3 4 4 3 2071 NB SMITH GW : - : - :LAPS : 6 : 40 : - : - : 1 MAR 9-98 VLY $500 1024 2060 5 2 3 4 2 2 2063 1900 SMITH GW : - : - :LAPS : 8 : 36 : - : - : 1 MAR 13-98 VLY $1,350 1004 2022 2 3 2 2 1 1 2022 1130 SARAMA GW : - : - :LAPS : 7 : 31 : - : - : 2 Can i get OUTPUT like: Code:
FEB 20-98 VLY 1041 2094 8 8 8 8 8 8 NB SMITH GW : - : - :LAPS : 8 : 40 : - : - : 1 JAN 7-98 VLY 1030 2064 6 3 3 4 4 3 2071 NB SMITH GW : - : - :LAPS : 6 : 40 : - : - : 1 MAR 9-98 VLY $500 1024 2060 5 2 3 4 2 2 2063 1900 SMITH GW : - : - :LAPS : 8 : 36 : - : - : 1 MAR 13-98 VLY $1,350 1004 2022 2 3 2 2 1 1 2022 1130 SARAMA GW : - : - :LAPS : 7 : 31 : - : - : 2 MAR 27-98 VLY $675 1013 2020 4 1 1 1 1 2 2020 *1750 SMITH GW : - : - :LAPS : 8 : 60 : - : - : 2 |
|
||||
|
Instead of a print, use a printf command. It allows you to specifiy a mask, then the data to print. for example
printf("%-30s", "MY NAME"); will right justify the value in the column. If you are a C programmer, it follows that printing convention. I suggest looking up the online (free and in pdf) version of "Effective awk programming" by Arnold Robbins for more information. |
|
|||||
|
I've been reading about, and testing printf options for a while now, and am stuck on how to handle the above situation where one of the fields in a column is blank-whitespace. I tried using printf in the above code, specifically the following line using the first six fields only (i want to format the rest of the fields too, but for testing purposes only tried the first six to show my problem):
Code:
NR > 2 && (NR % 2 == 0 ) {printf "%-5s%-8s: %-10s: %-15s: %-10s: %-5s:\n",$1,$2,$3,$4,$5,$6} /^$/{NR=0}
I GET RETURNED:
FEB 20-98 : VLY : 1041 : 2094 : 8 :
JAN 7-98 : VLY : 1030 : 2064 : 6 :
MAR 9-98 : VLY : $500 : 1024 : 2060 :
MAR 13-98 : VLY : $1,350 : 1004 : 2022 :
MAR 27-98 : VLY : $675 : 1013 : 2020 :
MAY 1-98 : VLY : $1,500 : 0594 : 1594 :
MAY 9-98 : HILL : $2,000 : 0581 : 1570 :
MAY 15-98 : HILL : 1003 : 2011 : 7 :
MAY 21-98 : HILL : 0593 : 2010 : 7 :
FEB 22-98 : VLY : $675 : 1013 : 2020 :
APR 3-98 : VLY : $675 : 1001 : 2011 :
MAR 8-99 : VLY : $500 : 1024 : 2060 :
MAR 13-99 : VLY : $1,350 : 1004 : 2022 :
: : : : :
AUG 25-98 : RIDC : 1011 : 2011 : 9 :
It should be: Code:
FEB 20-98 : VLY : : 1041 : 2094 : 8 : JAN 7-98 : VLY : : 1030 : 2064 : 6 : MAR 9-98 : VLY : $500 : 1024 : 2060 : MAR 13-98 : VLY : $1,350 : 1004 : 2022 : MAR 27-98 : VLY : $675 : 1013 : 2020 : MAY 1-98 : VLY : $1,500 : 0594 : 1594 : MAY 9-98 : HILL : $2,000 : 0581 : 1570 : MAY 15-98 : HILL : : 1003 : 2011 : 7 : MAY 21-98 : HILL : : 0593 : 2010 : 7 : FEB 22-98 : VLY : $675 : 1013 : 2020 : APR 3-98 : VLY : $675 : 1001 : 2011 : MAR 8-99 : VLY : $500 : 1024 : 2060 : MAR 13-99 : VLY : $1,350 : 1004 : 2022 : AUG 25-98 : RIDC : : 1011 : 2011 : 9 : |
|
||||
|
Yeah, but it is going to get complicated (believe it or not, this has been pretty straightforward).
the problem comes up from awk not being able to recognize a whitespace column. If you used tab separators, you could have a -F parameter for the tabs, but if it is simply spaces, you have to make an programmatic decision. for instance, it looks like if column 3 is a $ amount - if that is always true, you can check to see if it has a $, and print in the right column, or, if not, then you know everything has slid down one. So you could have to printf statements if ($3 ~ /\$/ ) { print style 1 } else { print style 2 } As much as I hate to admit it, I would have to try some trial and error to make sure the search for the $ works, since that is and End_of_line indicator and I was thinking that escaping it was the right idea. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|