> Covert Deci to Hex (New to AWK) | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Covert Deci to Hex (New to AWK)

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-04-2005
Linda_H Linda_H is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 16 June 2005, 3:37 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Covert Deci to Hex (New to AWK)

Hi, I am very new to awk and would be grateful for your help.

I need to convert a file with details like this,


Wregister( 45676, 0)
Wregister( 55550, 1)
Wregister( 44000, 0)
FlipFlop( 2342, 2)
.
.

to look like this

Wregister(626C, 0)
Wregister(D8FE, 1)
Wregister(ABE0, 0)
FlipFlop( 2342, 2) -----> unchanged,name doesn't start with "Wregister("
.
.

It converts $2 in deci into hex, and then combines the previous $1 and $2 together. The (,) at the end of the numbers is a problematic one for me. It needs to be retained too.

Please help me.
Sponsored Links
    #2  
Old Unix and Linux 05-04-2005
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 25 Times in 23 Posts

Code:
$ more file
Wregister( 45676, 0)
Wregister( 55550, 1)
Wregister( 44000, 0)
FlipFlop( 2342, 2)
$ more file.sh 
#!/bin/sh

while read line; do
  echo "$line" | grep "^Wreg" >/dev/null 2>&1
  if [ "$?" -eq "0" ]; then
     val=`echo "$line" | sed 's/^Wregister*([ ]*\([0-9]*\),.*$/\1/'`
     hex=`echo "obase=16;ibase=10; $val" | bc`
     echo "$line" | sed "s/$val/$hex/"
  else
     echo "$line"
  fi
done < file

exit 0
$ chmod +x ./file.sh
$ ./file.sh > newfile
$ more newfile
Wregister( B26C, 0)
Wregister( D8FE, 1)
Wregister( ABE0, 0)
FlipFlop( 2342, 2)

Cheers
ZB
Sponsored Links
    #3  
Old Unix and Linux 05-04-2005
Linda_H Linda_H is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 16 June 2005, 3:37 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi ZB,

Thanks very much for the help. I realised that you are using "sed" command, but can i do it in awk? and I am running the prog in UNIX.
    #4  
Old Unix and Linux 05-04-2005
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 25 Times in 23 Posts
This is run under "UNIX".

Also; you no doubt could solve this using awk, but why bother if it's already solved using the shell, bc and sed?

Cheers
ZB
Sponsored Links
    #5  
Old Unix and Linux 05-05-2005
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 July 2016, 3:07 PM EDT
Location: Foxborough, MA
Posts: 7,948
Thanks: 184
Thanked 685 Times in 641 Posts
Quote:
Originally Posted by Linda_H
Hi ZB,

Thanks very much for the help. I realised that you are using "sed" command, but can i do it in awk? and I am running the prog in UNIX.
nawk -f linda.awk myFile

linda.awk:

Code:
BEGIN {
  FS="[(,]"
  OFS=","
}
$1 == "Wregister" {
  $1 = sprintf("%s(%X", $1, $2)
  $2=$3; NF--
}
1

Sponsored Links
    #6  
Old Unix and Linux 06-03-2005
Linda_H Linda_H is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 16 June 2005, 3:37 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi vgersh99,

Thks for the help. I finally applied it succesfully today.

But, here comes another seperate but related problem. This time, I tried to convert from a Hex to Deci number by creating a new script with "%d" on the hex number, but it failed.

I need to convert a file with these,

ABC c001 DEF
ABC c050 EFG
ABC c802 GHI

to

ABC 49153 DEF
ABC 49232 EFG
ABC 51202 GHI

Please help me again.
Thank you!!
Sponsored Links
    #7  
Old Unix and Linux 06-03-2005
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 July 2016, 3:07 PM EDT
Location: Foxborough, MA
Posts: 7,948
Thanks: 184
Thanked 685 Times in 641 Posts
Quote:
Originally Posted by Linda_H
Hi vgersh99,

Thks for the help. I finally applied it succesfully today.

But, here comes another seperate but related problem. This time, I tried to convert from a Hex to Deci number by creating a new script with "%d" on the hex number, but it failed.

I need to convert a file with these,

ABC c001 DEF
ABC c050 EFG
ABC c802 GHI

to

ABC 49153 DEF
ABC 49232 EFG
ABC 51202 GHI

Please help me again.
Thank you!!
nawk -f linda.awk yourFile.txt

linda.awk:

Code:
BEGIN {
    for (i = 0; i < 10; i++)
      hex[i] = i
    hex["a"] = hex["A"] = 10
    hex["b"] = hex["B"] = 11
    hex["c"] = hex["C"] = 12
    hex["D"] = hex["d"] = 13
    hex["e"] = hex["E"] = 14
    hex["f"] = hex["F"] = 15
  }

  function dehex(h,  i,x) {
    for (i = 1; i <= length(h); i++)
      x = x*16 + hex[substr(h, i, 1)]
    return x
  }

{
 $2 = dehex($2)
 print
}

CompLangAWK on USENET
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Batch add covert art to AAC/m4b Files glev2005 UNIX for Dummies Questions & Answers 3 02-08-2011 04:36 PM
how to covert string into 2 diff int xiaojesus Programming 8 05-12-2009 11:09 AM
any script can be used to covert IP subnet? fedora Shell Programming and Scripting 0 03-04-2009 06:25 PM
Covert case of first letter only tisons Shell Programming and Scripting 1 01-28-2005 05:13 AM



All times are GMT -4. The time now is 05:08 PM.