Sponsored Content
Top Forums Shell Programming and Scripting Perl: Parse Hex file into fields Post 302525139 by morrbie on Wednesday 25th of May 2011 10:56:30 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
fmthard(1M)						  System Administration Commands					       fmthard(1M)

NAME
fmthard - populate label on hard disks SYNOPSIS
SPARC fmthard -d data | -n volume_name | -s datafile [-i] /dev/rdsk/c? [t?] d?s2 x86 fmthard -d data | -n volume_name | -s datafile [-i] /dev/rdsk/c? [t?] d?s2 DESCRIPTION
The fmthard command updates the VTOC (Volume Table of Contents) on hard disks and, on x86 systems, adds boot information to the Solaris fdisk partition. One or more of the options -s datafile, -d data, or -n volume_name must be used to request modifications to the disk label. To print disk label contents, see prtvtoc(1M). The /dev/rdsk/c?[t?]d?s2 file must be the character special file of the device where the new label is to be installed. On x86 systems, fdisk(1M) must be run on the drive before fmthard. If you are using an x86 system, note that the term ``partition'' in this page refers to slices within the x86 fdisk partition on x86 machines. Do not confuse the partitions created by fmthard with the partitions created by fdisk. OPTIONS
The following options are supported: -d data The data argument of this option is a string representing the information for a particular partition in the current VTOC. The string must be of the format part:tag:flag:start:size where part is the partition number, tag is the ID TAG of the partition, flag is the set of permission flags, start is the starting sector number of the partition, and size is the number of sectors in the partition. See the description of the datafile below for more information on these fields. -i This option allows the command to create the desired VTOC table, but prints the information to standard output instead of modifying the VTOC on the disk. -n volume_name This option is used to give the disk a volume_name up to 8 characters long. -s datafile This option is used to populate the VTOC according to a datafile created by the user. If the datafile is - (a hyphen), fmthard reads from standard input. The datafile format is described below. This option causes all of the disk partition timestamp fields to be set to zero. Every VTOC generated by fmthard will also have partition 2, by convention, that corresponds to the whole disk. If the input in datafile does not specify an entry for partition 2, a default partition 2 entry will be created automatically in VTOC with the tag V_BACKUP and size equal to the full size of the disk. The datafile contains one specification line for each partition, starting with partition 0. Each line is delimited by a new-line char- acter ( ). If the first character of a line is an asterisk (*), the line is treated as a comment. Each line is composed of entries that are position-dependent, separated by white space and having the following format: partition tag flag starting_sector size_in_sectors where the entries have the following values: partition The partition number. Currently, for Solaris SPARC, a disk can have up to 8 partitions, 0-7. Even though the partition field has 4 bits, only 3 bits are currently used. For x86, all 4 bits are used to allow slices 0-15. Each Solaris fdisk partition can have up to 16 slices. tag The partition tag: a decimal number. The following are reserved codes: 0 (V_UNASSIGNED), 1 (V_BOOT), 2 (V_ROOT), 3 (V_SWAP), 4 (V_USR), 5 (V_BACKUP), 6 (V_STAND), 7 (V_VAR), and 8 (V_HOME). flag The flag allows a partition to be flagged as unmountable or read only, the masks being: V_UNMNT 0x01, and V_RONLY 0x10. For mount- able partitions use 0x00. starting_sector The sector number (decimal) on which the partition starts. size_in_sectors The number (decimal) of sectors occupied by the partition. You can save the output of a prtvtoc command to a file, edit the file, and use it as the datafile argument to the -s option. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
uname(1), format(1M), prtvtoc(1M), attributes(5) x86 Only fdisk(1M), installgrub(1M) NOTES
Special care should be exercised when overwriting an existing VTOC, as incorrect entries could result in current data being inaccessible. As a precaution, save the old VTOC. For disks under two terabytes, fmthard cannot write a VTOC on an unlabeled disk. Use format(1M) for this purpose. SunOS 5.11 25 Sep 2008 fmthard(1M)
All times are GMT -4. The time now is 04:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy