Combine awk statement into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine awk statement into one
# 8  
Old 02-06-2015
First or last in a reversed file - my head starts spinning. How about posting in- and output samples, connecting them with some logic description and pointing to the one "Feb 2" that you want?
# 9  
Old 02-06-2015
This is ridiculous.

In addition to what RudiC has already said...

What do you mean by abort? (You do understand that the entire input file has to be read, don't you?) Are you saying you want to process to terminate as the result of a SIGABRT signal instead of terminating with an exit 0?

Explain what should happen in the corner cases. (Such as if the target string is not found.)
# 10  
Old 02-06-2015
Quote:
Originally Posted by Don Cragun
This is ridiculous.

In addition to what RudiC has already said...

What do you mean by abort? (You do understand that the entire input file has to be read, don't you?) Are you saying you want to process to terminate as the result of a SIGABRT signal instead of terminating with an exit 0?

Explain what should happen in the corner cases. (Such as if the target string is not found.)
i apologize if i wasn't clear.

when i said abort, i meant terminating with an exit code. my use of the word abort seems to have derailed this completely and that is my fault. again, my apologies.

say the content of the file is this:

Code:
Feb  1 a jakfif 
Feb  1 nahit 
Feb  2 a jakf
Feb  2 a bcaa
Feb  2 a bc
Feb  3 cna f
Feb  3 jaf af
Feb  4 uiaf aj
Feb  6 whatitoa


the code you supplied is doing this:

Code:
feb  6 whatitoa
Feb  4 uiaf aj
Feb  3 jaf af
Feb  3 cna f
Feb  2 a bc

notice how it stops at the first occurrence of the "Feb 2" string.

what i want it do is this:

Code:
Feb  6 whatitoa
Feb  4 uiaf aj
Feb  3 jaf af
Feb  3 cna f
Feb  2 a bc
Feb  2 a bcaa
Feb  2 a jakf

---------- Post updated at 09:39 PM ---------- Previous update was at 09:37 PM ----------

Quote:
Originally Posted by RudiC
First or last in a reversed file - my head starts spinning. How about posting in- and output samples, connecting them with some logic description and pointing to the one "Feb 2" that you want?

my apologies for the confusion.
# 11  
Old 02-06-2015
So you wanted the last occurrence of you pattern in the REVERSED file; not the last occurrence in the file. Try:
Code:
awk '
{	a[NR] = $0
}
!last && /Feb  2/{
	last = NR
}
END {	while(NR >= last)
		print a[NR--]
}' mail.log

You still refuse to answer the question about what should happen if the pattern you're looking for doesn't appear at all, so if I guessed wrong again about what you want in that case, you're on your own to fix it.

And, I'll repeat the usual warning... If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 02-06-2015
Quote:
Originally Posted by Don Cragun
So you wanted the last occurrence of you pattern in the REVERSED file; not the last occurrence in the file. Try:
Code:
awk '
{	a[NR] = $0
}
!last && /Feb  2/{
	last = NR
}
END {	while(NR >= last)
		print a[NR--]
}' mail.log

You still refuse to answer the question about what should happen if the pattern you're looking for doesn't appear at all, so if I guessed wrong again about what you want in that case, you're on your own to fix it.

And, I'll repeat the usual warning... If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
i will take heed of your warning.

if the pattern is not found, i'm totally ok with the code not showing anything at all.

btw, the log format is in the typical syslog format.
# 13  
Old 02-07-2015
Quote:
Originally Posted by SkySmart
i will take heed of your warning.

if the pattern is not found, i'm totally ok with the code not showing anything at all.

btw, the log format is in the typical syslog format.
I'm glad that you're OK with the code not showing anything. Unfortunately, if your pattern is not found that code prints the entire file backwards and then prints an empty line.

So why is a file in the typical syslog format named mail.log?
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