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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help in printing records where there is a 'header' in the first record ???
# 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:
# 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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy header values into records

I'm using a shell script to manipulate a data file. I have a large file with two sets of data samples (tracking memory consumption) taken over a long period of time, so I have many samples. The problem is that all the data is in the same file so that each sample contains two sets of data.... (2 Replies)
Discussion started by: abercrom
2 Replies

2. Shell Programming and Scripting

Approach on Header record

All, I currently have a requirement to fetch a Date value from a table. And then insert a Header record into a file along with that date value. ex: echo "HDR"" "`date +%Y%j` `date +%Y%m%d` In the above example I used julian date and standard date using Current Date. But the requirement... (0 Replies)
Discussion started by: cmaroju
0 Replies

3. Shell Programming and Scripting

Specific Header after every 30 records

Hi All, I have got a requirement. I have a source file, EMPFULL.txt and I need to split the data for every 30 records and place a Typical Header as below with system and page number too. 2012.01.03 Employee Dept Report 1... (6 Replies)
Discussion started by: srk409
6 Replies

4. UNIX for Dummies Questions & Answers

pull date from header and append to all records

I did some searches, but couldn't really find what I'm looking for. I have a file formatted as below: BOF ABC CO - XYZ COMM DATA OF 07/05/2011 EBA00000001 sdfa rtyus uyml EBB00000001 54682 984w3 EBA00000002 mkiyuasdf 98234 I want to pull the date from the header record and add it... (4 Replies)
Discussion started by: keeferb
4 Replies

5. Shell Programming and Scripting

Insertion of Header record

A header record is to be inserted in the begining of a flat file without using extra file or new file. It should be inserted into same file. Advace thanks for all help... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

6. Shell Programming and Scripting

Skip parsing the header record - Awk

Guys.... Got a scenario in which I need to skip parsing the header record while I do an awk. Does awk has the flexibility to accomplish this?. If so, how do we do this?. Thanks !!! -Anduzzi :) (2 Replies)
Discussion started by: anduzzi
2 Replies

7. UNIX for Dummies Questions & Answers

change order of fields in header record

Hello, after 9 months of archiving 1000 files, now, i need to change the order of fields in the header record. some very large, space padded files. HEADERCAS05212008D0210DOMEST01(spacepadded to record length 210) must now be 05212008HEADERCASD0210DOMEST01(spacepadded to record length 210) ... (1 Reply)
Discussion started by: JohnMario
1 Replies

8. Shell Programming and Scripting

awk script to update header record

I am using HP UX and think this may be done with awk but bot sure. I have a file with a several header records and undeneath many detail records I need to put in the header record the number of detail records above this header record and number of detail records below this header record Header... (5 Replies)
Discussion started by: klut
5 Replies

9. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

10. UNIX for Dummies Questions & Answers

How to extract duplicate records with associated header record

All, I have a task to search through several hundred files and extract duplicate detail records and keep them grouped with their header record. If no duplicate detail record exists, don't pull the header. For example, an input file could look like this: input.txt HA D1 D2 D2 D3 D4 D4... (17 Replies)
Discussion started by: run_eim
17 Replies
Login or Register to Ask a Question