convert/format a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert/format a file
# 8  
Old 09-12-2007
If on solaris use nawk...
Code:
awk 'BEGIN {
        FS = ","
        OFS = "|"
        c = "12,8,5,6,13,5"
        n = split(c,w)
     }
     {
        for(i=1; i<=n; i++) {
            printf "%-" w[i] "." w[i] "s" , $i
            printf (i==n ? ORS : OFS)
        }
     }
     ' file1 > file2

Input and output files...
Code:
==> file1 <==
20045667886,tamrindqiww,user,india,address1234,love
20045667886,tamrind,user,india,address45,love
20045667886,tamrind,user,india,address333333,love

==> file2 <==
20045667886 |tamrindq|user |india |address1234  |love
20045667886 |tamrind |user |india |address45    |love
20045667886 |tamrind |user |india |address333333|love

Change the column widths to what you want and you can unset the output field separator by using OFS=""
# 9  
Old 09-13-2007
thank its cool nd serves the purpose

now there rises one more question ....

i just want to verify the field values if they are more than the max size

==> file1 <==
20045667886,tamrindqiww,user,india,address1234,love
20045667886,tamrind,user,india,address45,love
20045667886,tamrind,user,india,address333333,love

consider the same file
if max field size for tamrind is 7 . how can check it with the same.


nd if its more i need a message notifying the same field
# 10  
Old 09-14-2007
Try adding some extra code to produce a log file, e.g...
Code:
awk 'BEGIN {
        FS = ","
        OFS = "|"
        c = "12,8,5,6,13,5"
        n = split(c,w)
        errlog = "file3"
        printf "" > errlog
     }
     {
        for(i=1; i<=n; i++) {
            printf "%-" w[i] "." w[i] "s" , $i
            printf (i==n ? ORS : OFS)
            if (length($i)>w[i]) {
                printf "Row:%s Column:%s Value:%s Truncated:%" w[i] "." w[i] "s\n", \
                            NR, i, $i, $i > errlog
            }
        }
     }
     ' file1 > file2

Output...
Code:
==> file3 <==
Row:1 Column:2 Value:tamrindqiww Truncated:tamrindq

# 11  
Old 09-15-2007
continued

here i woulddd not like to truncate the data ... instead i would like to give a message that the particular field is not as expected (file validation)
# 12  
Old 09-17-2007
So what's the problem? Just change the example to do what you want.
# 13  
Old 09-28-2007
some thing coming new the requirements

consider a CSV file

20045667886,tamrind,user,india,address,love
20045667886,tamrind,abcd,india,address,love
20045667886,tamrind,user,india,address,love
20045667886,tamrind,user,india,address,love
20045667886,tamrind,user,india,address,love


this is a perfect file for me

now i get a file like

20045667886,tamrind,user,india,address,love
20045667886,tamrind,user,india,address,love
20045667886,tamrind,user,
20045667886,tamrind,user,india,address,love
20045667886,tamrind,user,india,address,love


in which the third record of the file has some fields missing ...

i just want to exit the process by checking this .
and if possible give the record number.


ie an output like 'the record with $record_no is incorrect'
# 14  
Old 09-30-2007
Try...
Code:
awk 'BEGIN {
        FS = ","
     }
     NF != 6 {
        printf "the record with %d is incorrect\n", NR
     }
     ' file1

the record with 3 is incorrect
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 Badly to convert file format!

Can anyone provide me a unix script to convert file formats. Input and desired output is attached. (10 Replies)
Discussion started by: Ravi S M
10 Replies

2. Shell Programming and Scripting

Convert the below file to csv format

Hi , i want to change this question, i will post soon.. (6 Replies)
Discussion started by: srikanth2567
6 Replies

3. Shell Programming and Scripting

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (2 Replies)
Discussion started by: Samtel
2 Replies

4. UNIX for Dummies Questions & Answers

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (1 Reply)
Discussion started by: Samtel
1 Replies

5. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

6. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Convert UTF8 Format file to ANSI format

:confused: Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on... (9 Replies)
Discussion started by: rajreddy
9 Replies

9. UNIX for Advanced & Expert Users

Convert UTF8 Format file to ANSI format

:) Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on this.........Let me... (1 Reply)
Discussion started by: rajreddy
1 Replies

10. Shell Programming and Scripting

How to convert file format ?

Hi guys, I have a text file like in following format, IMSI:1 MSISDN:44569098 Data1=prov Data2=yes Data3=not Data4=yes Data5=yes Data6=yes IMSI:2 MSISDN:44569099 Data1=yes Data2=not Data3=prov Data4=prov (3 Replies)
Discussion started by: maheshsri
3 Replies
Login or Register to Ask a Question