The Archives

  • 04.Apr.15
    String manipulation in Bash shell | rafacas | (0)
    I rely on Bash on much of my scripting needs although it is usually not worthy when you need to do something complicated. However, we usually think Bash limits are lower than they really are. In this post I am going to explain the surprising number of string manipulation operations that Bash supports. String Length The syntax to know the lenght of a string is: ${#string} For example: $ string=abcdefghijklmnopqrstuvwxyz $ echo ${#string} 26 Substring Extraction ${string:position} Extracts substring from $string at $position. $ string=abcdefghijklmnopqrstuvwxyz $ echo ${string:1} bcdefghijklmnopqrstuvwxyz Note that strings are zero-based indexed. If you want to extract a substring of a specific length then: ${string:position:length} Extracts $length characters from $string at $position. For example: $ echo ${string:5:3} fgh It ...
  • 11.Jun.10
    hex2bin preserving endianness in C automated | pfortuny | (0)
    I cannot help copying this snippet. Assume f is a (char *) of length L, containing an hex string like '0aabdda' (without the leading "0x", like something coming from a sha function ---or like the sha1 stored by Leopard in the password files, which is the origin of this problem). You want to transform it into the corresponding sequence of bytes (that is, assuming the string is of even length, otherwise, we add a trailing, yes, trailing, at the end, '0'). We shall store the result in t, which points to a (char *) of length L/2. The following C code ...
  • 16.Feb.09
    Perl for accents and spaces in html hrefs automated, scripts, shell | pfortuny | (0)
    Despite the title looking like spam, it uses all the relevant keywords in a problem which I guess is more common than it looks like, especially in non-English speaking countries. I had created (automatically, not by hand) a set of html, pdf and rtf files of the 477 items of a regional catalogue of industrial estate (sorry, no references because it belongs to other people and they have not published it yet). Then I made the index.html page listing and linking to all those files. It looked like a list of lines like the following one (notice that what follows looks ...
  • 15.Nov.08
    Strings in bash shell | pfortuny | (0)
    Start with a string: $ a="hi, this is my beautifull string" Positional substrings: Substring from the 4th character on: $ echo ${a:3} this is my beautifull string Substring of length 4 from the 10th character: $ echo ${a:9:4} is m Substring modification: Substitute the first instance of a substring: $ echo ${a/full/ful} hi, this is my beautiful string Same example: $ echo ${a/h/H} Hi, this is my beautifull string Substitute all the instances of a substring: $ echo ${a//hi/HI} HI, tHIs is my beautifull string Substring removal: Remove the shortest match of a substring from the start: $ echo ${a#h*i} , this is my beautifull string Same, starting at the end $ echo ${a%i*g} hi, this is my beautifull str Remove the longest match of a ...