AWK: Access captured group from line pattern

With gawk, you can use the match function to capture parenthesized groups. gawk ‘match($0, pattern, ary) {print ary[1]}’ example: echo “abcdef” | gawk ‘match($0, /b(.*)e/, a) {print a[1]}’ outputs cd. Note the specific use of gawk which implements the feature in question. For a portable alternative you can achieve similar results with match() and substr. … Read more

How can I shuffle the lines of a text file on the Unix command line or in a shell script?

You can use shuf. On some systems at least (doesn’t appear to be in POSIX). As jleedev pointed out: sort -R might also be an option. On some systems at least; well, you get the picture. It has been pointed out that sort -R doesn’t really shuffle but instead sort items according to their hash … Read more

How do I use shell variables in an awk script?

#Getting shell variables into awk may be done in several ways. Some are better than others. This should cover most of them. If you have a comment, please leave below.                                                                                    v1.5 Using -v (The best way, most portable) Use the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk … Read more

What is the difference between sed and awk? [closed]

sed is a stream editor. It works with streams of characters on a per-line basis. It has a primitive programming language that includes goto-style loops and simple conditionals (in addition to pattern matching and address matching). There are essentially only two “variables”: pattern space and hold space. Readability of scripts can be difficult. Mathematical operations … Read more

How can I do a recursive find/replace of a string with awk or sed?

find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i ‘s/subdomainA\.example\.com/subdomainB.example.com/g’ -print0 tells find to print each of the results separated by a null character, rather than a new line. In the unlikely event that your directory has files with newlines in the names, this still lets … Read more

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