Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the current output in a text file

1            3.491136  C1 00 08 00 DD 92 01 FF
2            3.560963  C1 00 08 00 DD 92 01 FF
3            3.600959  C1 00 08 00 DD 92 01 FF
4            3.640694  01 00
5            3.680950  C1 00 08 00 DD 92 01 FF
6            3.720947  C1 00 08 00 DD 92 01 FF
7            3.760941  C1 00 08 00 DD 92 01 FF
8            3.780677  01 00
9            3.800937  C1 00 08 00 DD 92 01 FF

but the desired output is (after hex to dec conversion)

1            3.491136  193 0 8 0 221 146 1 255
2            3.560963  193 0 8 0 221 146 1 255
3            3.600959  193 0 8 0 221 146 1 255
4            3.640694  1   0
5            3.680950  193 0 8 0 221 146 1 255
6            3.720947  193 0 8 0 221 146 1 255
7            3.760941  193 0 8 0 221 146 1 255
8            3.780677  1   0
9            3.800937  193 0 8 0 221 146 1 255

The problem is: I'm able to do the conversion using following command but the output written back in text file is again the same (actually twice!), but I expect converted data to be written back in file.

sed p file_name.txt -i |tr '[a-z]' '[A-z]' |sed 's/ / p /g' |sed 's/$/ p/'|awk '{print "16i "$0}'|dc |tr '\n' ' '
share|improve this question

Perl solution:

perl -nlae '$, = " "; print @F[0,1], map {hex} @F[2..$#F]'

If whitespace is significant, you might need this one:

perl -lne '($x, $y) = /(^[0-9]+\s*[0-9.]+\s*)([[:alnum:]\s]+)/;
           print $x, map {/\s/ ? $_ : hex} split(/( +)/, $y)'
share|improve this answer

Here's a solution using awk only:

awk '{ printf "%s%20s  ", $1, $2; for(i=3;i<=NF;i++) printf "%-4d", "0x"$i; print "" }' file_name.txt

It will produce:

1            3.491136  193 0   8   0   221 146 1   255
2            3.560963  193 0   8   0   221 146 1   255
3            3.600959  193 0   8   0   221 146 1   255
4            3.640694  1   0  
5            3.680950  193 0   8   0   221 146 1   255
6            3.720947  193 0   8   0   221 146 1   255
7            3.760941  193 0   8   0   221 146 1   255
8            3.780677  1   0  
9            3.800937  193 0   8   0   221 146 1   255

Note: This won't work in-place, if you really need that you can solve with:

    mv file_name.txt file_name.tmp
    awk '...' file_name.tmp > file_name.txt
    rm file_name.tmp
share|improve this answer

using ex

$ echo -e "%s/\<\x\x\>/\=printf('%d', '0x'.submatch(0))/g\n%p" | ex file.txt | column -t
1  3.491136  193  0  8  0  221  146  1  255
2  3.560963  193  0  8  0  221  146  1  255
3  3.600959  193  0  8  0  221  146  1  255
4  3.640694  1    0
5  3.680950  193  0  8  0  221  146  1  255
6  3.720947  193  0  8  0  221  146  1  255
7  3.760941  193  0  8  0  221  146  1  255
8  3.780677  1    0
9  3.800937  193  0  8  0  221  146  1  255
share|improve this answer

The error you are making is in the first command.

When you say `sed p file.txt -i', this means:

For every line in file, print the line; save the output in the file along with the original. This command duplicates every line and is far from what you intended.

just put three lines in a file like this:

1
2
3

then run sed p file -i. you will end up with:

1
1
2
2
3
3

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .