Split string with bash with symbol

Using Parameter Expansion: str=”test1@test2″ echo “${str#*@}” The # character says Remove the smallest prefix of the expansion matching the pattern. The % character means Remove the smallest suffix of the expansion matching the pattern. (So you can do “${str%@*}” to get the “test1” part.) The / character means Remove the smallest and first substring of … Read more

How to run an awk commands in Windows?

If you want to avoid including the full path to awk, you need to update your PATH variable to include the path to the directory where awk is located, then you can just type awk to run your programs. Go to Control Panel->System->Advanced and set your PATH environment variable to include “C:\Program Files (x86)\GnuWin32\bin” at … Read more

Awk consider double quoted string as one token and ignore space in between

Another alternative would be to use the FPAT variable, that defines a regular expression describing the contents of each field. Save this AWK script as parse.awk: #!/bin/awk -f BEGIN { FPAT = “([^ ]+)|(\”[^\”]+\”)” } { print $2 } Make it executable with chmod +x ./parse.awk and parse your data file as ./parse.awk data.txt: “I … Read more

Awk print string with variables

printf allows interpolation of variables. With this as the test file: $ cat file a,b,APPLE,d,MANGO,f We can use printf to achieve the output you want as follows: $ awk -F, ‘{printf “/p/%s_abc/xyz/%s_abc_def/\n”,$3,$5;}’ file /p/APPLE_abc/xyz/MANGO_abc_def/ In printf, the string %s means insert-a-variable-here-as-a-string. We have two occurrences of %s, one for $3 and one for $5.

How to initialize an array of arrays in awk?

If you have GNU awk, you can use a true multidimensional array. Although this answer uses the split() function, it most certainly doesn’t abuse it. Run like: awk -f script.awk Contents of script.awk: BEGIN { x=SUBSEP a=”Red” x “Green” x “Blue” b=”Yellow” x “Cyan” x “Purple” Colors[1][0] = “” Colors[2][0] = “” split(a, Colors[1], x) … Read more

How to compare two decimal numbers in bash/awk?

You can do it using Bash’s numeric context: if (( $(echo “$result1 > $result2” | bc -l) )); then bc will output 0 or 1 and the (( )) will interpret them as false or true respectively. The same thing using AWK: if (( $(echo “$result1 $result2” | awk ‘{print ($1 > $2)}’) )); then

Printing verbose progress from sed and awk

This is based on potong’s answer. The following code replaces ‘ll’ with ‘zz’, creates a backup file, displays the new text, and writes the change(s) into the file. $ echo hello > test $ sed -e ‘s/ll/zz/;w /dev/stdout’ -i .backup test hezzo $ cat test hezzo $ cat test.backup hello

Casting to int in awk

Most new awks have an int() function. But the method for casting documented in ‘The Awk Programming Language’ is shown as you do it, by using numericValue and +0. I don’t have the book handy, but I think you can also cast for float value by using +0.0. I hope this helps.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)