File splitting according to the length of the fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File splitting according to the length of the fields
# 8  
Old 06-10-2015
Ohhh - does your info file look EXACTLY as posted in #1?

---------- Post updated at 13:52 ---------- Previous update was at 13:50 ----------

@Akshay Hegde: looks like the last field is missing ?
# 9  
Old 06-10-2015
Quote:
Originally Posted by RudiC
Ohhh - does your info file look EXACTLY as posted in #1?

---------- Post updated at 13:52 ---------- Previous update was at 13:50 ----------

@Akshay Hegde: looks like the last field is missing ?
I didn't get you, please explain, looks fine to me

@RudiC from your recent code I get like this
Code:
[akshay@localhost tmp]$ cat description
Field Name	Length	Start Position
ADJ-TYPE-CODE	1	1
ALLOW-AMT	11	2
CAP-SRVC-NO	1	13
BILL-AMT	11	14

[akshay@localhost tmp]$ cat datafile 
R-0000017611N-00000350001095ANZU01
A00000017611N000000350001095ANZU02
R-0000019427N-00000265001202BGYI03
R-0000005977N-00000092001202BGYI03
R-0000017195N-00000995001353B1IZ03
A00000099500N000000995001353B1IZ04
R-0000258547N-00002266002019AXAJ01
A00000258547N000002266002019AXAJ02
R-0000012277N-00000216002026BLCF03
A00000012277N000000216002026BLCF04


[akshay@localhost tmp]$ awk '                                             
NR < 3          {next}
NR == FNR       {HD=HD DL $1; DL=","
                 L[++n]=$2
                 S[n]  =$3
                 next}
FNR == 1        {print HD
                 next
                }
                {for (i=1; i<=n; i++) printf "%s,", substr($0, S[i], L[i])
                 printf "%s\n", substr ($0,S[n]+L[n])
                }
' description datafile
ALLOW-AMT,CAP-SRVC-NO,BILL-AMT
00000017611,N,00000035000,1095ANZU02  --- 4 fields                                     
-0000019427,N,-0000026500,1202BGYI03
-0000005977,N,-0000009200,1202BGYI03
-0000017195,N,-0000099500,1353B1IZ03
00000099500,N,00000099500,1353B1IZ04
-0000258547,N,-0000226600,2019AXAJ01
00000258547,N,00000226600,2019AXAJ02
-0000012277,N,-0000021600,2026BLCF03
00000012277,N,00000021600,2026BLCF04


Last edited by Akshay Hegde; 06-10-2015 at 09:02 AM..
# 10  
Old 06-10-2015
Not sure what the requestor really needs - the entire line with commas at the positions specified, like
Code:
A,00000012277,N,00000021600,2026BLCF04

, or (just) the four fields exactly as specified in the info file, like (Akshay Hegde's)
Code:
A,00000012277,N,00000021600

In Akshay Hegde's post#9 example, the first field is missing as his info file doesn't have the empty second line that the requestor's sample had in post#1.

Last edited by RudiC; 06-24-2015 at 11:37 AM..
# 11  
Old 06-24-2015
Syntax error

Hi,
I get the following error when I tried to use Akshay's code. Is it related to the unix flavor I use ? I am working on HP UX

Code:
 syntax error The source line is 20.
 The error context is
                        print >>>  FNR== <<<
 awk: The statement cannot be correctly parsed.
 The source line is 20.

---------- Post updated at 03:30 PM ---------- Previous update was at 03:28 PM ----------

@Rudic, you are correct. I need the format as below.

Code:
A,00000012277,N,00000021600,2026BLCF04

# 12  
Old 06-24-2015
Try parenthesizing the entire conditional assignment.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

Splitting multiple fields of /usr/bin/id

Hi, Iv got the following input $id |grep uid uid=6090(dsiddiq) gid=1(staff) groups=4001(cdgrp) and Im using the below command to split the field to grab the numberical userid as well the alphabetical userid $id|awk -F'=' '{print $2}'|awk -F')' '{print $1}'|awk -F'(' '{print $1" "$2}'... (4 Replies)
Discussion started by: dsid
4 Replies

3. Shell Programming and Scripting

Splitting a filed into multiple fields using awk

Hi, I have a tab delimited file as below: AWA Divi DD01 None 1 2 Room AC 01-MAY-15 31-OCT-15 OT 01-MAY-15 31-OCT-15 CF 01-MAY-15 31-OCT-15 AW0 Beach DD02 None 1 2 Double AC 01-MAY-15 31-OCT-15 AD 01-MAY-15 31-OCT-15 The number of columns(fields) after 7th field is not fixed and... (3 Replies)
Discussion started by: Bobby_2000
3 Replies

4. Shell Programming and Scripting

Help in splitting Sub Fields and compare with other field

Hi All, We are trying to pull out data from below table, the table contains four fields and out of which last two fields are having sub-fields with delimiter $, we want to identify number "1" position in the 3rd field and from 4th field need to extract the information from the same position. ... (4 Replies)
Discussion started by: rramkrishnas
4 Replies

5. Shell Programming and Scripting

Splitting a column in two separate fields

for making a summary I have a CSV file which is transformed to .DAT. I have an AWK file which is supposing to do the mapping of the DAT file. The code from the AWK file is the one below. The content of the DAT file looks like this (tab separated): ODT AGE CDT CO SEX TIME VALUE COMMENT ... (1 Reply)
Discussion started by: grikoss
1 Replies

6. Shell Programming and Scripting

Splitting fixed length file using awk

Hi, I need to split a fixed length file of 160 characters based on value of a column. Example: ABC 456780001 DGDG SDFSF BCD 444440002 SSSS TTTTT ABC 777750003 HHHH UUUUU THH 888880001 FFFF LLLLLL HHH 999990002 GGGG OOOOO I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies

7. Shell Programming and Scripting

Help with splitting fields

Hi. I want to put the first field to the end and the lines are of different number of fields. How should I do this with awk? Thanks. (3 Replies)
Discussion started by: dustinwang2003
3 Replies

8. Shell Programming and Scripting

Fixed length fields

HPUX and posix shell Hi all. I have a record with fixed length fields....I would like to reorder the fields and preserver the fixed lengths.... cat test 4 960025460 Dept of Music 8 960025248 Dept of Music 12-08 cat... (3 Replies)
Discussion started by: lyoncc
3 Replies

9. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

10. Shell Programming and Scripting

Need help in splitting a line into fields in shell scripting

I have a line of more than 3000 bytes which will contain & as fields separator..I am using following awk command ..Its working but its not accepting the line more than 3000 bytes...Anyother alternate solution even in othe shell command also fine... awk -F '&' '{for( i=1; i<=NF; i++ ) print $i}'... (2 Replies)
Discussion started by: punithavel
2 Replies
Login or Register to Ask a Question