Combine awk commands into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine awk commands into one
# 1  
Old 08-15-2016
Combine awk commands into one

my code:

Code:
gawk 'NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"'' ${LOGFILE} | gawk '{l[NR]=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o[i]=i; t++;} END { for(i=1; i<=NR; i++) if (o[i]) print l[i]; print t+=0;}'

i would like to combine this into one and have it operate exactly as the original code above.

i tried the following and it didn't work:

Code:
gawk '{NR>'"${LASTLINENUM}"' && NR<='"${LINEENDNUM}"' && l[NR]=$0;} /'"${STRING1}"'/ && /'"${STRING2}"'/ {for (i=NR-'"${BEFOREGLAF}"'; i<=NR+'"${AFTERGLAF}"'; i++) o[i]=i; t++;} END { for(i=1; i<=NR; i++) if (o[i]) print l[i]; print t+=0;}'

# 2  
Old 08-16-2016
I refuse to attempt to do 1-liners where I can't see what it is doing. And, with no sample variable values and no sample data to work on and no description of the expected output, this is obviously totally untested, but should come close to what your unreadable pipeline did:
Code:
gawk -v ll="$LASTLINENUM" -v le="$LINEENDNUM" -v s1="$STRING1" \
     -v s2="$STRING2" -v bg="$BEFOREGLAF" -v ag="$AFTERGLAF" '
NR <= ll || NR > le {
	ndel++
	next
}
{	l[NR - ndel] = $0
}
$0 ~ s1 && $0 ~ s2 {
	for(i = NR - ndel - bg; i <= NR - ndel + ag; i++)
		o[i] = i
	t++
}
END {	for(i = 1; i <= NR - ndel; i++)
		if(o[i])
			print l[i]
	print t+0
}' "$LOGFILE"

I think that the expression NR - ndel keeps track of the NR value that would have been seen in the second awk script. Without understanding what the shell variables BEFOREGLAF and AFTERGLAF are doing in your script, I have no idea whether or not any of the ndel calculations are needed as is, could be simplified by changing those two shell variables, or could just be skipped entirely.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-16-2016
Without being able to understand WHAT you are doing in your above two awk commands, try
Code:
awk -vLLN="$LASTLINENUM" -vLEN=$LINEENDNUM" '
NR <= LLN       {next
                }
NR >  LEN       {exit
                }
... your second script's contents ...

' ${LOGFILE}

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-17-2016
the command searches for a pattern, and grabs a specific number of lines that come before AND after the pattern is found.
# 5  
Old 08-18-2016
There are solutions around in here, e.g. using a "ring buffer", that do this more understandably (and mayhap elegantly).
This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-18-2016
As a general remark about one-liners: one-liners reduce readability and understandability. Their advantage is that they can be fastly written.

Given that you are unable to modify your already existing one-liners (which i take as a hint that you are not completely understanding them already) i suggest that it is a bad idea to combine these to an even more complex construct which you are bound to understand even less.

It is of course always possible to rely on us (or other people like us) to provide modifications as the code will need to be modified but, honestly: do you really want to preside over an environment where you do not understand what is going on? It might be me but i for my part would have a very bad feeling being in such a position.

It is in your own best interest if you rely on us only if you need an explanation to further your understanding but not to write or modify your productive code. Whatever you use finally you should be able to produce on your own and you should write it in a way that you are able to understand its inner workings.

Btw., over time i found that restricting my written code to 80 characters per line with only select few exceptions (for instance definitions of string constants) makes my code more readable and better overall. The reason is that a line containing more than 80 characters is most likely badly formed and should be rewritten in a more conclusive manner anyways. This in most cases means i reformat one-liners to their long form.

Here is an example where i filter out comments and whitespace from an input file before processing:

Code:
sed 's/#.*//;s/^[ 	]*//;s/[ 	]*$//;/^$/d' "$fCmd" | while read chCmd ; do
     .....
done

Going over that code i modified it to:

Code:
sed 's/#.*//
     s/^[ 	]*//
     s/[ 	]*$//
     /^$/d' "$fCmd" |\
while read chCmd ; do
     .....
done

I'd say the second variant gives you a much cleaner and easier to grasp impression of what is going on than the first.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine 2 Commands

Hello, I have the following code. I wonder if it can be combined into 1 command. y=`ls -1| tail -n 1` m=${y%.abc} Thank you. (3 Replies)
Discussion started by: april
3 Replies

2. Shell Programming and Scripting

Combine two awk commands

Hi, Can someone please guide me how to combine the following two awk calls in one? I noticed that it is very often situation for me, and I think that it can be replaced with one awk call. The question is more general, not the exact one. echo "A B C/D" | awk '{print $3}' | awk -F/ '{print... (4 Replies)
Discussion started by: mirusnet
4 Replies

3. Shell Programming and Scripting

Combine multiple commands

I have the following sh-script: konsole -T todo -e vi todo.txt & konsole -T window1 -e ssh user@server & konsole -T window2 -e ssh user@server2 -e cd directory & The first two lines are working fine. The first opens a txt-file, the second opens a ssh-connection. The third line... (6 Replies)
Discussion started by: andre666
6 Replies

4. Shell Programming and Scripting

Pipe or combine output of three awk commands

What is the correct syntax to pipe or run three awk commands? Basically, using the output of the first awk as input in the second. Then using the output of the second awk in the third. Thank you :). awk 'FNR==NR {E; next }$3 in E {print $3, $5}' panel_genes.txt RefSeqGene.txt > update.txt |... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Combine multiple awk commands

Hi Team, I am getting input like below $ ps -ef | grep pmon | grep -v asm | grep -v grep oracle 3246 1 0 00:03 ? 00:00:01 ora_pmon_racora1 oracle 4367 1 0 00:03 ? 00:00:01 ora_pmon_test1 oracle 6893 1 0 00:03 ? 00:00:01 ora_pmon_gipora1... (6 Replies)
Discussion started by: kamauv234
6 Replies

6. Shell Programming and Scripting

How to combine awk and bash commands in script ?

Dear friends, I am just trying write one script using 2 files one file will contain details like below #X SERVER X LOCATION URL="http://www.abcd.com" FILENAME="abc.txt" ID_NAME="myabc_xyz" SERVER_PATH="/usr/local/dummy/html/....." #Y SERVER Y LOCATION URL="http://www.xyz.com"... (10 Replies)
Discussion started by: Akshay Hegde
10 Replies

7. Shell Programming and Scripting

Can I combine these two commands into one?

sed -e :a -e 's/<*>//g;/</N;//ba' a2.html -removes html tags and sed -i 's/YOURS TRULY/Joe Bob/' a2.html Replaces a string with another string can i make it into one string? (2 Replies)
Discussion started by: boyboy1212
2 Replies

8. Shell Programming and Scripting

nawk, combine commands

How would I combine two nawk commands together without calling up nawk twice. Just like the sed -e command nawk '$3>=from&&$3<=to' from="$STIME" to="$ETIME" | nawk '{$5="";$6=""}1' (2 Replies)
Discussion started by: numele
2 Replies

9. UNIX for Dummies Questions & Answers

Combine commands

Hi, i tried to combine grep with find and it didnt work grep 'find dirname filename" i also would like that the file will be sorted in the way. thanks a lot. (2 Replies)
Discussion started by: Spoiler
2 Replies

10. Shell Programming and Scripting

Combine Two Commands Output

How i can combine output of two commands in one file.......i tried this but it is not working although each command is working good seperately..... head -1 filename | tail -1 filename i think there is problem with command concatenator? (16 Replies)
Discussion started by: 33junaid
16 Replies
Login or Register to Ask a Question