New answers tagged

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];' <<< '...
user avatar
  • 1,242
-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(_){ ...
user avatar
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 ...
user avatar
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} != &...
user avatar
  • 117k
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 ...
user avatar
  • 38.4k
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
user avatar
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 ...
user avatar
  • 90.4k
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 ...
user avatar
  • 272k
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' ...
user avatar
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 @...
user avatar
  • 18.6k
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 "$...
user avatar
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-...
user avatar

Top 50 recent answers are included