Sponsored Content
Top Forums Shell Programming and Scripting Add character based on record length Post 302368730 by durden_tyler on Thursday 5th of November 2009 12:40:13 PM
Old 11-05-2009
Quote:
Originally Posted by CutNPaste
All,

I can't seem to find exactly what I'm looking for, and haven't had any luck patching things together.

I need to look through a file, and if the record length is not 874, then add 'E' in position 778.

Your help is greatly appreciated.
Here's one way to do it with Perl:

Code:
perl -lne 'substr($_,777,1) = "E" if length != 874; print' filename

Here's a demonstration of this script with 8 in place of 874 and 6 in place of 778, i.e. for the problem "if the record length is not 8, then add 'E' in position 6":

Code:
$
$ cat f2
1234567
12345678
123456789
$
$ perl -lne 'substr($_,5,1) = "E" if length != 8; print' f2
12345E7
12345678
12345E789
$
$

HTH,
tyler_durden

* Not sure what your data file looks like, but here's a more robust variation of the script:

Code:
$
$ cat f2

1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
$
$ perl -lne 'substr($_,5,1) = "E" if length != 8; print' f2
substr outside of string at -e line 1, <> line 1.
$
$ # modified script
$ perl -lne 'substr($_,5,1) = "E" if length != 8 and length >= 6; print' f2

1
12
123
1234
12345
12345E
12345E7
12345678
12345E789
12345E7890
$
$


Last edited by durden_tyler; 11-05-2009 at 01:46 PM..
 

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

3. Shell Programming and Scripting

Add substring in a file containing fixed length record.

I am new to awk and writing a script using awk. I have file containing fixed length records, I wish to extract 2 substring(each substring is padded with zeros on left e.g 000000003623) and add each substring respectively for every record in the file to get total sum of respective substring for all... (5 Replies)
Discussion started by: Devesh5683
5 Replies

4. Shell Programming and Scripting

Record length

Hi, The record length may be differ in afile. I want to display the records if the record length is not equal to 50 using sed/awk command. Thanks in Advance (6 Replies)
Discussion started by: NareshN
6 Replies

5. Shell Programming and Scripting

Record length check fails due to '\' character

When I check the length for the records in the file, it does not give me the correct value. I used wc -l command. Example records: abcdefghij abcd\efghij abcdefghi Expected output is: 10 11 9 But the output returned is 10 10 9 Please help me on this issue. (10 Replies)
Discussion started by: Amrutha24
10 Replies

6. Shell Programming and Scripting

Delimit file based on character length using awk

Hi, I need help with one problem, I came across recently. I have one input file which I need to delimit based on character length. $ cat Input.txt 12345sda231453 asd760kjol62569 sdasw4g76gdf57 And, There is one comma separated file which mentions "start of the field" and "length... (6 Replies)
Discussion started by: Prathmesh
6 Replies

7. UNIX for Dummies Questions & Answers

Select lines based on character length

Hi, I've got a file like this: 22 22:35645163:T:<CN0>:0 0 35645163 T <CN0> 22 rs140738445:20902439:TTTTTTTG:T 0 20902439 T TTTTTTTG 22 rs149602065:40537763:TTTTTTG:T 0 40537763 T TTTTTTG 22 rs71670155:50538408:TTTTTTG:T 0 50538408 T TTTTTTG... (3 Replies)
Discussion started by: zajtat
3 Replies

8. Shell Programming and Scripting

Code snippet to cut XML files based on record length

I want to do FTP an Huge XML file to mainframe server using AIX server Since my file size is huge, i want to split the XML file based on a delimiter , the record delimiter should be set after every 27000 bytes of data and then do the ftp This is done becos the data send to the mainframe must... (1 Reply)
Discussion started by: vishwanath001
1 Replies

9. Shell Programming and Scripting

Add string based on character length

Good day, I am a newbie here and thanks for accepting me I have a task to modify input data where my input data looks like 123|34567|CHINE 1|23|INDIA 34512|21|USA 104|901|INDIASee that my input has two columns with different character length but max length is 5 and minimum length is 0 which... (1 Reply)
Discussion started by: fastlearner
1 Replies

10. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies
POE::Filter::Block(3pm) 				User Contributed Perl Documentation				   POE::Filter::Block(3pm)

NAME
POE::Filter::Block - translate data between streams and blocks SYNOPSIS
#!perl use warnings; use strict; use POE::Filter::Block; my $filter = POE::Filter::Block->new( BlockSize => 8 ); # Prints three lines: abcdefgh, ijklmnop, qrstuvwx. # Bytes "y" and "z" remain in the buffer and await completion of the # next 8-byte block. $filter->get_one_start([ "abcdefghijklmnopqrstuvwxyz" ]); while(1) { my $block = $filter->get_one(); last unless @$block; print $block->[0], " "; } # Print one line: yz123456 $filter->get_one_start([ "123456" ]); while(1) { my $block = $filter->get_one(); last unless @$block; print $block->[0], " "; } DESCRIPTION
POE::Filter::Block translates data between serial streams and blocks. It can handle fixed-length and length-prepended blocks, and it may be extended to handle other block types. Fixed-length blocks are used when Block's constructor is called with a BlockSize value. Otherwise the Block filter uses length-prepended blocks. Users who specify block sizes less than one deserve what they get. In variable-length mode, a LengthCodec parameter may be specified. The LengthCodec value should be a reference to a list of two functions: the length encoder, and the length decoder: LengthCodec => [ &encoder, &decoder ] The encoder takes a reference to a buffer and prepends the buffer's length to it. The default encoder prepends the ASCII representation of the buffer's length and a chr(0) byte to separate the length from the actual data: sub _default_encoder { my $stuff = shift; substr($$stuff, 0, 0) = length($$stuff) . ""; return; } The corresponding decoder returns the block length after removing it and the separator from the buffer. It returns nothing if no length can be determined. sub _default_decoder { my $stuff = shift; unless ($$stuff =~ s/^(d+)//s) { warn length($1), " strange bytes removed from stream" if $$stuff =~ s/^(D+)//s; return; } return $1; } This filter holds onto incomplete blocks until they are completed. PUBLIC FILTER METHODS
POE::Filter::Block has no additional public methods. SEE ALSO
Please see POE::Filter for documentation regarding the base interface. The SEE ALSO section in POE contains a table of contents covering the entire POE distribution. BUGS
The put() method doesn't verify block sizes. AUTHORS &; COPYRIGHTS The Block filter was contributed by Dieter Pearcey, with changes by Rocco Caputo. Please see POE for more information about authors and contributors. perl v5.14.2 2012-05-15 POE::Filter::Block(3pm)
All times are GMT -4. The time now is 08:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy