Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Help in printing records where there is a 'header' in the first record ???

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by newbie_01
newbie_01's Unix or Linux Image
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 18 January 2018, 11:48 AM EST
Posts: 232
Thanks: 44
Thanked 1 Time in 1 Post
Help in printing records where there is a 'header' in the first record ???

Hi,

I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others

I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk

Below is what I have so far:

This is the content of the report so far, hopefully it doesn't change. I change the information to not show the real information but the format is more or less correct.



Code:
$: cat /tmp/x.txt
Some text here
Some header information here
Some header information here
------- ------------- ------- ------------- -------------------- --------- ----
xxx2ns666-pr-non-xxxTEST xx_tmp_abcq xxx2ns111-dr-01h-xxxTEST vol_abcp_zz xxx1ns999_vol_abcp_zz_yy.1 online RO
                         xx_tmp_abct xxx2ns111-dr-01h-xxxTEST vol_abcp_zz xxx1ns999_vol_abcp_zz_yy.16 online RO
                         xx_tmp_defd xxx2ns111-dr-01h-xxxTEST vol_defp_zz xxx1ns999_vol_defp_zz_yy.16 online RO
                         xx_tmp_ghiq xxx2ns111-dr-01h-xxxTEST vol_ghip_zz xxx1ns999_vol_ghip_zz_yy.16 online RO
                         xx_tmp_ghit xxx2ns111-dr-01h-xxxTEST vol_ghip_zz xxx1ns999_vol_ghip_zz_yy.17 online RO
5 entries were displayed.

Below is my long very newbie awk command Linux



Code:
cat /tmp/x.txt | grep xx_tmp | grep -v "^$" | awk 'NR==1 { print $2 " " $3 " " $4 " " $5 " " $6 " " $7 } NR>1 { print $1 " " $2 " " $3 " " $4 " " $5 " " $6 }'

xx_tmp_abcq xxx2ns111-dr-01h-xxxTEST vol_abcp_zz xxx1ns999_vol_abcp_zz_yy.1 online RO
xx_tmp_abct xxx2ns111-dr-01h-xxxTEST vol_abcp_zz xxx1ns999_vol_abcp_zz_yy.16 online RO
xx_tmp_defd xxx2ns111-dr-01h-xxxTEST vol_defp_zz xxx1ns999_vol_defp_zz_yy.16 online RO
xx_tmp_ghiq xxx2ns111-dr-01h-xxxTEST vol_ghip_zz xxx1ns999_vol_ghip_zz_yy.16 online RO
xx_tmp_ghit xxx2ns111-dr-01h-xxxTEST vol_ghip_zz xxx1ns999_vol_ghip_zz_yy.17 online RO

The grep -v "^$" is just a sanity thing. Is there a shorter way that does the same?
Sponsored Links
    #2  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by newbie_01
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 4:40 AM EST
Location: San Jose, CA, USA
Posts: 10,916
Thanks: 609
Thanked 3,818 Times in 3,262 Posts
The grep -v "^$" is NOT just a sanity thing. It is a waste of system resources that only serves to make your script run slower. Any line that is matched by the first grep in your pipeline can't possibly be an empty line that will be discarded by the second grep in your pipeline. The cat in your pipeline is an unnecessary use of cat. It, again, only serves to make your script run slower.

The following script produces the same output as your script. But, of course, it depends on your sample input being a realistic sample of the actual contents of /tmp/x.txt. If it isn't, all bets are off.



Code:
awk 'sub(/^.*xx_tmp/, "xx_tmp")' /tmp/x.txt

The Following User Says Thank You to Don Cragun For This Useful Post:
Neo (2 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by newbie_01
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 11:36 AM EST
Location: Aachen, Germany
Posts: 11,958
Thanks: 353
Thanked 3,687 Times in 3,385 Posts
That looks like a query result from a DB created with a special formatting. Any chance to modify the query's format in the first place?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Approach on Header record cmaroju Shell Programming and Scripting 0 07-11-2012 06:47 AM
Specific Header after every 30 records srk409 Shell Programming and Scripting 6 01-30-2012 11:03 AM
Insertion of Header record shreekrishnagd Shell Programming and Scripting 7 08-19-2008 07:28 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 01:15 PM
How to extract duplicate records with associated header record run_eim UNIX for Dummies Questions & Answers 17 01-16-2007 11:46 PM



All times are GMT -4. The time now is 05:10 PM.