awk

Arithmetic calculation in shell scripting-bash

◇◆丶佛笑我妖孽 提交于 2020-01-17 08:41:16
问题 I have an input notepad file as shown below: sample input file: vegetables and rates kg rate total Tomato 4 50 100 potato 2 60 120 Beans 3 80 240 Overalltotal: (100+120++240) = 460 I need to multiply the column 2 and column 3 and check the total if it is right and the overall total as well. If that's not right we need to print in the same file as an error message as shown below enter code here sample output file: vegetables and rates kg rate vegtotal Tomato 4 50 200 potato 2 60 120 Beans 3 80

tcl extra characters after close-brace

二次信任 提交于 2020-01-17 08:21:22
问题 i have this error Tcl error : extra characters after close-brace proc exact {nick host handle channel text} { global db_handle network; set size exec curl -3 --ftp-ssl -k ftp://xxx:xxx@192.210.0.8:2300/source/ | grep \\.r | awk '{print $5}'| awk '{ SUM += $1} END { print SUM/1024/1024 }' putnow "PRIVMSG #chnnel :source has $size" } 回答1: Per the exec(n) man page you need to replace single quotes with curly braces. You also need [] around exec to invoke it: set size [exec curl -s -3 --ftp-ssl

Using awk how do I print all lines containing duplicates of specific columns?

微笑、不失礼 提交于 2020-01-17 06:56:34
问题 Input: a;3;c;1 a;4;b;2 a;5;c;1 Output: a;3;c;1 a;5;c;1 Hence, all lines which have duplicates of columns 1,3 and 4 should be printed. 回答1: If a 2-pass approach is OK: $ awk -F';' '{key=$1 FS $3 FS $4} NR==FNR{cnt[key]++;next} cnt[key]>1' file file a;3;c;1 a;5;c;1 otherwise: $ awk -F';' ' { key=$1 FS $3 FS $4; a[key,++cnt[key]]=$0 } END { for (key in cnt) if (cnt[key] > 1) for (i=1; i<=cnt[key]; i++) print a[key,i] } ' file a;3;c;1 a;5;c;1 The output order of keys in that second script will be

sed or awk deleting lines between pattern matches, excluding the second token's line

允我心安 提交于 2020-01-17 02:50:11
问题 I have a sed command which will successfully print lines matching two patterns: sed -n '/PAGE 2/,/\x0c/p' filename.txt What I haven't figured out, is that I want it to print all the lines from the first token, up until the second token. The \x0c token is a record separator on a big flat file, and I need to keep THAT line intact. In between the two tokens, the data is completely variable, and I do not have a reliable anchor to work with. [CLARIFICATION] Right now it prints all the lines

Processing of awk with multiple variable from previous processing?

旧巷老猫 提交于 2020-01-17 01:23:07
问题 I have a Q's for awk processing, i got a file below cat test.txt /home/shhh/ abc.c /home/shhh/2/ def.c gthjrjrdj.c /kernel/sssh sarawtera.c wrawrt.h wearwaerw.h My goal is to make a full path from splitting sentences into /home/jhyoon/abc.c . This is the command I got from someone: cat test.txt | awk '/^\/.*/{path=$0}/^[a-zA-Z]/{printf("%s/%s\n",path,$0);}' It works, but I do not understand well about how do make interpret it step by step. Could you teach me how do I make interpret it? Result

awk + bash: combining arbitrary number of files

扶醉桌前 提交于 2020-01-16 13:17:23
问题 I have a script that takes a number of data files with identical layout but different data and combines a specified data column into a new file, like this: gawk '{ names[$1]= 1; data[$1,ARGIND]= $2 } END { for (i in names) print i"\t"data[i,1]"\t"data[i,2]"\t"data[i,3] }' $1 $2 $3 > combined_data.txt ... where the row IDs can be found in the first column, and the interesting data in the second column. This works nicely, but not for an arbitrary number of files. While I could simply add $4 $5

How to extract words between 2 parentheses in Unix/ Linux?

我是研究僧i 提交于 2020-01-16 04:43:09
问题 I have a SAS dataset with the list of queries as one of the variable. Below is one of the variable value: SELECT * FROM ( SELECT Table1 file2.txt file.txt QUEUES QDefinitions Parameters TRAP-Deposit-DSTran.dat.2016-08-07 FROM CS_CASE WHERE ANT_CD='FI_BASE_TENANT')t1 LEFT OUTER JOIN Table2 t2 ON t2.CASE_ID=t1.CASE_ID LEFT OUTER JOIN Table3 t3 ON t3.SERVICE_XID=t1.SERVICE_XID LEFT OUTER JOIN Table4 t4 ON t4.SERVICE_ID=t1.SERVICE_ID WHERE ( t1.CASESTATUs_CD = (NEW) OR t1.CASE_STATUS_CD = (OPEN)

Extract columns with values matching a specific pattern

烈酒焚心 提交于 2020-01-16 04:42:08
问题 I have a multi-column GTF file, where each row has different number of columns: chr1 Cufflinks exon 12659 12721 . + . gene_id "XLOC_000001"; transcript_id "TCONS_00000001"; exon_number "1"; oId "CUFF.3.1"; class_code "u"; tss_id "TSS1"; chr1 Cufflinks exon 13221 16604 . + . gene_id "XLOC_000001"; transcript_id "TCONS_00000001"; exon_number "2"; oId "CUFF.3.1"; class_code "u"; tss_id "TSS1"; chr1 Cufflinks exon 29554 30039 . + . gene_id "XLOC_000002"; transcript_id "TCONS_00000002"; exon

awk的使用方法

守給你的承諾、 提交于 2020-01-16 04:09:57
awk 使用方法 awk ‘{pattern + action}’ {filenames} 尽管操作可能会很复杂,但语法总是这样,其中 pattern 表示 AWK 在数据中查找的内容,而 action 是在找到匹配内容时所执行的一系列命令。花括号({})不需要在程序中始终出现,但它们用于根据特定的模式对一系列指令进行分组。 pattern就是要表示的正则表达式,用斜杠括起来。 awk语言的最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取信息后,才能进行其他文本操作。完整的awk脚本通常用来格式化文本文件中的信息。 通常,awk是以文件的一行为处理单位的。awk每接收文件的一行,然后执行相应的命令,来处理文本。 1.命令行方式 awk [-F field-separator] ‘commands’ input-file(s) 其中,commands 是真正awk命令,[-F域分隔符]是可选的。 input-file(s) 是待处理的文件。 在awk中,文件的每一行中,由域分隔符分开的每一项称为一个域。通常,在不指名-F域分隔符的情况下,默认的域分隔符是空格。 2.shell脚本方式 将所有的awk命令插入一个文件,并使awk程序可执行,然后awk命令解释器作为脚本的首行,一遍通过键入脚本名称来调用。 相当于shell脚本首行的:#!/bin/sh 可以换成:#!

Group files and pipe to awk command

限于喜欢 提交于 2020-01-16 03:51:07
问题 I have files in a directory; they are named using YYYY_MM_DD: -rw-r--r-- 1 root root 497186 Apr 21 13:17 2012_03_25 -rw-r--r-- 1 root root 490558 Apr 21 13:17 2012_03_26 -rw-r--r-- 1 root root 488797 Apr 21 13:17 2012_03_27 -rw-r--r-- 1 root root 316290 Apr 21 13:17 2012_03_28 -rw-r--r-- 1 root root 490081 Apr 21 13:17 2012_03_29 -rw-r--r-- 1 root root 486621 Apr 21 13:17 2012_03_30 -rw-r--r-- 1 root root 490904 Apr 21 13:17 2012_03_31 -rw-r--r-- 1 root root 491788 Apr 21 13:17 2012_04_01 -rw