Creating repeating record in between file through script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating repeating record in between file through script
# 1  
Old 03-23-2014
Wrench Creating repeating record in between file through script

apprecieate your help to resove this.

My source file looke like
Code:
1001 000 HEADER  
1001 001 RAJESH
1001 002    100 
1001 002    200
1001 002    500
1001    006 FOOTER
1002 000 HEADER  
1002 001 RAMESH
1002 002    100 
1002 002    200
1002 002    500
1002    006 FOOTER

my tartet file output should be in below way.

Here single set of record would be considered from Header record to footer record . In source, 001(second column) will start after header record for each set. It presesnt only one time source record in each set. But as per my requirement, I need '001' record has to come every before '002'. Please help to resolve this. apprecieate your help.

I need unix shell script to make it in below way.
Code:
1001 000 HEADER  
1001 001 RAJESH
1001 002    100
1001 001 RAJESH 
1001 002    200
1001 001 RAJESH
1001 002    500
1001    006 FOOTER
1002 000 HEADER  
1002 001 RAMESH
1002 002    100
1002 001 RAMESH 
1002 002    200
1002 001 RAMESH
1002 002    500
1002    006 FOOTER


Last edited by Scott; 03-23-2014 at 03:03 AM.. Reason: Use code tags, please...
# 2  
Old 03-23-2014
Hi guy,
try this and may it could work.
Code:
$ echo '1001 000 HEADER
1001 001 RAJESH
1001 002 100
1001 002 200
1001 002 500
1001 006 FOOTER
1002 000 HEADER
1002 001 RAMESH
1002 002 100
1002 002 200
1002 002 500
1002 006 FOOTER'|awk '/HEADER/{s=1;print;next}s{r=$0;s=0;t=1;print;next}!/FOOTER/&&!t{print r}{t=0;print}'
1001 000 HEADER
1001 001 RAJESH
1001 002 100
1001 001 RAJESH
1001 002 200
1001 001 RAJESH
1001 002 500
1001 006 FOOTER
1002 000 HEADER
1002 001 RAMESH
1002 002 100
1002 001 RAMESH
1002 002 200
1002 001 RAMESH
1002 002 500
1002 006 FOOTER

cheer up.

Lucas
This User Gave Thanks to Lucas_0418 For This Post:
# 3  
Old 03-23-2014
Thanks for you reply

I have tried modifying as below. Iam getting error. is that correct??
Code:
<filename>'|awk '/001/{s=1;print;next}s{r=$0;s=0;t=1;print;next}!/006/&&!t{print r}{t=0;print}' > <targetfile>

any chagnes needed??

Last edited by Scott; 03-23-2014 at 03:15 AM.. Reason: Code tags, please...
# 4  
Old 03-23-2014
Hi guy,

I used ' echo something | ' to be stdin of the awk in my reply, and if you want to use a file name, don't use pipeline | like that, otherwise you will get an error.

And I saw you modify the HEADER and FOOTER in my code, may you want to locate them with the number in the second field instead of the string in the 3rd.

you'd better do it like this:
Code:
awk '$2~/000/{s=1;print;next}s{r=$0;s=0;t=1;print;next}$2!~/006/&&!t{print r}{t=0;print}'  filename > targetfile

# 5  
Old 03-23-2014
Hi ,

Iam getting below error. can you please on this.
Code:
awk: syntax error near line 1
awk: bailing out near line 1

Thanks
ganesh.

Last edited by Scott; 03-23-2014 at 07:19 AM.. Reason: Code tags
# 6  
Old 03-23-2014
Hi ,
The codes run successfully in my cygwin environment, and I am sorry that I can't get any more test environment in weekend.
What's your environment , OS ? awk version ?
And I just try to modify my code to make it more compatible.
Code:
awk '{if($2~/000/){s=1;print $0;next}}
{if(s){r=$0;t=1;s=0;print $0;next}}
{if($2!~/006/&&t==0){print r}}
{t=0;print $0}' file

This User Gave Thanks to Lucas_0418 For This Post:
# 7  
Old 03-23-2014
Very nice. i can able to do. it is working fine. Thanks a lot....
This User Gave Thanks to Ganesh L For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. Shell Programming and Scripting

Deleting Repeating lines from a txt file via script

Hi, I'm having trouble in achieving the following scenario. There is a txt file with thousands of lines and few lines are repeated, which needs to be removed using a script. File.txt 20140522121432,0,12,ram Loc=India From=ram@xxx.com, To=ravi@yyy.com,, 1 2 3 4 . . 30... (18 Replies)
Discussion started by: Gautham
18 Replies

3. Shell Programming and Scripting

Help creating a timestamp script to record mem usage

Hi, I'm looking into doing a few performance tweaks by adjusting my max memory on a few lpars. I would to create a time stamp script so i could review it for a week and determine how much space i can lower my max memory to so i could reclaim and allocate that memory to where it is needed the... (2 Replies)
Discussion started by: vpundit
2 Replies

4. Shell Programming and Scripting

repeating ftp script question

so i got this request to do this: - Script should check for the file ever 15 minutes on the FTP server…if the file is not found, then the whole script exits. File will only be created one a week at random. i have gotten this far, but am kind of stuck, also sleep command doesnt work... (3 Replies)
Discussion started by: zapatur23
3 Replies

5. Shell Programming and Scripting

Reading a file (one record) in a SHL script

I am trying to read a file in a shl script (only one record) and stored in a variable file_number I got the following read -u $BANNER_HOME/xxxxxxx/misc/EFTSQL.dat file_number file_number2 = $file_number + 1 echo $file_number2 > $BANNER_HOME/xxxxxx/misc/EFTSQL.dat EOF It is not working... (2 Replies)
Discussion started by: rechever
2 Replies

6. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

7. UNIX for Dummies Questions & Answers

Creating new file with new record length

Hi there, using the command below > cat file1 | tr "\001" " " > tmp2 how can I alter the record length of tmp2 to fix length of 350 bytes ? Thanks a lot! (7 Replies)
Discussion started by: mrjunsy
7 Replies

8. Shell Programming and Scripting

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (2 Replies)
Discussion started by: shilendrajadon
2 Replies

9. UNIX for Advanced & Expert Users

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (1 Reply)
Discussion started by: shilendrajadon
1 Replies

10. Shell Programming and Scripting

Repeating commands in a script

I have written a script that I want to be repeated. The script that I wrote outputs the date, how many people are on the system, how many people logged in before me, and how many people logged in after me. Than what I want it to do is after it outputs the 4 lines I want it to go back to the... (4 Replies)
Discussion started by: Dave2874
4 Replies
Login or Register to Ask a Question