LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-19-2019, 07:27 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
stop sed from replacing unicode hex characters


I have a variable with some unicode hex characters:
Code:
LABEL=$(blkid CentOS-7-x86_64-DVD-1810.iso | awk '{ print $2, $3, $4 }' | sed -e 's| | \\x20|g' | sed -e 's|"||g")
which results in:
Code:
echo ${LABEL}
LABEL=CentOS\x207.6\x20x86_64
Now, I want to insert that in a file:
Code:
sed -e "/^label linux/ i label kickstart\n  menu label ^Kickstart CentOS 7\n  kernel vmlinux\n  append initrd=initrd.img inst.stage2=hd:${LABEL} inst.ks=cdrom:/ks.cfg\n" isolinux.cfg
which changes the unicode hex
Code:
\x20
back to a space. Any ideas on how to keep that from happening?
 
Old 11-19-2019, 08:57 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
probably you need to use double \ instead of single ones in LABEL.
 
Old 11-19-2019, 09:00 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
I don't really understand your sed line

Couple of examples:
Code:
LABEL=CentOS\x207.6\x20x86_64
echo "$LABEL"
CentOSx207.6x20x86_64

LABEL="CentOS\x207.6\x20x86_64"
echo "$LABEL"
CentOS\x207.6\x20x86_64
An to echo that to file:
Code:
echo "$LABEL" > test.txt
cat test.txt
CentOS\x207.6\x20x86_64
 
Old 11-19-2019, 11:25 AM   #4
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
The first command gives the the variable in the required format. However, when I go to insert it into isolinux.cfg, at that point sed replaces the unicode hex string with an ascii space. I guess I just shouldn't bother with changing to unicode hex when I create the variable. Go ahead an insert it with sed, then issue a second sed command to change those two spaces to the unicode hex. That seems like the most expedient way to do it.
 
Old 11-19-2019, 12:34 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
I think you need to define what unicode character set you are using. I don't think its sed's fault.

The echo command displays as posted because escape characters are not interpreted by default. The -e option enables escape characters and therefore compare the outputs:

echo ${LABEL}
versus
echo -e ${LABEL}

Since I am a native English speaker I have not paid enough attention character sets. It depends on what unicode character set you are using, what application you are using to view the file and how it interprets the characters. Many unicodes sets are backwards compatible with ASCII and therefore \x20 is a space.

As I had time to think a bit more I would refrain from using spaces the disk label. Maybe underscore or a hyphen.

Last edited by michaelk; 11-19-2019 at 01:38 PM.
 
Old 11-19-2019, 02:45 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
why all the messing about to get a space?

Code:
LABEL=CentOS$'\x20'7.6$'\x20'x86_64
cat -A - <<<$LABEL 

LABEL="CentOS 7.6 x86_64"
cat -A - <<<$LABEL 


#
an no need for the sed, awk can do sub() and gsub()
 
Old 11-19-2019, 04:25 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Quote:
By default, the inst.stage2= boot option is used on the installation media and set to a specific label (for example, inst.stage2=hd:LABEL=RHEL7\x20Server.x86_64).
I had to go back to the documentation... Hmm, it does show a space... I agree with pan64 that a double \\ should work.
 
Old 11-19-2019, 04:54 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
https://wiki.syslinux.org/wiki/index...e=Config#LABEL

or not

https://wiki.syslinux.org/wiki/index...=Config#APPEND
https://wiki.syslinux.org/wiki/index...=Config#INITRD

as for getting the Label

instead of messing about with awk/sed

Code:
MyISO="/path/to/CentOS-7-x86_64-DVD-1810.iso"
LABEL="$( /sbin/blkid -s LABEL -o value "${MyISO}" )"
echo ${LABEL// /_}


#
 
1 members found this post helpful.
Old 11-20-2019, 04:42 AM   #9
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
Thanks everyone! I'm trying to have a script to edit an isolinux.cfg file while creating a custom kickstart ISO image. I have it working with:
Code:
LABEL=$(blkid CentOS-7-x86_64-DVD-1810.iso | awk '{ print $2, $3, $4 }' | sed -e 's|"||g")
sed -i "/^label linux/ i label kickstart\n  menu label ^Kickstart CentOS 7\n  kernel vmlinux\n  append initrd=initrd.img inst.stage2=hd:${LABEL} inst.ks=cdrom:/ks.cfg\n" isolinux.cfg
read -r -a LBL <<< ${LABEL}
sed -i "s|\(${LBL[0]}\) \($LBL[1]}\) \(${LBL[2]}\)|\1\\\x20\2\\\x20\3|" isolinux.cfg
Thanks again for the help!
 
Old 11-20-2019, 05:42 AM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
my spin on it

Code:
#!/bin/bash
Entry () {
cat <<EOF
label kickstart\n\
  menu label ^Kickstart ${LABEL% *}\n\
  kernel vmlinux\n\
  append initrd=initrd.img\
 inst.stage2=hd:UUID=${UUID} LABEL=${LABEL// /\\\\\x20}\
 inst.ks=cdrom:\/ks.cfg
EOF
}
MyISO="/path/to/CentOS-7-x86_64-DVD-1810.iso"
eval $( /sbin/blkid -s UUID -s LABEL -o export "${MyISO}" )

sed "/^label linux/ i $(Entry)" isolinux.cfg


#
[edit]
and, to start to making it more robust
Code:
MyISO="${1-/path/to/default.iso}"

#
# ...
#
if [[ ${LABEL} ]]
then
  sed "/^label linux/ i $(Entry)" isolinux.cfg
else
  echo "failed to probe ${MyISO}"
fi


#

Last edited by Firerat; 11-20-2019 at 05:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] ASCII to HEX and hex to ascii ////// Programming 17 05-08-2018 09:55 PM
[SOLVED] Questions relating to Unicode characters and missing glyphs/characters coralfang Linux - Software 4 04-20-2018 05:20 PM
[SOLVED] Problem displaying Unicode special characters in Urxvt/rxvt-unicode terminal shahinism Slackware 4 10-22-2012 03:08 PM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
hex to ascii and ascii to hex ilnli Programming 7 08-31-2007 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration