Perl: Parse Hex file into fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Parse Hex file into fields
# 1  
Old 05-25-2011
Lightbulb Perl: Parse Hex file into fields

Hi,

I want to split/parse certain bits of the hex data into another field.

Example:
Input data is
Word1: 4f72abfd
Output:
Parse bits (5 to 0) into field word1data1=0x00cd=205 decimal
Parse bits (7 to 6) into field word1data2=0x000c=12 decimal
etc.
Word2: efff3d02
Parse bits (13 to 3) into field word2data1=0x0f40=3904 decimal
Parse bits (16 to 14) into field word2data2=0x0004=4 decimal
etc.

The bits that are parsed from each input word is different for all 8 input hex words.

I have the following INPUT hex file (input.txt):

# line is a comment
Code:
% 30105 3 5 3 1 1  -- this line is used as a tag
4f72abfd
efff3d02
353b3212
ffff9299
22222222
2299330a
9abe8f12
930d9d2d
# line is a comment
% 30105 3 5 3 1 1  -- this line is used as a tag
3572aaff
820f9283
ffff9299
22222222
2299330a
9abe8f12
930d9d2d

... and so on and so forth

I would like the OUTPUT to look like this (output.txt):

Code:
# line is a comment
% 30105 3 5 3 1 1  -- this line is used as a tag
4f72abfd word1field1=0x00cd=205, word1data2=0x000c=12, word1da#=...
efff3d02 word2field1=0x0f40=3904 , word2data2=0x0004=4, word2da#=...
353b3212 word3field1=..., word3field2=..., etc.
ffff9299 word4field1=..., word4field2=..., etc.
22222222 word5field1=..., word5field2=..., etc.
2299330a word6field1=..., word6field2=..., etc.
9abe8f12 word7field1=..., word7field2=..., etc.
930d9d2d word8field1=..., word8field2=..., etc.
# line is a comment
% 30105 3 5 3 1 1  -- this line is used as a tag
3572aaff word1field1=..., word1data2=..., word1data#=...
820f9283 word2field1=..., word2data2=..., word2data#=...
ffff9299
22222222
2299330a
9abe8f12
930d9d2d word8data1=..., word8data2=..., word8data#=...

... and so on and so forth

Can someone help me write a perl script?

Greatly appreciated!! Thanks.

Last edited by pludi; 05-26-2011 at 03:36 AM..
morrbie
# 2  
Old 05-26-2011
Well, as you read the character data, you must substring and convert from hex in character to integer, before you "and" '&' to extract bits. hex - perldoc.perl.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse file for fields and specific text

I have a file of ~500,000 entries in the following: file.txt chr1 11868 12227 ENSG00000223972.5 . + HAVANA exon . gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type... (17 Replies)
Discussion started by: cmccabe
17 Replies

2. UNIX for Advanced & Expert Users

Script to parse and compare information in two fields of file

Hello, I am working parsing a large input file1(field CFA) I have to compare the the file1 field(CFA byte 88-96) with the content of the file2(It contains only one field) and and insert rows equal in another file. Here is my code and sample input file: ... (7 Replies)
Discussion started by: GERMANOS
7 Replies

3. Shell Programming and Scripting

awk script to parse case with information in two fields of file

The below awk parser works for most data inputs, but I am having trouble with the last one. The problem is in the below rules steps 1 and 2 come from $2 (NC_000013.10:g.20763686_20763687delinsA) and steps 3 and 4 come from $1 (NM_004004.5:c.34_35delGGinsT). Parse Rules: The header is... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

awk or perl to parse file

I have an input file attached that I am trying to parse in tab-delimanted format: The chromosomal variant column contains all the information: parse rules: 1. 4 zeros after the NC_ and the digits before the . 2. digits after the g. repeated twice separated by a tab 3. letter before the > 4.... (10 Replies)
Discussion started by: cmccabe
10 Replies

5. UNIX for Advanced & Expert Users

Convert 32 bit hex value into fields in decimal

I have 32 bit value in hex that I want to separate into fields and then convert the fields into decimal values. Input file has 2 words of 32 bit hex values: 000001ac ca85210e Output both words separated into individual bit fields: ca85210e: f1(31:9), f2(8:0) f7c392ac: f1(31:14),... (2 Replies)
Discussion started by: morrbie
2 Replies

6. Shell Programming and Scripting

Parse file contents in perl...

Hi, I have the file like this: #Contents of file 1 are: Dec 10 12:33:44 User1 Interface: Probe Dec 10 12:33:47 uSER1 SOME DATA Dec 10 12:33:47 user1 Interface: MSGETYPE Dec 10 12:34:48 user1 ID: 10. Dec 10 12:33:55 user1 Interface: MSGTYPE Dec 10 12:33:55 user1 Id: 9 ... (1 Reply)
Discussion started by: vanitham
1 Replies

7. UNIX for Dummies Questions & Answers

grep on Hex fields

I'm trying to find all modules that contain line feed characters. It shows up at ^M (Hex 0D0A). Does anyone know how to do a search for hex fields? I tried doing "egrep ^M *.cbl", but that doesn't work. Thanks! (3 Replies)
Discussion started by: rthiele
3 Replies

8. Shell Programming and Scripting

How to read and parse the content of csv file containing # as delimeter into fields using Bash?

#!/bin/bash i=0 cat 1.csv | while read fileline do echo "$fileline" IFS="#" flds=( $fileline ) nrofflds=${#flds} echo "noof fields$nrofflds" fld=0 while do echo "noof counter$fld" echo "$nrofflds" #fld1="${flds}" trying to store the content of line to fields but i... (4 Replies)
Discussion started by: barani75
4 Replies

9. Shell Programming and Scripting

CSV File parse help in Perl

Folks, I have a bit of an issue trying to obtain some data from a csv file using PERL. I can sort the file and remove any duplicates leaving only 4 or 5 rows containing data. My problem is that the data contained in the original file contains a lot more columns and when I try ro run this script... (13 Replies)
Discussion started by: lodey
13 Replies

10. UNIX for Dummies Questions & Answers

hex value in a file + perl

Am not able to display the corresponding character for the hex value using the format specifier into a file Could you please help me with that >cat other a|\xc2\xbo >cat write.pl #! /opt/third-party/bin/perl open(FILE2, "< other") || die "Unable to open file other\n"; while (... (7 Replies)
Discussion started by: matrixmadhan
7 Replies
Login or Register to Ask a Question