Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert character between values.

Status
Not open for further replies.

Cybex1

Technical User
Sep 3, 2011
33
US
I have a script I found that I am trying to adapt to read the ASCII characters of a file and convert them all to decimal. The script works but I can not tell the values apart once they are printed. I.E: 101961245175 but I need: 101:96:12:45:175
I don't care what the separator value is I just figured a colon would do well.

Here is the script I have;
Perl:
perl -lne 'chomp; @x=unpack("C* ",$_); print @x' test.txt

Thanks,
Cybex
 
Hi

Until the [tt]unpack[/tt] gurus wake up, a workaround :
Code:
perl -lne '[b]chomp[/b][teal];[/teal] [navy]@x[/navy][teal]=[/teal][b]unpack[/b][teal]([/teal][green][i]"C* "[/i][/green][teal],[/teal][navy]$_[/navy][teal]);[/teal] [b]print[/b] [highlight][b]join[/b] [green][i]":"[/i][/green][teal],[/teal][/highlight][navy]@x[/navy]' test.txt

Feherke.
[link feherke.github.com/][/url]
 
This
Code:
perl -lne 'chomp; @x=unpack("C* ",$_); print "@x\n"' test.txt
will use a space as the separator (and add a newline at the end). For any other separator
Code:
perl -lne 'chomp; @x=unpack("C* ",$_); print join(":",@x)' test.txt

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top