Combine awk statement into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine awk statement into one
# 1  
Old 02-06-2015
Combine awk statement into one

Code:
awk '{a[NR]=$0} END {while (NR) print a[NR--]}' mail.log | awk '/Feb  2/ {print ;exit;}'


first, can this code be combined into one?

what i want this code to do is output every single line it finds up until the last occurrence of "Feb 2".

right now, this code simply outputs one line, and aborts. but i want it to output all the lines it finds up until the very last occurrence of "Feb 2".
# 2  
Old 02-06-2015
It looks like your current script is trying to print lines in reverse order. Assuming that what you want to do is print lines from mail.log from line 1 through the last line that contains Feb 2 (and print nothing if that string does not appear in the input file), you could try something like:
Code:
awk '{a[NR]=$0}
/Feb  2/{last=NR}
END {for(i=1;i<=last;i++)print a[i]}' mail.log

Since you didn't supply any sample input or desired output, this is untested. But, if I understood your requirements correctly, it should be close to what you need as long as you have room to read the entire file into memory.

As always, if you're running this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

Last edited by Don Cragun; 02-06-2015 at 06:23 PM.. Reason: Remove extraneous single quote.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-06-2015
Quote:
Originally Posted by Don Cragun
It looks like your current script is trying to print lines in reverse order. Assuming that what you want to do is print lines from mail.log from line 1 through the last line that contains Feb 2 (and print nothing if that string does not appear in the input file), you could try something like:
Code:
awk '{a[NR]=$0}
/Feb  2/{last=NR}
END {for(i=1;i<=last;i++)print a[i]}' mail.log'

Since you didn't supply any sample input or desired output, this is untested. But, if I understood your requirements correctly, it should be close to what you need as long as you have room to read the entire file into memory.

As always, if you're running this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
what i want to do is print the file in reverse order and abort after the very last line containing the string i specified is found.
# 4  
Old 02-06-2015
Reversing the order and talking about the last line matched creates an ambiguity. Making the wild assumption that you want to print the end of the file from the last line in the file that contains the pattern to the end of the file with the output printed in reverse order you should just need to change the last line of the script I suggested to:
Code:
END {if(last)for(i=NR;i>=last;i--)print a[i]}' mail.log

Again, with no sample input and expected output, this is untested.

Last edited by Don Cragun; 02-06-2015 at 06:24 PM.. Reason: Remove extraneous single quote.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 02-06-2015
Combining your (skysmart's) approach with Don Cragun's, try
Code:
awk '{a[NR]=$0} /Feb  2/ {last=NR} END {while (NR >= last) print a[NR--]}' file


Last edited by RudiC; 02-06-2015 at 06:52 PM..
This User Gave Thanks to RudiC For This Post:
# 6  
Old 02-06-2015
The difference between these two approaches is that if Feb 2 is not found in the file, my script will print nothing while RudiC's script will print the entire file in reverse order. From the discussion in this thread, I don't know which outcome is preferred.
# 7  
Old 02-06-2015
both of these commands worked great.

however, just one thing. it appears the awk code aborts immediately after the first entry containing "Feb 2" is found. i want it to abort when the last entry is found.

so, it should keep going up until the "Feb 2" string changes to something else.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combine awk scripts

Hi, Below command is working as expected, but would like to know how to club the two AWK scripts in the command into one echo -e "MMS000101S0203430A|20180412E|\nMMB0001INVESTMENT||107-86193-01-03|\nMMB0001FUND||107-86193-04-01|\nMMC9991 " | awk -F'|' -v OFS=, '/^MMC9991/{print r"|"s,t; next}... (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

Combine awk commands into one

my code: gawk 'NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"'' ${LOGFILE} | gawk '{l=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o=i; t++;} END { for(i=1; i<=NR; i++) if (o) print l; print t+=0;}' i would like to combine this into one... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

4. Shell Programming and Scripting

Combine rows with awk

I have a file of 100,000 entries that look like: chr1 980547 980667 + chr1:980547-980667 chr1 980728 980848 + chr1:980728-980848 chr1 980793 980913 + chr1:980793-980913 I am trying to reformat them to into 5 columns that are tab delineated: chr1 980547 980667 + ... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

How to combine two files with awk?

Hi, everyone! I have two files, I want to combine them as follows: File1 AAAA 23 45 AAAB 44 56 AAAC 34 65 AAAD 34 87 File2 AAAA 34 54 AAAE 34 56 Combined file AAAA 23 45 34 54 AAAB 44 56 AAAC 34 65 AAAD 34 87 AAAE 34 56 (13 Replies)
Discussion started by: xshang
13 Replies

6. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

7. Shell Programming and Scripting

combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated! I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line... (3 Replies)
Discussion started by: Cheri
3 Replies

8. Shell Programming and Scripting

combine awk and tr -d

Hi Everyone, awk 'BEGIN{print strftime("%c",1272814948)}' | tr -d '\n' how to change tr -d '\n' to be part of the awk? means awk this pchoh time, and awk also remove '\n', instead of using "|" to combine "tr" command. Thanks (2 Replies)
Discussion started by: jimmy_y
2 Replies

9. Shell Programming and Scripting

combine 2 awks statement into 1 liner

Using these 2 comands to concatenate both outputs into single file: cat testdata | awk 'BEGIN { FS="\n"; RS=""; } /<pattern1>/ {print}' > testdata1 cat testdata| awk '/<pattern2>/,EOF' >> testdata1 is it possible to combine both "awk" into 1-liner? pls advise and thanks in advance. (5 Replies)
Discussion started by: ux4me
5 Replies

10. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 Replies
Login or Register to Ask a Question