New answers tagged string
0
votes
Split string by delimiter and get N-th element
Using Raku (formerly known as Perl_6)
A=$(raku -e 'print $*IN.split("_")[1];' <<< 'one_two_three_four_five')
B=$(raku -e 'print $*IN.split("_")[3];' <<< '...
-1
votes
Awk substr index 0
the inconsistencies in substr() non-positive starting index has long been one of the criteria i've used for my own module to identify which awk variant it's running upon :
function _testawk_util20(_){
...
0
votes
grep on a variable
I would just use sed to break up the sentence for me and then compare each line in a loop because this seems like the easiest way to me (no offence to anyone here, and also I am not an expert so there ...
1
vote
Accepted
IP address string manipulation problem
I would break each octet apart, and remove any leading zeroes from each, then concatenate them together. Something like this:
str="$five_digits"
if [[ ${#str} != 5 ]] || [[ ${str:0:2} != &...
4
votes
Accepted
Awk substr index 0
From the GNU awk online documentation: 'substr() function':
If start is less than one, substr() treats it as if it was one. (POSIX
doesn’t specify what to do in this case: BWK awk acts this way, and
...
0
votes
Better way to convert IP?
With sed :
sed 's/[^.][.][^.]*$/2.252/'
ip="10.AB.C9.XYZ"
echo "$ip" | sed 's/[^.][.][^.]*$/2.252/'
10.AB.C2.252
0
votes
Better way to convert IP?
You can subtract 7 from the third octet, and replace the fourth:
echo 10.11.19.44 | awk -F. '{OFS=FS; print $1,$2,$3-7,252}'
10.11.12.252
This process would work most efficiently if you had a list of ...
3
votes
Accepted
Better way to convert IP?
If all you want to do is to replace the last digit of the third octet and the whole of the fourth octet with 2.252, then you may do that with
new_ip=${ip_main%?.*}2.252
This deletes the shortest ...
4
votes
Accepted
How to correctly line break and concatenate a string in a bash script
FYI, a better log_message in bash could be written:
exec {logfd}>> log.txt
log_message() {
local severity="$1" message
shift
for message do
printf '[%(%FT%T%z)T]-[%s] : %s\n' ...
5
votes
How to correctly line break and concatenate a string in a bash script
In your case, the problem is not the line continuation, but the fact that you used double-quotes inside your double-quoted second argument without escaping them (Note that seeing the answer by @...
1
vote
Remove certain words and set word at end of string in files
You can use GNU grep to find files that contain at least one occurrence of the pattern and GNU sed to do the substitution in-place:
ere='Get<[[:digit:]]+>\(([[:alnum:]_]+)\)'
grep -rlEZe "$...
1
vote
Loop that replaces a string only once each time after reading a different string
perl -0777 -pe 's/character = yes.*?age = \K\d+/int(18+rand(45-18+1))/ges' < file
Would replace the first occurrence of age = <digits> after character = yes with age = <some-random-number-...
Top 50 recent answers are included
Related Tags
string × 744bash × 297
shell-script × 160
sed × 116
shell × 113
awk × 83
grep × 82
text-processing × 68
regular-expression × 50
linux × 43
search × 42
variable × 38
replace × 37
files × 30
scripting × 26
array × 25
filenames × 24
zsh × 23
command-line × 19
find × 18
quoting × 17
test × 17
cut × 13
date × 12
wildcards × 12