Remove the last-occured lines of patterns - bash
I want to exclude/delete the last line of pattern {n}{n}{n}.log for each possible 3-digit numbers. Each lines end with a sample pattern "123.log".
Sample input file:
aaaa116.log
a112.log
aaa112.log
a113.log
aaaaa112.log
aaa113.log
aa112.log
aaa116.log
a113.log
aaaaa116.log
aaa113.log
aa114.log
Output file:
aaaa116.log
a112.log
aaa112.log
a113.log
aaaaa112.log
aaa113.log
aaa116.log
a113.log
How could this be performed by bash scripting?
It is fairly simple to remove the last matching line in awk without retaining order.
awk -F'[^0-9]+' '/[0-9]+\.log$/ {
t = $(NF - 1);
if (t in a)
print a[t];
a[t] = $0;
}'
To keep the output ordered is more complicated, and requires more memory.
awk -F'[^0-9]+' '/[0-9]+\.log$/ {
t = $(NF - 1);
a[++i] = $0;
b[$0] = t;
c[t] = i;
}
END {
for (n = 1; n <= i; n++)
if (n != c[b[a[n]]])
print a[n];
}'
To pass through non-matching lines in the first example a next statement can be added to the action, and a pattern of 1 can be appended. For the second example assignment into array a can be moved to its own action.
Probably awk would be the easiest tool for this. For example, this one-liner
tac file | awk 'match($0, /[0-9]{3}.log/,a) && a[0] in b; {b[a[0]]}' | tac
produces the requested output for the sample input. This does not require the entire file to be stored in memory.
Change the regular expression to suit your specific needs.
$ awk '{k=substr($0,length()-7)} NR==FNR{n[k]=NR;next} FNR!=n[k]' file file
aaaa116.log
a112.log
aaa112.log
a113.log
aaaaa112.log
aaa113.log
aaa116.log
a113.log
Related
Replace values in text file for one batch with AWK and increment subsequent value from last one
I have the following in a text file called data.txt &st=1000&type=rec&uniId=5800000000&acceptCode=1000&drainNel=supp& &st=1100&type=rec&uniId=5800000000&acceptCode=1000&drainNel=supp& &st=4100&type=rec&uniId=6500000000&acceptCode=4100&drainNel=ured& &st=4200&type=rec&uniId=6500000000&acceptCode=4100&drainNel=iris& &st=4300&type=rec&uniId=6500000000&acceptCode=4100&drainNel=iris& &st=8300&type=rec&uniId=7700000000&acceptCode=8300&drainNel=teef& 1) Script will take an input argument in the form of a number, e.g: 979035210000000098 2) I want to replace all the text value for uniId=xxxxxxxxxx with the given long number passed in the argument to script. IMPORTANT: if uniID is same, it will replace same value for all of them. (In this case, first two lines are same, then next three lines are same, then last one is same) For the next batch, it will replace + increment (5,000,000,000) from last one Ignore all other fields and they should not be modified. So essentially doing this: ./script.sh 979035210000000098 .. still confused? Well, the final result could be this: &st=1000&type=rec&uniId=979035210000000098&acceptCode=1000&drainNel=supp& &st=1100&type=rec&uniId=979035210000000098&acceptCode=1000&drainNel=supp& &st=4100&type=rec&uniId=979035215000000098&acceptCode=4100&drainNel=ured& &st=4200&type=rec&uniId=979035215000000098&acceptCode=4100&drainNel=iris& &st=4300&type=rec&uniId=979035215000000098&acceptCode=4100&drainNel=iris& &st=8300&type=rec&uniId=979035220000000098&acceptCode=8300&drainNel=teef& This ^ should be REPLACED and applied to tempfile datanew.txt - not just print on screen. An AWK script exists which does replacement for &st=xxx and for &acceptCode=xxx but perhaps I can reuse, not able to get it working as I expect? # $./script.sh [STARTCOUNT] < data.txt > datanew.txt # $ mv -f datanew.txt data.txt awk -F '&' -v "cnt=${1:-10000}" -v 'OFS=&' \ 'NR == 1 { ac = cnt; uni = $4; } NR > 1 && $4 == uni { cnt += 100 } $4 != uni { cnt += 5000000000; ac = cnt; uni = $4 } { $2 = "st=" cnt; $5 = "acceptCode=" ac; print }'
Using gnu awk you may use this: awk -M -i inplace -v num=979035210000000098 'BEGIN{FS=OFS="&"} !seen[$4]++{p = (NR>1 ? p+5000000000 : num)} {$4="uniId=" p} 1' file &st=1000&type=rec&uniId=979035210000000098&acceptCode=1000&drainNel=supp& &st=1100&type=rec&uniId=979035210000000098&acceptCode=1000&drainNel=supp& &st=4100&type=rec&uniId=979035215000000098&acceptCode=4100&drainNel=ured& &st=4200&type=rec&uniId=979035215000000098&acceptCode=4100&drainNel=iris& &st=4300&type=rec&uniId=979035215000000098&acceptCode=4100&drainNel=iris& &st=8300&type=rec&uniId=979035220000000098&acceptCode=8300&drainNel=teef& Options -M or --bignum forces arbitrary precision arithmetic on numbers in gnu awk.
Average of first ten numbers of text file using bash
I have a file of two columns. The first column is dates and the second contains a corresponding number. The two commas are separated by a column. I want to take the average of the first three numbers and print it to a new file. Then do the same for the 2nd-4th number. Then 3rd-5th and so on. For example: File1 date1,1 date2,1 date3,4 date4,1 date5,7 Output file 2 2 4 Is there any way to do this using awk or some other tool?
Input akshay#db-3325:/tmp$ cat file.txt date1,1 date2,1 date3,4 date4,1 date5,7 akshay#db-3325:/tmp$ awk -v n=3 -v FS=, '{ x = $2; i = NR % n; ma += (x - q[i]) / n; q[i] = x; if(NR>=n)print ma; }' file.txt 2 2 4 OR below one useful for plotting and keeping reference axis (in your case date) at center of average point Script akshay#db-3325:/tmp$ cat avg.awk BEGIN { m=int((n+1)/2) } {L[NR]=$2; sum+=$2} NR>=m {d[++i]=$1} NR>n {sum-=L[NR-n]} NR>=n{ a[++k]=sum/n } END { for (j=1; j<=k; j++) print d[j],a[j] # remove d[j], if you just want values only } Output akshay#db-3325:/tmp$ awk -v n=3 -v FS=, -v OFS=, -f avg.awk file.txt date2,2 date3,2 date4,4
$ awk -F, '{a[NR%3]=$2} (NR>=3){print (a[0]+a[1]+a[2])/3}' file 2 2 4 Add a little bit math tricks here, set $2 to a[NR%3] for each record. So the value in each element would be updated cyclically. And the sum of a[0], a[1], a[2] would be the sum of past 3 numbers.
updated based on the changes made due to the helpful feedback from Ed Morton here's a quick and dirty script to do what you've asked for. It doesn't have much flexibility in it but you can easily figure out how to extend it. To run save it into a file and execute it as an awk script either with a shebang line or by calling awk -f // { Numbers[NR]=$2; if ( NR >= 3 ) { printf("%i\n", (Numbers[NR] + Numbers[NR-1] + Numbers[NR-2])/3) } } BEGIN { FS="," } Explanation: Line 1: Match all lines, "/" is the match operator and in this case we have an empty match which means "do this thing on every line". Line 3: Use the Record Number (NR) as the key and store the value from column 2 Line 4: If we have 3 or more values read from the file Line 5: Do the maths and print as an integer BEGIN block: Change the Field Separator to a comma ",".
Storing Mulitdimensional array using awk
My input file is a|b|c|d w|r|g|h i want to store the value in array like a[1,1] = a a[1,2] = b a[2,1] = w Kindly suggest in any way to achieve this in awk bash. I have two i/p files and need to do field level validation.
Like this awk -F'|' '{for(i=1;i<=NF;i++)a[NR,i]=$i} END {print a[1,1],a[2,2]}' file Output a r
This parses the file into an awk array: awk -F \| '{ for(i = 1; i <= NF; ++i) a[NR,i] = $i }' filename You'll have to add code that uses the array for this to be of any use, of course. Since you didn't say what you wanted to do with the array once it is complete (after the pass over the file), this is all the answer i can give you.
You're REALLY going to want to get/use gawk 4.* if you're using multi-dimensional arrays as that's the only awk that supports them. When you write: a[1,2] in any awk you are actually creating a psedudo-multi-dimensional array which is a 1-dimensional array indexed by the string formed by the concatenation of 1 SUBSEP 2 where SUBSEP is a control char that's unlikely to appear in your input. In GNU awk 4.* you can do: a[1][2] (note the different syntax) and that populates an actual multi-dimentional array. Try this to see the difference: $ cat tst.awk BEGIN { SUBSEP=":" # just to make it visible when printing oneD[1,2] = "a" oneD[1,3] = "b" twoD[1][2] = "c" twoD[1][3] = "d" for (idx in oneD) { print "oneD", idx, oneD[idx] } print "" for (idx1 in twoD) { print "twoD", idx1 for (idx2 in twoD[idx1]) { # you CANNOT do this with oneD print "twoD", idx1, idx2, twoD[idx1][idx2] } } } $ awk -f tst.awk oneD 1:2 a oneD 1:3 b twoD 1 twoD 1 2 c twoD 1 3 d
Comparison shell script for large text/csv files - improvement needed
My task is the following - I have two CSV files: File 1 (~9.000.000 records): type(text),number,status(text),serial(number),data1,data2,data3 File 2 (~6000 records): serial_range_start(number),serial_range_end(number),info1(text),info2(text) The goal is to add to each entry in File 1 the corresponding info1 and info2 from File 2: type(text),number,status(text),serial(number),data1,data2,data3,info1(text),info2(text) I use the following script: #!/bin/bash USER="file1.csv" RANGE="file2.csv" for SN in `cat $USER | awk -F , '{print $4}'` do #echo \n "$SN" for LINE in `cat $RANGE` do i=`grep $LINE $RANGE| awk -F, '{print $1}'` #echo \n "i= " "$i" j=`grep $LINE $RANGE| awk -F, '{print $2}'` #echo \n "j= " "$j" k=`echo $SN` #echo \n "k= " "$k" if [ $k -ge $i -a $k -le $j ] then echo `grep $SN $USER`,`grep $i $RANGE| cut -d',' -f3-4` >> result.csv break #else #echo `grep $SN $USER`,`echo 'N/A','N/A'` >> result.csv fi done done The script works rather good on small files but I'm sure there is a way to optimize it because I am running it on an i5 laptop with 4GB of RAM. I am a newbie in shell scripting and I came up with this script after hours and hours of research, trial and error but now I am out of ideas. note: not all the info in file 1 can be found in file. Thank you! Adrian. FILE EXAMPLES and additional info: File 1 example: prep,28620026059,Active,123452010988759,No,No,No post,28619823474,Active,123453458466109,Yes,No,No post,28619823474,Inactive,123453395270941,Yes,Yes,Yes File 2 example: 123452010988750,123452010988759,promo32,1.11 123453458466100,123453458466199,promo64,2.22 123450000000000,123450000000010,standard128,3.333 Result example (currently): prep,28620026059,Active,123452010988759,No,No,No,promo32,1.11 post,28619823474,Active,123453458466109,Yes,No,No,promo64,2.22 Result example (nice to have): prep,28620026059,Active,123452010988759,No,No,No,promo32,1.11 post,28619823474,Active,123453458466109,Yes,No,No,promo64,2.22 post,28619823474,Inactive,123453395270941,Yes,Yes,Yes,NA,NA File 1 is sorted after the 4th column File 2 is sorted after the first column. File 2 does not have ranges that overlap Not all the info in file 1 can be found in a range in file 2 Thanks again! LE: The script provided by Jonathan seems to have an issue on some records, as follows: file 2: 123456780737000,123456780737012,ONE 32,1.11 123456780016000,123456780025999,ONE 64,2.22 file 1: Postpaid,24987326427,Active,123456780737009,Yes,Yes,Yes Postpaid,54234564719,Active,123456780017674,Yes,Yes,Yes The output is the following: Postpaid,24987326427,Active,123456780737009,Yes,Yes,Yes,ONE 32,1.11 Postpaid,54234564719,Active,123456780017674,Yes,Yes,Yes,ONE 32,1.11 and it should be: Postpaid,24987326427,Active,123456780737009,Yes,Yes,Yes,ONE 32,1.11 Postpaid,54234564719,Active,123456780017674,Yes,Yes,Yes,ONE 64,2.22 It seems that it returns 0 and writes the info on first record from file2...
I think this will work reasonably well: awk -F, 'BEGIN { n = 0; OFS = ","; } NR==FNR { lo[n] = $1; hi[n] = $2; i1[n] = $3; i2[n] = $4; n++ } NR!=FNR { for (i = 0; i < n; i++) { if ($4 >= lo[i] && $4 <= hi[i]) { print $1, $2, $3, $4, $5, $6, $7, i1[i], i2[i]; break; } } }' file2 file1 Given file2 containing: 1,10,xyz,pqr 11,20,abc,def 21,30,ambidextrous,warthog and file1 containing: A,123,X2,1,data01_1,data01_2,data01_3 A,123,X2,2,data02_1,data02_2,data02_3 A,123,X2,3,data03_1,data03_2,data03_3 A,123,X2,4,data04_1,data04_2,data04_3 A,123,X2,5,data05_1,data05_2,data05_3 A,123,X2,6,data06_1,data06_2,data06_3 A,123,X2,7,data07_1,data07_2,data07_3 A,123,X2,8,data08_1,data08_2,data08_3 A,123,X2,9,data09_1,data09_2,data09_3 A,123,X2,10,data10_1,data10_2,data10_3 A,123,X2,11,data11_1,data11_2,data11_3 A,123,X2,12,data12_1,data12_2,data12_3 A,123,X2,13,data13_1,data13_2,data13_3 A,123,X2,14,data14_1,data14_2,data14_3 A,123,X2,15,data15_1,data15_2,data15_3 A,123,X2,16,data16_1,data16_2,data16_3 A,123,X2,17,data17_1,data17_2,data17_3 A,123,X2,18,data18_1,data18_2,data18_3 A,123,X2,19,data19_1,data19_2,data19_3 A,223,X2,20,data20_1,data20_2,data20_3 A,223,X2,21,data21_1,data21_2,data21_3 A,223,X2,22,data22_1,data22_2,data22_3 A,223,X2,23,data23_1,data23_2,data23_3 A,223,X2,24,data24_1,data24_2,data24_3 A,223,X2,25,data25_1,data25_2,data25_3 A,223,X2,26,data26_1,data26_2,data26_3 A,223,X2,27,data27_1,data27_2,data27_3 A,223,X2,28,data28_1,data28_2,data28_3 A,223,X2,29,data29_1,data29_2,data29_3 the output of the command is: A,123,X2,1,data01_1,data01_2,data01_3,xyz,pqr A,123,X2,2,data02_1,data02_2,data02_3,xyz,pqr A,123,X2,3,data03_1,data03_2,data03_3,xyz,pqr A,123,X2,4,data04_1,data04_2,data04_3,xyz,pqr A,123,X2,5,data05_1,data05_2,data05_3,xyz,pqr A,123,X2,6,data06_1,data06_2,data06_3,xyz,pqr A,123,X2,7,data07_1,data07_2,data07_3,xyz,pqr A,123,X2,8,data08_1,data08_2,data08_3,xyz,pqr A,123,X2,9,data09_1,data09_2,data09_3,xyz,pqr A,123,X2,10,data10_1,data10_2,data10_3,xyz,pqr A,123,X2,11,data11_1,data11_2,data11_3,abc,def A,123,X2,12,data12_1,data12_2,data12_3,abc,def A,123,X2,13,data13_1,data13_2,data13_3,abc,def A,123,X2,14,data14_1,data14_2,data14_3,abc,def A,123,X2,15,data15_1,data15_2,data15_3,abc,def A,123,X2,16,data16_1,data16_2,data16_3,abc,def A,123,X2,17,data17_1,data17_2,data17_3,abc,def A,123,X2,18,data18_1,data18_2,data18_3,abc,def A,123,X2,19,data19_1,data19_2,data19_3,abc,def A,223,X2,20,data20_1,data20_2,data20_3,abc,def A,223,X2,21,data21_1,data21_2,data21_3,ambidextrous,warthog A,223,X2,22,data22_1,data22_2,data22_3,ambidextrous,warthog A,223,X2,23,data23_1,data23_2,data23_3,ambidextrous,warthog A,223,X2,24,data24_1,data24_2,data24_3,ambidextrous,warthog A,223,X2,25,data25_1,data25_2,data25_3,ambidextrous,warthog A,223,X2,26,data26_1,data26_2,data26_3,ambidextrous,warthog A,223,X2,27,data27_1,data27_2,data27_3,ambidextrous,warthog A,223,X2,28,data28_1,data28_2,data28_3,ambidextrous,warthog A,223,X2,29,data29_1,data29_2,data29_3,ambidextrous,warthog This uses a linear search on the list of ranges; you can write functions in awk and a binary search looking for the correct range would perform better on 6,000 entries. That part, though, is an optimization — exercise for the reader. Remember that the first rule of optimization is: don't. The second rule of optimization (for experts only) is: don't do it yet. Demonstrate that it is a problem. This code shouldn't take all that much longer than the time it takes to copy the 9,000,000 record file (somewhat longer, but not disastrously so). Note, though, that if the file1 data is sorted, the tail of the processing will take longer than the start because of the linear search. If the serial numbers are in a random order, then it will all take about the same time on average. If your CSV data has commas embedded in the text fields, then awk is no longer suitable; you need a tool with explicit support for CSV format — Perl and Python both have suitable modules. Answer to Exercise for the Reader awk -F, 'BEGIN { n = 0; OFS = ","; } NR==FNR { lo[n] = $1; hi[n] = $2; i1[n] = $3; i2[n] = $4; n++ } NR!=FNR { i = search($4) print $1, $2, $3, $4, $5, $6, $7, i1[i], i2[i]; } function search(i, l, h, m) { l = 0; h = n - 1; while (l <= h) { m = int((l + h)/2); if (i >= lo[m] && i <= hi[m]) return m; else if (i < lo[m]) h = m - 1; else l = m + 1; } return 0; # Should not get here }' file2 file1 Not all that hard to write the binary search. This gives the same result as the original script on the sample data. It has not been exhaustively tested, but appears to work. Note that the code does not really handle missing ranges in file2; it assumes that the ranges are contiguous but non-overlapping and in sorted order and cover all the values that can appear in the serial column of file1. If those assumptions are not valid, you get erratic behaviour until you fix either the code or the data.
In Unix you may use the join command (type 'man join' for more information) which can be configured to work similarly to join operation in databases. That may help you to add the information from File2 to File1.
How to grep number of unique occurrences
I understand that grep -c string can be used to count the occurrences of a given string. What I would like to do is count the number of unique occurrences when only part of the string is known or remains constant. For Example, if I had a file (in this case a log) with several lines containing a constant string and a repeating variable like so: string=value1 string=value1 string=value1 string=value2 string=value3 string=value2 Than I would like to be able to identify the number of each unique set with an output similar to the following: (ideally with a single grep/awk string) value1 = 3 occurrences value2 = 2 occurrences value3 = 1 occurrences Does anyone have a solution using grep or awk that might work? Thanks in advance!
This worked perfectly... Thanks to everyone for your comments! grep -oP "wwn=[^,]*" path/to/file | sort | uniq -c
In general, if you want to grep and also keep track of results, it is best to use awk since it performs such things in a clear manner with a very simple syntax. So for your given file I would use: $ awk -F= '/string=/ {count[$2]++} END {for (i in count) print i, count[i]}' file value1 3 value2 2 value3 1 What is this doing? -F= set the field separator to =, so that we can compute the right and left part of it. /string=/ {count[$2]++} when the pattern "string=" is found, check it! This uses an array count[] to keep track on the times the second field has appeared so far. END {for (i in count) print i, count[i]} at the end, loop through the results and print them.
Here's an awk script: #!/usr/bin/awk -f BEGIN { file = ARGV[1] while ((getline line < file) > 0) { for (i = 2; i < ARGC; ++i) { p = ARGV[i] if (line ~ p) { a[p] += !a[p, line]++ } } } for (i = 2; i < ARGC; ++i) { p = ARGV[i] printf("%s = %d occurrences\n", p, a[p]) } exit } Example: awk -f script.awk somefile ab sh Output: ab = 7 occurrences sh = 2 occurrences