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

 
Thread Tools Search this Thread
# 1  
Old 12-28-2017
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 Smilie

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?
# 2  
Old 12-28-2017
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

This User Gave Thanks to Don Cragun For This Post:
Neo (01-04-2018)
# 3  
Old 12-28-2017
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?

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Removing Header and Trailer record of a EBCDIC file abhilashnair UNIX for Advanced & Expert Users 1 06-11-2015 08:30 AM
Prefix a variable in the first column of all the records of the files with and without header dhruuv369 Shell Programming and Scripting 7 10-27-2013 12:45 PM
Copy header values into records abercrom Shell Programming and Scripting 2 09-17-2013 05:48 PM
Generate a DML dynamically based off of header record anduzzi Shell Programming and Scripting 17 06-14-2013 01:35 PM
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
Printing columns with header Unilearn UNIX for Dummies Questions & Answers 1 08-05-2011 12:03 PM
pull date from header and append to all records keeferb UNIX for Dummies Questions & Answers 4 07-07-2011 09:20 AM
Insertion of Header record shreekrishnagd Shell Programming and Scripting 7 08-19-2008 07:28 AM
Skip parsing the header record - Awk anduzzi Shell Programming and Scripting 2 08-14-2008 12:45 PM
change order of fields in header record JohnMario UNIX for Dummies Questions & Answers 1 05-22-2008 03:58 PM
awk script to update header record klut Shell Programming and Scripting 5 04-16-2008 10:04 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
Need to Chop Header and Footer record from input file coolbudy Shell Programming and Scripting 4 08-09-2005 01:26 PM