Bash - Inserting non printable character(s) in string variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - Inserting non printable character(s) in string variable
# 1  
Old 12-18-2015
Bash - Inserting non printable character(s) in string variable

Hello.
I have a string variable named L_TEMP to test a very simple filter.
L_TEMP="50AwL.|KWp9jk"
I want to insert a non printable character [ 0x00 - 0x1F ] between K and W.
I have try this :
Code:
linux-g65k:~ # a='50AwL.|K'
linux-g65k:~ # b='Wp9jk'
linux-g65k:~ # L_TEMP="$a$'\x07'$b"
linux-g65k:~ # echo "PATTERN : $L_TEMP"
PATTERN : 50AwL.|K$'\x07'Wp9jk
linux-g65k:~ # L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")
linux-g65k:~ # echo $L_TEMP_TEST
50AwL.|K$'\x07'Wp9jk
linux-g65k:~ # 
linux-g65k:~ #

The result should be :
Code:
linux-g65k:~ # a='50AwL.|K'
linux-g65k:~ # b='Wp9jk'
linux-g65k:~ # L_TEMP="$a$'\x07'$b"
linux-g65k:~ # echo "PATTERN : $L_TEMP"
PATTERN : 50AwL.|KWp9jk
linux-g65k:~ # L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")
linux-g65k:~ # echo $L_TEMP_TEST

linux-g65k:~ # 
linux-g65k:~ #

Any help is welcome
# 2  
Old 12-18-2015
Try
Code:
L_TEMP=$a$'\x07'$b

or
Code:
L_TEMP=$a$'\a'$b

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-18-2015
My filter is :
Code:
L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")

My filter is malformed.
I want to exclude any value which does not contains exclusively printable character.
Code:
L_TEMP=$a$'\x07'$b
L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")

L_TEMP_TEST should be null
What is the good filter ?
Any help is welcome
# 4  
Old 12-18-2015
Not sure I understand. You want to eliminate non-printable chars from a string?
# 5  
Old 12-18-2015
Quote:
Originally Posted by RudiC
Not sure I understand. You want to eliminate non-printable chars from a string?
in pseudo code :
Code:
 MY_VAR=$L_TEMP
while read -N 1 ; do
   A_CHAR="$REPLY"
   if [[ $A_CHAR is not A_PRINTABLE_CHARACTER ]] ; then
    MY_VAR=""
    break
   fi
done <<<"$L_TEMP"

if [[ ! z $MY_VAR ]] ; then
    OK KEEP MY_VAR
else
    CALCULATE ANOTHER MY_VAR
fi

MY_VAR should set to null if it contains one or more not printable character
Somethings like
Code:
grep -E "[all characters are printable]"

my current filter is
Code:
grep -E "[one or more characters are printable]"

---------- Post updated at 13:09 ---------- Previous update was at 12:54 ----------

I have try
Code:
grep -e '[^[:print:]]'

Which seems not working
# 6  
Old 12-18-2015
You could use
Code:
echo $L_TEMP | grep -v "[^[:print:]]"

or, with a recent shell,
Code:
L_TEMP_TEST=${L_TEMP//[[:print:]]}

The second would leave L_TEMP_TEST empty if no non-printable chars are in the variable, or the non-printable chars only if there were.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-18-2015
That works
Great
Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. Shell Programming and Scripting

Inserting a non printable character in a file

For some testing I want to insert a non printable character in a file. How to do it? I inserted ctrl-v ctrl-k through vi. But I do not think it is a proper non printable character. (3 Replies)
Discussion started by: Soham
3 Replies

3. Shell Programming and Scripting

Bash: Pulling first and last character in string

I am writing a bash script that will find all references to the “Well_List” in the “Comp_File”. I am filtering a Well_List that contains the following: TEST_WELL_01 TEST_WELL_02 TEST_WELL_11 TEST_WELL_22 GOV_WELL_1 GOV_WELL_201 PUB_WELL_57 PUB_WELL_82 . . Comparison... (5 Replies)
Discussion started by: petfyp
5 Replies

4. Shell Programming and Scripting

[Solved] SED - Bash - Inserting multi Tab character in the command

Hello. I am using : sed -i -e '/§name_script§/a#'"${MY_TAB11}"'# \ #'"${MY_TAB1}"'The Standard way'"${MY_TAB7}"'# \ #'"${MY_TAB1}"'==============='"${MY_TAB7}"'# \ ' "$CUR_FILE" Is there a better way to define "MY_TAB7","MY_TAB11" in other way than : MY_TAB1=$'\t' MY_TAB2=${MY_TAB1}$'\t'... (2 Replies)
Discussion started by: jcdole
2 Replies

5. Shell Programming and Scripting

Bash: How to remove the last character of a string?

In bash, how can one remove the last character of a string? In perl, the chop function would remove the last character. However, I do not know how to do the same job in bash. Many thanks in advance. (12 Replies)
Discussion started by: LessNux
12 Replies

6. Shell Programming and Scripting

Bash - get specific character from the string

Hi! If I want to extract a character from a specific position of a string, I can use ${string:1:1} (if I want character at the position 1). How can I do the same thing, when the number of position is contained in the variable? ${string:$var:1}doesn't work, unfortunately. Thanks in advance. (2 Replies)
Discussion started by: xqwzts
2 Replies

7. Shell Programming and Scripting

Check whether there is a non printable character in the unix variables

cp $l_options $srcdirfile $destdirfile If i want to check whether there is a non printable character in the variables $l_options $srcdirfile $destdirfile how it can be done? (2 Replies)
Discussion started by: lalitpct
2 Replies

8. Shell Programming and Scripting

Inserting newline in front of multi-character string

I'm working with a large file with multiple records, each record begins with ISA. The issue is, sometimes ISA is at the start of the line, sometimes it's in the middle of the line. So before I can csplit my main file into multiple records, I have to get each record header onto its own line. ... (7 Replies)
Discussion started by: verge
7 Replies

9. Shell Programming and Scripting

inserting a character between string

i have a file contains like this: i want to create a script that will insert a comma "." after the 10th character so it would be look like this thanks in advance (5 Replies)
Discussion started by: dakid
5 Replies

10. Shell Programming and Scripting

bash script to check the first character in string

Hello would appreciate if somebody can post a bash script that checks if the first character of the given string is equal to, say, "a" thnx in advance (2 Replies)
Discussion started by: ole111
2 Replies
Login or Register to Ask a Question