Computing.Net > Forums > Unix > Using hex-code in shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Using hex-code in shell script

Reply to Message Icon

Name: wendy
Date: November 18, 2002 at 18:50:47 Pacific
OS: AIX
CPU/Ram: RS 6000, memory 1024MB
Comment:

In shell script sed or awk command, can I use hexadecimal code for substitution? I want to perform the following:

Input file:
Line1
Line2
Line3
Line4

Output file: (all in one line now)
Line1Line2Line3Line4


The output file will then be processed by my program and my program will detect the hexadecimal code '0000' as separator.



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: November 20, 2002 at 06:46:02 Pacific
Reply:

Are you thinking you can do a global substitution, replacing the newline characters with something else, thus creating a single-line file? The newline character cannot be changed in that manner because as each line is read into the buffer for processing (with awk or whatever), the buffer does not include the terminating newline character (octal 12).

To get your input file into one line, you can print each line with the newline character suppressed. Run the following command as an example:

print "Enter selection: \c"

awk allows octal and hex representation (the letter E can be represented as "\105" or "\x45"). But I cannot get awk to output a null, although no problem outputting other non-printing characters.

But print or printf will output nulls. The script below will output a single line with the original lines separated by two null characters. I am assuming that your hex 0000 is two null bytes, with each zero representing a 4-bit hex code. Since this solution appends nulls to the end of each original line, the output file ends with nulls. Since you want these nulls as line delimiters, you really want them only BETWEEN the lines. The delimiter at the very end, with nothing following, is probably undesirable. I could resolve this with awk, if awk would only output the nulls.

# !/bin/sh
IFS=
cat infile | while read line
do
print "$line\000\000\c"
done > outfile
exit 0


0

Response Number 2
Name: LANkrypt0
Date: November 20, 2002 at 13:21:13 Pacific
Reply:

You just delete the newlines with:
tr -d "\n"


0

Response Number 3
Name: James Boothe
Date: November 20, 2002 at 13:40:24 Pacific
Reply:

Thanks for that, LANkrypt0. I knew there was a command to do it - just could not recall it.

So, to combine multiple lines into one AND insert a single null character as delimiter for the original lines, this works:

cat infile | tr ["\n"] ["\0"] > outfile

Apologies to Wendy and all for incorrectly saying that you cannot directly substitute newline.


0

Response Number 4
Name: wendy
Date: November 21, 2002 at 20:25:29 Pacific
Reply:

James Boothe and LANkrpt0, thanks!

The tr command works.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Using hex-code in shell script

loops in shell scripts!!! www.computing.net/answers/unix/loops-in-shell-scripts/5140.html

Array size in shell script? www.computing.net/answers/unix/array-size-in-shell-script/5761.html

SQL in shell script www.computing.net/answers/unix/sql-in-shell-script/5538.html