Splitting fixed length file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting fixed length file using awk
# 1  
Old 05-03-2013
Question 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:
Code:
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 position 12-15 i.e 0001,0002,0003 and copy the data to files 0001.txt,0002.txt,0003.txt.

so 0001.txt should contain
Code:
ABC   456780001 DGDG SDFSF
THH   888880001 FFFF  LLLLLL

002.txt should contain
Code:
BCD   444440002 SSSS TTTTT
HHH   999990002 GGGG OOOOO

003.txt should contain
Code:
ABC   777750003 HHHH UUUUU

and all the new files should have same fixed width as the original file.

Kindly help how can i achieve this.Smilie

Last edited by radoulov; 05-03-2013 at 09:15 AM..
This User Gave Thanks to Neelkanth For This Post:
# 2  
Old 05-03-2013
Code:
awk '{ F=substr($0,12,4) ".txt"; print $0 >> F; close(F) }' file

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 05-03-2013
Question Splitting fixed length file using awk

Thanks Yoda. It is working fine.

Can you also tell me how can i append the file name with a parameter. Something like 0001_RL_$1.txt. $1 being passed as an arguement.

or else if $1 can be picked up from file itself from position 60-64.
# 4  
Old 05-03-2013
Quote:
Originally Posted by Neelkanth
Something like 0001_RL_$1.txt. $1 being passed as an arguement.
Code:
awk -v A="$1" '{F=substr($0,12,4) "_RL_" A ".txt"; print $0 >> F; close(F) }' file

# 5  
Old 05-03-2013
superb..
thanks a lot..
# 6  
Old 05-04-2013
Quote:
Originally Posted by Neelkanth
. . .
or else if $1 can be picked up from file itself from position 60-64.
stealing Yoda's code:
Code:
awk '{F=substr($0,12,4) "_RL_" substr($0,60,5) ".txt"; print $0 >> F; close(F) }' file

# 7  
Old 05-06-2013
Hi,
I am facing one issue here. The command works fine if i hardcode the file name but if is pass it as an arguement it doesn't work. For e.g:Below commands works fine
Code:
awk -v A="$type" '{F=substr($0,23,8) "_LTD_" A ".txt"; print $0 >> F; close(F) }' RL004.txt

But the below command does not work.
Code:
awk -v A="$type" '{F=substr($0,23,8) "_LTD_" A ".txt"; print $0 >> F; close(F) }' $path/$filename

Can you please help with this.

Last edited by Franklin52; 05-07-2013 at 03:53 AM.. Reason: Code tags
This User Gave Thanks to Neelkanth For This Post:
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

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

3. Shell Programming and Scripting

awk to print fixed length columns to right side

Hi, I am in a situation to print the message on a column, where the each line starting position should be same. For example code: HOSTNAME1="1.2.3.4.5.6.7" TARGET_DIR="/tmp" echo "HOSTNAME1:" "$HOSTNAME1" | awk -v var="Everyone" '{len=55-length;printf("%s%*s\n",$0,len,var)}' echo... (4 Replies)
Discussion started by: tprabhaker
4 Replies

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

5. Shell Programming and Scripting

awk to extract incorrect fixed length records

I have a number of unix text files containing fixed-length records (normal unix linefeed terminator) where I need to find odd records which are an incorrect length. The data is not validated and records can contain odd backslash characters and control characters which makes them awkward to process... (2 Replies)
Discussion started by: methyl
2 Replies

6. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

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... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

7. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

8. Shell Programming and Scripting

Awk - Working with fixed length files

OK I am somewhat new to UNIX programming please see what you can do to help. I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1. I... (3 Replies)
Discussion started by: ambroze
3 Replies

9. Shell Programming and Scripting

Awk with fixed length files

Hi Unix Champs, I want to awk on a fixed length file. Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields. How do i do the same in case of fixed length file? Thanks in Advance. Regards. (7 Replies)
Discussion started by: c2b2
7 Replies

10. Shell Programming and Scripting

fixed length fields in awk

I am trying to display df -h command out in proper format, how can I display each field of each record in a fixed length. (2 Replies)
Discussion started by: roopla
2 Replies
Login or Register to Ask a Question