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

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"

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

shell script Key value compare 2 files [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
Need help on shell script.
file1.txt
ABC1:10
ABC2:20
ABC3:15
file2.txt
ABC1:20
ABC2:10
ABC3:10
I have to compare the 2 files file1.txt and file2.txt .
when key matches from both files, We have to verify the value is greater or equal for that key compared to first file.
when ABC1 matches in 2 files it's value is compared. 10 (ABC1 in fil1.txt) is less than 20 (ABC2 in file2.txt) it shouldnt print, for second ABC2 20 is greater than 10 it has to print in output
when i run the shell script, it has to show below output.
ABC2:20
ABC3:15
I think you're looking for something like this?
#!/bin/bash
for line1 in $(cat $1)
do
key=$(echo $line1 | sed -E 's/(.+):.+/\1/')
val1=$(echo $line1 | sed -E 's/.+:(.+)/\1/')
for line2 in $(grep $key $2)
do
val2=$(echo $line2 | sed -E 's/.+:(.+)/\1/')
if (( $val2 <= $val1 ))
then
echo $line1
fi
done
done
If you save this as cmp.sh and make it executable then you can compare your files with
$ ./cmp.sh file1.txt file2.txt

A bash loop to echo all possible ASCII characters [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 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"

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.

Linux rename time stamped file [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 7 years ago.
Improve this question
i have some files with timestamp (XXX_20160125-17.dmp) and i want to rename to XXX_20160124.dmp (-1 day, and only YYYYMMDD).
I try a few things but doesn't work. Thanks.
ls *.dmp |
perl -lne '
m/(.*_)(\d\d\d\d)(\d\d)(\d\d).*(\.dmp)/;
chomp($d = qx(date -d"$2-$3-$4 - 1 day" +%Y%m%d));
#rename $_, "$1$d$5" or die "rename $_ -> $1$d$5: $!\n";
print "mv $_ $1$d$5";
'
This generates commands like:
mv XXX_20160101-19.dmp XXX_20151231.dmp
mv XXX_20160125-17.dmp XXX_20160124.dmp
Assuming the filenames have no spaces, you can pipe the result into bash.
Or uncomment the 'rename' stmt in the perl script.

Resources