Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
Line4Output 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.

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

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.

![]() |
![]() |
![]() |

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