Change Hexa to binary in awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change Hexa to binary in awk script
# 1  
Old 05-09-2013
Change Hexa to binary in awk script

hi all

i want to change hexa to binary in awk script as my script is :
Code:
grep  -e ID -e Name -e HexaID | awk '{ print $1"|"$2"|"$3) }'

i want instead of $3 to be any thing to change HexaID to binary as if :
Code:
input:   1|john|5F 
output: 2|Makr|01011111

thanks alot
# 2  
Old 05-09-2013
There is no easy way to do it in awk.. you might have to convert the hex to binary then use it in script

Code:
 
var="5F" #HexID
echo 16 i 2 o $var p|dc

# 3  
Old 05-09-2013
How about perl

Code:
#!/usr/bin/perl

%h2b = (0 => "0000",
1 => "0001", 2 => "0010", 3 => "0011",
4 => "0100", 5 => "0101", 6 => "0110",
7 => "0111", 8 => "1000", 9 => "1001",
a => "1010", b => "1011", c => "1100",
d => "1101", e => "1110", f => "1111"
);

while (<DATA>) {
        chomp;
        @hexfileds=split(/\|/);
                ($binary = $hexfileds[2]) =~ s/(.)/$h2b{lc $1}/g;
                print $binary, "\n";
}
__DATA__
1|john|5F
2|Tom|6FE

# 4  
Old 05-09-2013
An awk program using pravin27's idea:
Code:
awk -F'|' '
        BEGIN {
                A["0"]="0000"
                A["1"]="0001"
                A["2"]="0010"
                A["3"]="0011"
                A["4"]="0100"
                A["5"]="0101"
                A["6"]="0110"
                A["7"]="0111"
                A["8"]="1000"
                A["9"]="1001"
                A["A"]="1010"
                A["B"]="1011"
                A["C"]="1100"
                A["D"]="1101"
                A["E"]="1110"
                A["F"]="1111"
        }
        {
                n = split ( $3, V, "" )
                printf  $1 "|" $2 "|"
                for ( i = 1; i <= n; i++ )
                        printf A[toupper(V[i])]
                printf "\n"
        }
' file

# 5  
Old 05-09-2013
You can use the popular bits2str function from GAWK manual
Code:
function bits2str(bits, data)
{
    if (bits == 0)
        return "0"

    mask = 1
    for (; bits != 0; bits = rshift(bits, 1))
        data = (and(bits, mask) ? "1" : "0") data

    while ((length(data) % 8) != 0)
        data = "0" data

    return data
}

{
    printf "%s = %s\n", $2, bits2str(sprintf("%d", "0x" $2))
}

Input File:
Code:
John|5D
Mike|6A
Paul|1B

Code:
gawk --non-decimal-data -F"|" -f awkscript infile
5D = 01011101
6A = 01101010
1B = 00011011

You do not need the --non-decimal-data option flag for mawk or nawk.
These 2 Users Gave Thanks to fpmurphy For This Post:
# 6  
Old 05-10-2013
thanks alot for all posts
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to change file into an awk script

Hi I want to use sed to change a text files input into an awk script. For example if the input says " chord -- english " I want to change this using sed 's/pattern 1 /pattern 2 /'g filename but I don't understand how to use part of the pattern 1 to input that into pattern 2 . Like after... (10 Replies)
Discussion started by: enforcer
10 Replies

2. Shell Programming and Scripting

Make change to variable value inside of awk script

Hello, I have text data that looks like this, Mrv16a3102061815532D 6 6 0 0 0 0 999 V2000 -0.4018 1.9634 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 1.5509 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 0.7259 ... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

3. Shell Programming and Scripting

Using awk to assign binary values to data above/below a certain threshold?

Hello, I have files containing a large amount of values in columns, and I want to simplify the data by making the values binary, i.e. assigning 1/0 for each value above or below a certain threshold. I am curious to know if it's possible to use awk to do this (or another way in bash). I've... (2 Replies)
Discussion started by: ksennin
2 Replies

4. Shell Programming and Scripting

Grep - build character range with octal or hexa representation

Hello I would like to make a character range like that : echo "ABCDEF+1234" | grep -E ''or echo "ABCDEF+1234" | grep -E ''or echo "ABCDEF+1234" | grep -E ''Which should works on linux with english language And works also on linux with french language ( english install and after add french )... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

Need a script for HEXA meta creator

Hi All, Need help how I can create a script generate a Meta if have give range. For e.g This is range 4264:4803 around 1440 device i need to built a script to create a meta file if I have input field as below Output format I need. Like that I need 40 meta with the rage of 30 device and... (0 Replies)
Discussion started by: ranjancom2000
0 Replies

6. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

7. HP-UX

How to use chatr to change static library path in binary?

I have solved this but now cannot recall the syntax. I want to strip or change the hard wired library paths on binaries I download from a repository. I do not have root and just want a personal install, but would rather not build every tool. I even posted the solution here once for others, but... (2 Replies)
Discussion started by: DGPickett
2 Replies

8. Shell Programming and Scripting

awk get length of a binary file.

Hi.. Just got a problem with awk and cannot figure out whats the issue. I am not really expert in awk so looking for help/opinion from experts Command is $ cat abinaryfile | sed -e "s/@@//g;s/@$//" | awk ' begin {} { printf(length($0)); }' Well its a binary file and I am... (3 Replies)
Discussion started by: iffarrukh
3 Replies

9. Shell Programming and Scripting

need help in expanding hexa decimal character range

Hi all, I have a input like this 3AF9:3B01 and need to expand to the below output 3AF9 3AFA 3AFB 3AFC 3AFD 3AFE 3AFF 3B00 3B01 Please let me know the easiest way for achieving this. Thanks for the help in advance... (6 Replies)
Discussion started by: leo.maveriick
6 Replies

10. Programming

Conversion of Hexa Value in String From to Primitive Hexavalue.

Hi, In My Program I have HEXA value in a string array as below : char hexa="0xabcd1234"; //This is how I'm getting source data. Actaully I want this hexa value to be decremented with -1 and store it in another string as "0xabcd1234". Can any body help me how to do that..? I... (1 Reply)
Discussion started by: S.Vishwanath
1 Replies
Login or Register to Ask a Question