Convert Chinese characters to URL code under Linux shell

Source: Internet
Author: User
Tags printf linux

In the Linux shell environment, there is an OD command that allows files to be exported in different ASCII code, which completes the task of converting Chinese characters to URL encoding under the Linux shell.

The URL encoding is a hexadecimal-like%e6%9c%8d%e5%8a%a1%e5 string, and test is a file that contains the characters "database network," The following command output:

#od -t x /test
0000000 ddbefdca f8cde2bf 0a2ce7c2
0000014

The string corresponds to the URL encoding%ca%fd%be%dd%bf%e2%cd%f8%c2%e7%2c.

You can see the corresponding relationship between the encodings. The following is the script for the conversion:

CODE:#!/bin/sh
#make url code
od -t x /test |awk '{
w=split($0,linedata," ");
for (j=2;j<w+1;j++)
{
for (i=7;i>0;i=i-2)
{
if (substr(linedata[j],i,2) != "00") {printf "%" ;printf toupper(substr(linedata[j],i,2));}
}
}
}' >/testurl

The content of the Testurl file is the result of the conversion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.