awk

Can the regex matching pattern for awk be placed above the opening brace of the action line, or must it be on the same line?

笑着哭i 提交于 2020-05-28 05:46:25
问题 I'm studying awk pretty fiercely to write a git diffn implementation which will show line numbers for git diff , and I want confirmation on whether or not this Wikipedia page on awk is wrong [Update: I've now fixed this part of that Wikipedia page, but this is what it used to say]: (pattern) { print 3+2 print foobar(3) print foobar(variable) print sin(3-2) } Output may be sent to a file: (pattern) { print "expression" > "file name" } or through a pipe: (pattern) { print "expression" |

“BEGIN blocks must have an action part” error in awk script

人盡茶涼 提交于 2020-05-25 11:25:15
问题 Here is my code: #!/bin/sh filename=$(/usr/bin/find -name "INSTANCE-*.log") echo "filename is " $filename awk ' BEGIN { print "Processing file: " filename } { if($0 ~ /Starting/) { print "The bill import has been Started on "$1 " " $2 } }' $filename > report.txt When I execute it I get the following error: BEGIN blocks must have an action part My BEGIN block has a print statement so it has an action part. What am I missing here? 回答1: This happens because your opening curly brace is in the

“BEGIN blocks must have an action part” error in awk script

╄→гoц情女王★ 提交于 2020-05-25 11:24:19
问题 Here is my code: #!/bin/sh filename=$(/usr/bin/find -name "INSTANCE-*.log") echo "filename is " $filename awk ' BEGIN { print "Processing file: " filename } { if($0 ~ /Starting/) { print "The bill import has been Started on "$1 " " $2 } }' $filename > report.txt When I execute it I get the following error: BEGIN blocks must have an action part My BEGIN block has a print statement so it has an action part. What am I missing here? 回答1: This happens because your opening curly brace is in the

Filter column with awk and regexp

試著忘記壹切 提交于 2020-05-24 14:33:25
问题 I've a pretty simple question. I've a file containing several columns and I want to filter them using awk. So the column of interest is the 6th column and I want to find every string containing : starting with a number from 1 to 100 after that one "S" or a "M" again a number from 1 to 100 after that one "S" or a "M" So per example : 20S50M is ok I tried : awk '{ if($6 == '/[1-100][S|M][1-100][S|M]/') print} file.txt but it didn't work... What am I doing wrong? 回答1: This should do the trick:

Filter column with awk and regexp

℡╲_俬逩灬. 提交于 2020-05-24 14:33:01
问题 I've a pretty simple question. I've a file containing several columns and I want to filter them using awk. So the column of interest is the 6th column and I want to find every string containing : starting with a number from 1 to 100 after that one "S" or a "M" again a number from 1 to 100 after that one "S" or a "M" So per example : 20S50M is ok I tried : awk '{ if($6 == '/[1-100][S|M][1-100][S|M]/') print} file.txt but it didn't work... What am I doing wrong? 回答1: This should do the trick:

execute mkpasswd inside awk

杀马特。学长 韩版系。学妹 提交于 2020-05-24 05:17:42
问题 I am trying to use mkpasswd inside awk to compare a file field with encryption (I use Ubuntu): Execute: mkpasswd -m sha-512 word abcdefgh Output: $6$abcdefgh$SByAdlFKQWuVuMNFUL.ERj1CxsscDs.v6nR2h2cyIkM.PAEUEqaMudTk3I/yfyFeaJY/da4dJto/1wXxMCaok/ Trying: awk 'mkpasswd -m sha-512 $7 abcdefgh =="$6$abcdefgh$SByAdlFKQWuVuMNFUL.ERj1CxsscDs.v6nR2h2cyIkM.PAEUEqaMudTk3I/yfyFeaJY/da4dJto/1wXxMCaok/"' FS=: file > file1 File: 6:g:g:g:g:g:word1 7:g:g:g:g:g:word 8:g:g:g:g:g:word2 Expected output: 7:g:g:g:g

git diff with line numbers and proper code alignment/indentation

不想你离开。 提交于 2020-05-23 11:58:49
问题 I obtained this code sample from someone else here: git diff --color=always | \ gawk '{bare=$0;gsub("\033[[][0-9]*m","",bare)};\ match(bare,"^@@ -([0-9]+),[0-9]+ [+]([0-9]+),[0-9]+ @@",a){left=a[1];right=a[2];next};\ bare ~ /^(---|\+\+\+|[^-+ ])/{print;next};\ {line=gensub("^(\033[[][0-9]*m)?(.)","\\2\\1",1,$0)};\ bare~/^-/{print "-"left++ ":" line;next};\ bare~/^[+]/{print "+"right++ ":" line;next};\ {print "("left++","right++"):"line;next}' and would like to have it output properly-aligned

Remove consecutive duplicate words from a file using awk or sed

♀尐吖头ヾ 提交于 2020-05-23 11:40:08
问题 My input file looks like below: “true true, rohith Rohith; cold burn, and fact and fact good good?” Output shoud look like: "true, rohith Rohith; cold burn, and fact and fact good?" i am trying the same with awk, but couldn't able to get the desired result. awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s ",$i,FS)}{printf("\n")}' input.txt Could someone please help me here. Regards, Rohith 回答1: With GNU awk for the 4th arg to split(): $ cat tst.awk { n = split($0,words,/[^[:alpha:]]+/,seps)

keep fasta records which have 2 matches of OX values

╄→гoц情女王★ 提交于 2020-05-16 22:05:49
问题 I have a file that looks as follows : >sp|rin-1 ghsfdhjkuesl OX=10116 GN=Cdh1 PE=1 SV=1|sp|P10287|ghsfdjdeosd gdhkhs OX=10090 GN=Cdh3 PE=1 SV=2 WRDTANWLEINPETGVISTRAEMDREDSEHVKNSTYTALIIATDDGSPIATGTGTLLLVLSDVNDNAPIPEPRNMQFCQRNPKPHVITILDPDLPP >sp|erin-1 ghsfdshkd OX=10116 GN=Cdh1 PE=1 SV=1|sp|P22223|CADH3_HUMAN Cadherin-3 OX=9606 GN=CDH3 PE=1 SV=2 ESYPTYTLVVQAADLQGEGLSTTAKAVITVKDINDNAPIFNPSTYLQCAASEPCRAVFREAEVTLEAGGAEQEPGQALGKVFMGCPGQEPALFSTD >sp|n-1 ghsfd OX=10116 GN=Cdh1 PE=1 SV=1|tr|F1LMI3

keep fasta records which have 2 matches of OX values

怎甘沉沦 提交于 2020-05-16 22:05:17
问题 I have a file that looks as follows : >sp|rin-1 ghsfdhjkuesl OX=10116 GN=Cdh1 PE=1 SV=1|sp|P10287|ghsfdjdeosd gdhkhs OX=10090 GN=Cdh3 PE=1 SV=2 WRDTANWLEINPETGVISTRAEMDREDSEHVKNSTYTALIIATDDGSPIATGTGTLLLVLSDVNDNAPIPEPRNMQFCQRNPKPHVITILDPDLPP >sp|erin-1 ghsfdshkd OX=10116 GN=Cdh1 PE=1 SV=1|sp|P22223|CADH3_HUMAN Cadherin-3 OX=9606 GN=CDH3 PE=1 SV=2 ESYPTYTLVVQAADLQGEGLSTTAKAVITVKDINDNAPIFNPSTYLQCAASEPCRAVFREAEVTLEAGGAEQEPGQALGKVFMGCPGQEPALFSTD >sp|n-1 ghsfd OX=10116 GN=Cdh1 PE=1 SV=1|tr|F1LMI3