Need help to format one txt file to required format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to format one txt file to required format
# 1  
Old 01-10-2014
Need help to format one txt file to required format

Hello Everyone,

I have one source file which is genarated by SAP in different format(Which I've never seen). I need to convert that file to required format and I need to read this target file from Datastage to use this in my Jobs. So I do not have any other options except to use Unix script to modify this file. I am not that expert in Unix so I would like to take Ideas from all of you, it may help us to complete this task.

I am attaching my source file to this thread and below is my required output format.


Code:
 
TCode|Sales deal|Date|User|Cond|Time|NewVal|Validity period
VK11||10-31-2013|ET8IVND|ZDEC|06:47:51|4.529- %|10-31-2013-12-31-9999
VK11||10-31-2013|ET8IVND|ZDEC|06:47:51|4.527- %|10-31-2013-12-31-9999
VK11||10-31-2013|ET8IVND|ZDEC|06:47:51|4.527- %|10-31-2013-12-31-9999
VK11||10-31-2013|ET8IVND|ZDEC|06:47:51|4.528- %|10-31-2013-12-31-9999
VK11||10-31-2013|ET8IVND|ZDEC|08:28:35|0.200- %|11-01-2013-12-31-9999
VK11||10-31-2013|ET8IVND|ZDEC|08:28:35||11-01-2013-12-31-9999

Can anyone please let me know is it possible to do this through Unix or not?

Thanks in Advance!!
# 2  
Old 01-10-2014
you can try something like this to start with: awk -f pra.awk ConditionChanges.txt
pra.awk:
Code:
BEGIN {
  FS=OFS="|"
  tab=sprintf("\t")
}
function trim(str)
{
    sub("^([ ]*|" tab "*)", "", str);
    sub("([ ]*|" tab "*)" "$", "", str);
    return str;
}

NF && $2 ~ / *-$/ {
     date=$3
     user=$4
     cond=$5
     validity=$(NF-1)
}
NF && $2~/^ *$/ && $3~/^ *$/ {
     if ($4 ~ /[0-9:]/) {
         time=$4
         tcode=$5
         salesDeal=$9
     }
     else if ($(NF-1) ~ "%")
        print trim(tcode), trim(salesDeal), trim(date), trim(user), trim(cond), trim(time), trim($(NF-1)), trim(validity)
}


Last edited by vgersh99; 01-10-2014 at 04:59 PM.. Reason: handle empty records
# 3  
Old 01-10-2014
Quote:
Originally Posted by vgersh99
you can try something like this to start with: awk -f pra.awk ConditionChanges.txt
pra.awk:
Code:
BEGIN {
  FS=OFS="|"
  tab=sprintf("\t")
}
function trim(str)
{
    sub("^([ ]*|" tab "*)", "", str);
    sub("([ ]*|" tab "*)" "$", "", str);
    return str;
}
 
$2 ~ / *-$/ {
     date=$3
     user=$4
     cond=$5
     validity=$(NF-1)
}
$2~/^ *$/ && $3~/^ *$/ {
     if ($4 ~ /[0-9:]/) {
         time=$4
         tcode=$5
         salesDeal=$9
     }
     else if ($(NF-1) ~ "%")
        print trim(tcode), trim(salesDeal), trim(date), trim(user), trim(cond), trim(time), trim($(NF-1)), trim(validity)
}

Thanks for the quick response vgersh99!! As I am starter in Unix scripting, I think it may take lot time to complete the testing for the code which you given. But I will definetly try your code to get the results.

Thanks Again!!
# 4  
Old 01-10-2014
I've updated the code with one more condition - take the latest version.
# 5  
Old 01-10-2014
Quote:
Originally Posted by vgersh99
I've updated the code with one more condition - take the latest version.
Okay. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in creating a file in required format form another existing file

I have a text file with contents like this: a,b,c, d~e,f,g,h~i,j ,k,l,m~n,o,p,q~ I need to convert this file into this format unix shell script commands: a,b,c,d~ e,f,g,h~ i,j,k,l,m~ n,o,p,q~ as you may have noticed, I need to retain the ~ signs at the end. Any help is greatly... (3 Replies)
Discussion started by: harsha1238
3 Replies

2. UNIX for Dummies Questions & Answers

help required in converting a file format

My file format: -------------------------------------------------- Complete Consistency Check Valid Area : VALID:VALID Started by : esanwad Started at : Thu Dec 11 16:04:46 2014 CNA version : R21H04_EC08 Check range : AREA VALID/VALID ... (4 Replies)
Discussion started by: Gautam Banerjee
4 Replies

3. UNIX for Dummies Questions & Answers

Format txt file in UNIX

Hello All, I have File_all.txt which have content as below Volume in drive E is Data Volume Serial Number is 586D-6932 Directory of E:\mydi2\siudfor\TFG058 03/27/2014 09:59 PM 5,569 FX\FX01 my4_pg4_MON_20140327_C.zip 1 File(s) 5,569 bytes ... (5 Replies)
Discussion started by: kumar30213
5 Replies

4. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

5. Shell Programming and Scripting

Format txt file as html table

I have a short time to solve a problem, so I need some help. I've searched the forum, but I couldn't find a solution to my problem. I made a script to filter some text and now I have a new requirement to make it available as html table. Problem is that I more than one files with different set... (2 Replies)
Discussion started by: tetreb
2 Replies

6. Shell Programming and Scripting

Format txt file to CSV

Hi All, I have a file with content FLIGHT PLANS PRODUCED ON 26.08.2008(SORTED BY FPLAN NUMBER) RUN DATED 27/08/08 PAGE 1 -------------------------------------------------------------- FPLAN FPLAN PRE BTCH BATCH POST BTCH BATCH BATCH ... (1 Reply)
Discussion started by: digitalrg
1 Replies

7. UNIX for Dummies Questions & Answers

Save Excel file as .txt in UNIX format

I have some files created in Excel that have to be saved as .txt files in order to load them into our accounting system. I can save the files as .txt files through Excel, but I then have to open them in TextPad and do a save as to change the Format from PC to UNIX. Is there a way to skip this step... (2 Replies)
Discussion started by: jroyalty
2 Replies

8. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

9. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like öƒ.ƒ.„İİ¡Š·œƒ.„İİ¡Š· ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies

10. Shell Programming and Scripting

How to format a txt file using typeset?

Hi all, i have a file toto.txt : blabla;toumtoudoum;blablabla toumtoudoumtoudoum;blabla;popopopo blablabla;etcetcetc;blabla etc.... As you can see, it is a simple file containig 3 fields separated by a ";". What i want to do is to display this file with a nice formating... (4 Replies)
Discussion started by: HowardIsHigh
4 Replies
Login or Register to Ask a Question