Add character based on record length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add character based on record length
# 1  
Old 11-05-2009
Add character based on record length

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.
# 2  
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..
# 3  
Old 11-05-2009
Code:
awk 'length($0)<874{printf "%-877s%s\n", $0,"E";next}1' file

Not tested Smilie
# 4  
Old 11-05-2009
Durden,

The only change that I need is that I do not want the 'E' to be written over the current character, but instead it needs to be inserted.

Thanks again.

---------- Post updated at 12:58 PM ---------- Previous update was at 12:49 PM ----------

Just figure out that I need to change the "1" to "0" in Durden's example. Thanks a million to both of you.

perl -lne 'substr($_,777,0) = "E" if length != 874; print' $filename > dc_3.txt
# 5  
Old 11-05-2009
Code:
awk 'length!=874{sub(778,"E")}1' infile



---------- Post updated at 11:33 AM ---------- Previous update was at 11:18 AM ----------

insert:
Code:
awk 'length!=874{sub(778,"E"substr($0,778,1))}1' infile


Last edited by Scrutinizer; 11-05-2009 at 03:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question