A bash loop to echo all possible ASCII characters [closed] - linux

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I know how to print all letters
{a..z} and {A..Z} and {0..9}
But is there a way to print all possible ASCII Characters via a bash loop?

You don't need a loop
echo -e \\x{0..7}{{0..9},{A..F}}
It prints all chars from 0 to 127.

If it is okay to use awk:
awk 'BEGIN{for (i=32;i<127;i++) printf("%c", i)}'
Or using printf:
for((i=32;i<127;i++)) do printf "\x$(printf %x $i)"; done

use this:
for ((i=32;i<127;i++)) do printf "\\$(printf %03o "$i")"; done;printf "\n"

Related

Bash how to replace nth character in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I have a string with in a file:
"1.0.0.0.5";
I would like to replace 5 with 6 so the output should look like
"1.0.0.0.6";
could you please help me in this to achieve the output as above in bash
It seems to me that what you really want to do is increment the 5th dot-delimited field in that line.
line='"1.0.0.0.5";'
if [[ $line =~ \"([^\"]+) ]]; then
IFS="." read -ra fields <<< "${BASH_REMATCH[1]}"
((++fields[-1]))
(
IFS="."
printf '"%s";\n' "${fields[*]}"
)
fi
"1.0.0.0.6";
bash doesn't have an operator for assigning to a string index. So you'll need to concatenate the portions before and after the index you want to replace.
s=1.0.0.0.5
s=${s:0:8}6${s:9:}
${s:0:8} means the first 8 characters, and ${s:9:} means all the characters starting from index 9. So this replaces the character at index 8.
You can use a Bash regex:
s="1.0.0.0.5"
replacement="6"
[[ $s =~ ^(.*\.)[^.]*$ ]]
echo "${BASH_REMATCH[1]}$replacement"
Prints:
1.0.0.0.6

How to represent formatted printf using echo in bash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What is a command that produces the same output as formatted printf in bash
If the echo on your System supporting -ne then you can define a function like...
$ puts(){(echo -ne "sometext ${1}\t ${2}\n ${3}\n")}
$ puts hello world bye
sometext hello world
bye
( Typed in a bash )
For looping through the List of Arguments this is possible...
puts(){
for string in ${#}; do
echo -ne ${string}
done
}
Than you can do...
puts 0 1 "\n" 3 "\t" 4 5 6 "\n" 7 8 9 "\n"

paste data based on certain condition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
experts i have many files in a directory and these files contain some numerical numbers.
Probably not POSIX-compatible; tested on Bash 5.0. Assuming that the filenames don't contain whitespaces and have fixed length.
for file_with_a in *A.??
do
target_left=${file_with_a:0:15} # e.g., 2019__01_NDV.NT
target_right=${file_with_a:16:3} # e.g., .AS
cat $target_left[A-C]$target_right > ${target_left}_ABC$target_right
done
With mapfile aka readarray which is a bash4+ feature.
#!/usr/bin/env bash
files=([0-9][0-9][0-9][0-9]__*[ABC]*.??)
while mapfile -d '' -n3 array && ((${#array[*]} == 3)); do
if [[ ${array[0]%%_*} == ${array[1]%%_*} && ${array[0]%%_*} == ${array[2]%%_*} ]]; then
paste "${array[0]}" "${array[1]}" "${array[2]}" > "${array[0]%${array[0]:(-4)}}_ABC.${array[0]:(-2)}"
fi
done < <(printf '%s\0' "${files[#]}")

How to replace string with multiple semicolons and special characters using sed in Linux [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string "config"
and want to replace with
"server"
using sed in Linux. I tried the below one. But It did not work.
sed -i "s#$"config"#$"server"#g" setup.xml-->
How can I do that? If not sed other options are fine too.
before "config"
after "server"
One example:
sed 's/"config"/"\s\e\r\v\e\r"/' setup.xml
The replacement string has characters with special meaning in sed such as ; # and &. These will all need to be escaped and so:
sed -n 's/"config"/"\&\#115\;\&\#101\;\&\#114\;\&\#118\;\&\#101\;\&\#114\;"/p' <<< '"config"'

Substrings after 3 underscores and before a dot in Linux [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a some strings like below in Linux.
abc_def_fgh_2018_08_11.zip
I want to extract just 2018_08_11 as a variable. All the strings have the same pattern 3 underscores and some dates or characters and .zip
How can I do that?
Take a look at this parameter expansion:
$ foo='abc_def_fgh_2018_08_11.zip'
$ foo=${foo#*_*_*_} # remove the prefix
$ echo "${foo%.*}" # remove the sufix and print
2018_08_11
Or using regular expression:
$ foo='abc_def_fgh_2018_08_11.zip'
$ regex='^([^_]*_){3}(.*)\.'
$ [[ $foo =~ $regex ]] && echo "${BASH_REMATCH[2]}"
2018_08_11
BASH_REMATCH is a special array where the matches from [[ ... =~ ... ]] are assigned to.

Resources