How to Duplicate String in Bash

How to duplicate string in bash?

This should work:

str="kallel"
str2="${str}"
while (( ${#str2} < 20 ))
do
str2="${str2}${str}"
done
str2="${str2:0:20}"

How to duplicate strings in each line using bash?

You could call sed:

sed -r 's/(.*)/\1 \1/' dupcol.csv

The .* says to match any character on a line, repeatedly. The ( ) around it says to store the match in the first register (\1). Then the full match is output twice.

I just usually default to using sed’s -r option to make for cleaner (extended) regexes. However, omitting the -r enables & to match the full pattern. So a simpler version of this special case where you want to keep the full match is:

sed -r 's/.*/& &/' dupcol.csv

If you want to change the file in-place, add the -i option.

Duplicate of string in shell script

Thanks to user1934428 I was able to think about array elements representation in a shell. I found a simple solution.
I hope it can be helpful for somebody who will also be looking for an answer.

SOLUTION:

In my case, I wrote "${maven_params[@]}" where [@] represents all arguments separated from each other. I had to use [*] instead. It provides using array elements in a row like $1$2..etc.

"$@" expands each element as a separate argument, while "$*" expands to the args merged into one argument

Find duplicate items in string


s="4.2.2.2 8.8.8.8 4.2.2.2 4.2.2.2 8.8.8.8 8.8.8.8"
n=`echo $s | tr " " "\n" | wc -l`
nuniq=`echo $s | tr " " "\n" | sort | uniq | wc -l`
[ $n -eq $nuniq ] || echo "we've got duplicates"

or

echo $s | tr " " "\n" | sort | uniq -c | grep -qv '^ *1 ' && echo "duplicates!"

How can I repeat a character in Bash?

You can use:

printf '=%.0s' {1..100}

How this works:

Bash expands {1..100} so the command becomes:

printf '=%.0s' 1 2 3 4 ... 100

I've set printf's format to =%.0s which means that it will always print a single = no matter what argument it is given. Therefore it prints 100 =s.

How to replace a duplicate string with counter appended values in multiple files in linux command line

Your question still isn't clear but this MAY be what you're looking for:

$ awk -v str='"string"' '
BEGIN { lgth = length(str) }
pos=index($0,str) {
$0 = substr($0,1,pos+lgth-1) cnt substr($0,pos+lgth)
cnt++
}
1' file{1,2,3,4}
some text
"string" here
some text
"string"1 here
some more
text "string"2
why "string"3
some text
why here
some more
text pttn
why pttn
some "string"4
no here

Just add -i inplace (with GNU awk) for it to change the input files instead of printing output. The above assumes you need a literal string match and that string doesn't need to be separated from other text by spaces, punctuition or anything else.



Related Topics



Leave a reply



Submit