Elegant way to replace tr 'n' '' (Null byte generating warnings at runtime)
I strongly doubt about the grep
best use in my code and would like to find a better and cleaner coding style for extracting the session ID and security level from my cookie file :
cat mycookie
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
#HttpOnly_127.0.0.1 FALSE /mydir/ FALSE 0 security medium
The expected output is the SSID hash :
1hjs18icittvqvpa4tm2lv9b12
Piping grep
with tr 'n' ''
works like a charm in the command line, but generates warnings (warning: command substitution: ignored null byte in input
”) at the bash code execution. Here is the related code (with warnings):
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '')
I am using bash 4.4.12 (x86_64-pc-linux-gnu)
and could read here this crystal clear explanation :
Bash variables are stored as C strings. C strings are NUL-terminated.
They thus cannot store NULs by definition.
I could see here and there in both cases a coding solution using read
:
# read content from stdin into array variable and a scalar variable "suffix"
array=( )
while IFS= read -r -d '' line; do
array+=( "$line" )
done < <(process that generates NUL stream here)
suffix=$line # content after last NUL, if any
# emit recorded content
printf '%s' "${array[@]}"; printf '%s' "$suffix"
I don't want to use arrays nor a while
loop for this specific case, or others. I found this workaround using sed
:
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '_' | sed -e 's/_//g')
My two questions are :
1) Would it be a better way to substitute tr 'n' ''
, without using read
into a while
loop ?
2) Would it be a better way to extract properly the SSID and security level ?
Thx
bash
add a comment |
I strongly doubt about the grep
best use in my code and would like to find a better and cleaner coding style for extracting the session ID and security level from my cookie file :
cat mycookie
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
#HttpOnly_127.0.0.1 FALSE /mydir/ FALSE 0 security medium
The expected output is the SSID hash :
1hjs18icittvqvpa4tm2lv9b12
Piping grep
with tr 'n' ''
works like a charm in the command line, but generates warnings (warning: command substitution: ignored null byte in input
”) at the bash code execution. Here is the related code (with warnings):
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '')
I am using bash 4.4.12 (x86_64-pc-linux-gnu)
and could read here this crystal clear explanation :
Bash variables are stored as C strings. C strings are NUL-terminated.
They thus cannot store NULs by definition.
I could see here and there in both cases a coding solution using read
:
# read content from stdin into array variable and a scalar variable "suffix"
array=( )
while IFS= read -r -d '' line; do
array+=( "$line" )
done < <(process that generates NUL stream here)
suffix=$line # content after last NUL, if any
# emit recorded content
printf '%s' "${array[@]}"; printf '%s' "$suffix"
I don't want to use arrays nor a while
loop for this specific case, or others. I found this workaround using sed
:
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '_' | sed -e 's/_//g')
My two questions are :
1) Would it be a better way to substitute tr 'n' ''
, without using read
into a while
loop ?
2) Would it be a better way to extract properly the SSID and security level ?
Thx
bash
1
ssid=$(awk '/PHPSESSID/{printf("%s",$NF)}' file)
?
– Cyrus
Dec 29 '18 at 6:06
Please add your desired output for that sample input to your question.
– Cyrus
Dec 29 '18 at 6:06
I ran your commandssid=$(...
, both on command line and from a script, but didn't receive any error or warning. And I don't understand that substitution about n... what I am missing?
– linuxfan
Dec 29 '18 at 7:07
@Cyrus: Thx, I edited my question and added the expected output.
– hornetbzz
Dec 29 '18 at 19:29
@linuxfan: I did not say there is an error. I said I'm looking for a more elegant solution than my uggly pipedgrep
andsed
sequence and that I'd like to understand how to appropriately replacetr n
by stg else, w/o using awhile, read
loop. Thx.
– hornetbzz
Dec 29 '18 at 19:32
add a comment |
I strongly doubt about the grep
best use in my code and would like to find a better and cleaner coding style for extracting the session ID and security level from my cookie file :
cat mycookie
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
#HttpOnly_127.0.0.1 FALSE /mydir/ FALSE 0 security medium
The expected output is the SSID hash :
1hjs18icittvqvpa4tm2lv9b12
Piping grep
with tr 'n' ''
works like a charm in the command line, but generates warnings (warning: command substitution: ignored null byte in input
”) at the bash code execution. Here is the related code (with warnings):
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '')
I am using bash 4.4.12 (x86_64-pc-linux-gnu)
and could read here this crystal clear explanation :
Bash variables are stored as C strings. C strings are NUL-terminated.
They thus cannot store NULs by definition.
I could see here and there in both cases a coding solution using read
:
# read content from stdin into array variable and a scalar variable "suffix"
array=( )
while IFS= read -r -d '' line; do
array+=( "$line" )
done < <(process that generates NUL stream here)
suffix=$line # content after last NUL, if any
# emit recorded content
printf '%s' "${array[@]}"; printf '%s' "$suffix"
I don't want to use arrays nor a while
loop for this specific case, or others. I found this workaround using sed
:
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '_' | sed -e 's/_//g')
My two questions are :
1) Would it be a better way to substitute tr 'n' ''
, without using read
into a while
loop ?
2) Would it be a better way to extract properly the SSID and security level ?
Thx
bash
I strongly doubt about the grep
best use in my code and would like to find a better and cleaner coding style for extracting the session ID and security level from my cookie file :
cat mycookie
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
#HttpOnly_127.0.0.1 FALSE /mydir/ FALSE 0 security medium
The expected output is the SSID hash :
1hjs18icittvqvpa4tm2lv9b12
Piping grep
with tr 'n' ''
works like a charm in the command line, but generates warnings (warning: command substitution: ignored null byte in input
”) at the bash code execution. Here is the related code (with warnings):
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '')
I am using bash 4.4.12 (x86_64-pc-linux-gnu)
and could read here this crystal clear explanation :
Bash variables are stored as C strings. C strings are NUL-terminated.
They thus cannot store NULs by definition.
I could see here and there in both cases a coding solution using read
:
# read content from stdin into array variable and a scalar variable "suffix"
array=( )
while IFS= read -r -d '' line; do
array+=( "$line" )
done < <(process that generates NUL stream here)
suffix=$line # content after last NUL, if any
# emit recorded content
printf '%s' "${array[@]}"; printf '%s' "$suffix"
I don't want to use arrays nor a while
loop for this specific case, or others. I found this workaround using sed
:
ssid=$(grep -Po 'PHPSESSID.*' path/sessionFile | grep -Po '[a-z]|[0-9]' | tr 'n' '_' | sed -e 's/_//g')
My two questions are :
1) Would it be a better way to substitute tr 'n' ''
, without using read
into a while
loop ?
2) Would it be a better way to extract properly the SSID and security level ?
Thx
bash
bash
edited Dec 29 '18 at 19:28
hornetbzz
asked Dec 29 '18 at 5:54
hornetbzzhornetbzz
4,18452846
4,18452846
1
ssid=$(awk '/PHPSESSID/{printf("%s",$NF)}' file)
?
– Cyrus
Dec 29 '18 at 6:06
Please add your desired output for that sample input to your question.
– Cyrus
Dec 29 '18 at 6:06
I ran your commandssid=$(...
, both on command line and from a script, but didn't receive any error or warning. And I don't understand that substitution about n... what I am missing?
– linuxfan
Dec 29 '18 at 7:07
@Cyrus: Thx, I edited my question and added the expected output.
– hornetbzz
Dec 29 '18 at 19:29
@linuxfan: I did not say there is an error. I said I'm looking for a more elegant solution than my uggly pipedgrep
andsed
sequence and that I'd like to understand how to appropriately replacetr n
by stg else, w/o using awhile, read
loop. Thx.
– hornetbzz
Dec 29 '18 at 19:32
add a comment |
1
ssid=$(awk '/PHPSESSID/{printf("%s",$NF)}' file)
?
– Cyrus
Dec 29 '18 at 6:06
Please add your desired output for that sample input to your question.
– Cyrus
Dec 29 '18 at 6:06
I ran your commandssid=$(...
, both on command line and from a script, but didn't receive any error or warning. And I don't understand that substitution about n... what I am missing?
– linuxfan
Dec 29 '18 at 7:07
@Cyrus: Thx, I edited my question and added the expected output.
– hornetbzz
Dec 29 '18 at 19:29
@linuxfan: I did not say there is an error. I said I'm looking for a more elegant solution than my uggly pipedgrep
andsed
sequence and that I'd like to understand how to appropriately replacetr n
by stg else, w/o using awhile, read
loop. Thx.
– hornetbzz
Dec 29 '18 at 19:32
1
1
ssid=$(awk '/PHPSESSID/{printf("%s",$NF)}' file)
?– Cyrus
Dec 29 '18 at 6:06
ssid=$(awk '/PHPSESSID/{printf("%s",$NF)}' file)
?– Cyrus
Dec 29 '18 at 6:06
Please add your desired output for that sample input to your question.
– Cyrus
Dec 29 '18 at 6:06
Please add your desired output for that sample input to your question.
– Cyrus
Dec 29 '18 at 6:06
I ran your command
ssid=$(...
, both on command line and from a script, but didn't receive any error or warning. And I don't understand that substitution about n... what I am missing?– linuxfan
Dec 29 '18 at 7:07
I ran your command
ssid=$(...
, both on command line and from a script, but didn't receive any error or warning. And I don't understand that substitution about n... what I am missing?– linuxfan
Dec 29 '18 at 7:07
@Cyrus: Thx, I edited my question and added the expected output.
– hornetbzz
Dec 29 '18 at 19:29
@Cyrus: Thx, I edited my question and added the expected output.
– hornetbzz
Dec 29 '18 at 19:29
@linuxfan: I did not say there is an error. I said I'm looking for a more elegant solution than my uggly piped
grep
and sed
sequence and that I'd like to understand how to appropriately replace tr n
by stg else, w/o using a while, read
loop. Thx.– hornetbzz
Dec 29 '18 at 19:32
@linuxfan: I did not say there is an error. I said I'm looking for a more elegant solution than my uggly piped
grep
and sed
sequence and that I'd like to understand how to appropriately replace tr n
by stg else, w/o using a while, read
loop. Thx.– hornetbzz
Dec 29 '18 at 19:32
add a comment |
2 Answers
2
active
oldest
votes
It looks like you're trying to get rid of the newlines in the output from grep
, but turning them into nulls doesn't do this. Nulls aren't visible in your terminal, but are still there and (like many other nonprinting characters) will wreak havoc if they get treated as part of your actual data. If you want to get rid of the newlines, just tell tr
to delete them for you with ... | tr -d 'n'
. But if you're trying to get the PHPSESSID
value from a Netscape-format cookie file, there's a much much better way:
ssid=$(awk '($6 == "PHPSESSID") {print $7}' path/sessionFile)
This looks for "PHPSESSID" only in the sixth field (not in e.g. the path or cookie values -- both places it could legally appear), and specifically prints the seventh field of matching lines (not just anything after "PHPSESSID" that happens to be a digit or lowercase letter).
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
add a comment |
You could also try this, if you don't want to use awk:
ssid=$(grep -P 'bPHPSESSIDb' you_cookies_file)
echo $ssid # for debug only
which outputs something like
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
Then with cut(1) extract the relevant field:
echo $ssid |cut -d" " -f7
which outputs
1hjs18icittvqvpa4tm2lv9b12
Of course you should capture the last echo
.
UPDATE
If you don't want to use cut, it is possible to emulate it with:
echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7)
Demonstration to capture in a variable:
$ field=$(echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7))
$ echo $field
1hjs18icittvqvpa4tm2lv9b12
$
Another way is to use positional parameters passing the string to a function which then refers to $7. Perhaps cleaner. Otherwise, you can use an array:
array=($(echo $ssid))
echo ${array[6]} # outputs the 7th field
It should also be possible to use regular expressions and/or string manipulation is bash, but they seem a little more difficult to me.
Thx, works like a charm, but I'd forgot to mention that I also like to avoid usingcut
. Thx again.
– hornetbzz
Dec 29 '18 at 19:37
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967085%2felegant-way-to-replace-tr-n-0-null-byte-generating-warnings-at-runtime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you're trying to get rid of the newlines in the output from grep
, but turning them into nulls doesn't do this. Nulls aren't visible in your terminal, but are still there and (like many other nonprinting characters) will wreak havoc if they get treated as part of your actual data. If you want to get rid of the newlines, just tell tr
to delete them for you with ... | tr -d 'n'
. But if you're trying to get the PHPSESSID
value from a Netscape-format cookie file, there's a much much better way:
ssid=$(awk '($6 == "PHPSESSID") {print $7}' path/sessionFile)
This looks for "PHPSESSID" only in the sixth field (not in e.g. the path or cookie values -- both places it could legally appear), and specifically prints the seventh field of matching lines (not just anything after "PHPSESSID" that happens to be a digit or lowercase letter).
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
add a comment |
It looks like you're trying to get rid of the newlines in the output from grep
, but turning them into nulls doesn't do this. Nulls aren't visible in your terminal, but are still there and (like many other nonprinting characters) will wreak havoc if they get treated as part of your actual data. If you want to get rid of the newlines, just tell tr
to delete them for you with ... | tr -d 'n'
. But if you're trying to get the PHPSESSID
value from a Netscape-format cookie file, there's a much much better way:
ssid=$(awk '($6 == "PHPSESSID") {print $7}' path/sessionFile)
This looks for "PHPSESSID" only in the sixth field (not in e.g. the path or cookie values -- both places it could legally appear), and specifically prints the seventh field of matching lines (not just anything after "PHPSESSID" that happens to be a digit or lowercase letter).
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
add a comment |
It looks like you're trying to get rid of the newlines in the output from grep
, but turning them into nulls doesn't do this. Nulls aren't visible in your terminal, but are still there and (like many other nonprinting characters) will wreak havoc if they get treated as part of your actual data. If you want to get rid of the newlines, just tell tr
to delete them for you with ... | tr -d 'n'
. But if you're trying to get the PHPSESSID
value from a Netscape-format cookie file, there's a much much better way:
ssid=$(awk '($6 == "PHPSESSID") {print $7}' path/sessionFile)
This looks for "PHPSESSID" only in the sixth field (not in e.g. the path or cookie values -- both places it could legally appear), and specifically prints the seventh field of matching lines (not just anything after "PHPSESSID" that happens to be a digit or lowercase letter).
It looks like you're trying to get rid of the newlines in the output from grep
, but turning them into nulls doesn't do this. Nulls aren't visible in your terminal, but are still there and (like many other nonprinting characters) will wreak havoc if they get treated as part of your actual data. If you want to get rid of the newlines, just tell tr
to delete them for you with ... | tr -d 'n'
. But if you're trying to get the PHPSESSID
value from a Netscape-format cookie file, there's a much much better way:
ssid=$(awk '($6 == "PHPSESSID") {print $7}' path/sessionFile)
This looks for "PHPSESSID" only in the sixth field (not in e.g. the path or cookie values -- both places it could legally appear), and specifically prints the seventh field of matching lines (not just anything after "PHPSESSID" that happens to be a digit or lowercase letter).
answered Dec 29 '18 at 7:14
Gordon DavissonGordon Davisson
68.1k97793
68.1k97793
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
add a comment |
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
Thx for the helpful explanation.
– hornetbzz
Dec 29 '18 at 19:36
add a comment |
You could also try this, if you don't want to use awk:
ssid=$(grep -P 'bPHPSESSIDb' you_cookies_file)
echo $ssid # for debug only
which outputs something like
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
Then with cut(1) extract the relevant field:
echo $ssid |cut -d" " -f7
which outputs
1hjs18icittvqvpa4tm2lv9b12
Of course you should capture the last echo
.
UPDATE
If you don't want to use cut, it is possible to emulate it with:
echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7)
Demonstration to capture in a variable:
$ field=$(echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7))
$ echo $field
1hjs18icittvqvpa4tm2lv9b12
$
Another way is to use positional parameters passing the string to a function which then refers to $7. Perhaps cleaner. Otherwise, you can use an array:
array=($(echo $ssid))
echo ${array[6]} # outputs the 7th field
It should also be possible to use regular expressions and/or string manipulation is bash, but they seem a little more difficult to me.
Thx, works like a charm, but I'd forgot to mention that I also like to avoid usingcut
. Thx again.
– hornetbzz
Dec 29 '18 at 19:37
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
add a comment |
You could also try this, if you don't want to use awk:
ssid=$(grep -P 'bPHPSESSIDb' you_cookies_file)
echo $ssid # for debug only
which outputs something like
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
Then with cut(1) extract the relevant field:
echo $ssid |cut -d" " -f7
which outputs
1hjs18icittvqvpa4tm2lv9b12
Of course you should capture the last echo
.
UPDATE
If you don't want to use cut, it is possible to emulate it with:
echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7)
Demonstration to capture in a variable:
$ field=$(echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7))
$ echo $field
1hjs18icittvqvpa4tm2lv9b12
$
Another way is to use positional parameters passing the string to a function which then refers to $7. Perhaps cleaner. Otherwise, you can use an array:
array=($(echo $ssid))
echo ${array[6]} # outputs the 7th field
It should also be possible to use regular expressions and/or string manipulation is bash, but they seem a little more difficult to me.
Thx, works like a charm, but I'd forgot to mention that I also like to avoid usingcut
. Thx again.
– hornetbzz
Dec 29 '18 at 19:37
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
add a comment |
You could also try this, if you don't want to use awk:
ssid=$(grep -P 'bPHPSESSIDb' you_cookies_file)
echo $ssid # for debug only
which outputs something like
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
Then with cut(1) extract the relevant field:
echo $ssid |cut -d" " -f7
which outputs
1hjs18icittvqvpa4tm2lv9b12
Of course you should capture the last echo
.
UPDATE
If you don't want to use cut, it is possible to emulate it with:
echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7)
Demonstration to capture in a variable:
$ field=$(echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7))
$ echo $field
1hjs18icittvqvpa4tm2lv9b12
$
Another way is to use positional parameters passing the string to a function which then refers to $7. Perhaps cleaner. Otherwise, you can use an array:
array=($(echo $ssid))
echo ${array[6]} # outputs the 7th field
It should also be possible to use regular expressions and/or string manipulation is bash, but they seem a little more difficult to me.
You could also try this, if you don't want to use awk:
ssid=$(grep -P 'bPHPSESSIDb' you_cookies_file)
echo $ssid # for debug only
which outputs something like
#HttpOnly_127.0.0.1 FALSE / FALSE 0 PHPSESSID 1hjs18icittvqvpa4tm2lv9b12
Then with cut(1) extract the relevant field:
echo $ssid |cut -d" " -f7
which outputs
1hjs18icittvqvpa4tm2lv9b12
Of course you should capture the last echo
.
UPDATE
If you don't want to use cut, it is possible to emulate it with:
echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7)
Demonstration to capture in a variable:
$ field=$(echo $ssid | (read a1 b2 c3 d4 e5 f6 g7; echo $g7))
$ echo $field
1hjs18icittvqvpa4tm2lv9b12
$
Another way is to use positional parameters passing the string to a function which then refers to $7. Perhaps cleaner. Otherwise, you can use an array:
array=($(echo $ssid))
echo ${array[6]} # outputs the 7th field
It should also be possible to use regular expressions and/or string manipulation is bash, but they seem a little more difficult to me.
edited Dec 30 '18 at 6:45
answered Dec 29 '18 at 7:44
linuxfanlinuxfan
2,1082720
2,1082720
Thx, works like a charm, but I'd forgot to mention that I also like to avoid usingcut
. Thx again.
– hornetbzz
Dec 29 '18 at 19:37
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
add a comment |
Thx, works like a charm, but I'd forgot to mention that I also like to avoid usingcut
. Thx again.
– hornetbzz
Dec 29 '18 at 19:37
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
Thx, works like a charm, but I'd forgot to mention that I also like to avoid using
cut
. Thx again.– hornetbzz
Dec 29 '18 at 19:37
Thx, works like a charm, but I'd forgot to mention that I also like to avoid using
cut
. Thx again.– hornetbzz
Dec 29 '18 at 19:37
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
@hornetbzz I added some ideas... hope it helps.
– linuxfan
Dec 30 '18 at 6:48
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
yes it does and I like the array coding style. . Thx again.
– hornetbzz
Dec 31 '18 at 10:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967085%2felegant-way-to-replace-tr-n-0-null-byte-generating-warnings-at-runtime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
ssid=$(awk '/PHPSESSID/{printf("%s",$NF)}' file)
?– Cyrus
Dec 29 '18 at 6:06
Please add your desired output for that sample input to your question.
– Cyrus
Dec 29 '18 at 6:06
I ran your command
ssid=$(...
, both on command line and from a script, but didn't receive any error or warning. And I don't understand that substitution about n... what I am missing?– linuxfan
Dec 29 '18 at 7:07
@Cyrus: Thx, I edited my question and added the expected output.
– hornetbzz
Dec 29 '18 at 19:29
@linuxfan: I did not say there is an error. I said I'm looking for a more elegant solution than my uggly piped
grep
andsed
sequence and that I'd like to understand how to appropriately replacetr n
by stg else, w/o using awhile, read
loop. Thx.– hornetbzz
Dec 29 '18 at 19:32