Splitting the file based on two fields - Fixed length file


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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:
Code:
AA0998703000000000000190510095350019500010005101980301      
K 0998703000000000000190510095351019500020005101480         
CC0338703368396368396190510114449019600010005101980301      
L 03387033683963683961905101144500196000200051012803        
I O553203000000000000190510120433019700010005101980301

Split based on 4th position to 4 char (9987) and 21st to 6 char(190510)

So, in the above example there will be 3 files generated.
Code:
9987_190510.txt
3387_190510.txt
5532_190510.txt

I tried with the below command,
Code:
awk '{ F=substr($0,4,4)".txt"; print $0 >> F; close(F) }' filename

But it splits only on the first set, I need to do it with the combination of both sub strings.


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it easier to read and preserves white space which is important for indenting or fixed-width data.

Last edited by rbatte1; 1 Week Ago at 12:49 PM..
# 2  
Hi, try so
Code:
awk '
/^.{3}9987.{14}190510/  {print >"file1"}
/^.{3}3387.{14}190510/  {print >"file2"}
/^.{3}5532.{14}190510/  {print >"file3"}
' file

# 3  
Code:
awk '{ F=substr($0,4,4) "_" substr($0,21,6) ".txt"; print $0 >> F; close(F) }' filename

This User Gave Thanks to vgersh99 For This Post:
# 4  
Code:
awk '{print > gensub(/^.{3}(.{4}).{14}(.{6}).*/, "\\1_\\2", 1)".txt"}'

This User Gave Thanks to nezabudka For This Post:
# 5  
With bash builtins:
Code:
#!/bin/bash
outfile="" prefile=""
while IFS= read line
do
  outfile="${line:3:4}_${line:21:6}.txt"
  if [ "$outfile" != "$prefile" ]
  then
    exec >>"$outfile"
    prefile=$outfile
  fi
  echo "$line"
done < filename

It tries to reduce the number of open() calls.
The same is achieved in awk by minimizing the close()
Code:
awk '{ F=substr($0,4,4) "_" substr($0,22,6) ".txt"; print $0 >> F } F!=PF { close(PF); PF=F }' filename

If there are not too many output files, do not close (and re-open) them at all:
Code:
awk '{ F=substr($0,4,4) "_" substr($0,22,6) ".txt"; print $0 > F }' filename

It allows overwriting the files because there is only one open() per file (awk does it automatically at the first write).
These 3 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
File splitting according to the length of the fields
nua7
Hi All, I have two files: 1> Data file 2> info file which has field lengths and start position. Is there a way to create a comma delimited file according to the fields length and start position. Data file : R-0000017611N-00000350001095ANZU01 A00000017611N000000350001095ANZU02...... Shell Programming and Scripting
11
Shell Programming and Scripting
Splitting fixed length file using awk
Neelkanth
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...... Shell Programming and Scripting
7
Shell Programming and Scripting
Need awk script to compare 2 fields in fixed length file.
Muga801
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...... Shell Programming and Scripting
1
Shell Programming and Scripting
Convert a tab delimited/variable length file to fixed length file
Everton_Silveir
Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected...... UNIX for Dummies Questions & Answers
2
UNIX for Dummies Questions & Answers
What the command to find out the record length of a fixed length file?
tranq01
I want to find out the record length of a fixed length file? I forgot the command. Any body know?... UNIX for Dummies Questions & Answers
9
UNIX for Dummies Questions & Answers