Sponsored Content
Top Forums Shell Programming and Scripting Combine awk commands into one Post 302979708 by bakunin on Thursday 18th of August 2016 05:56:08 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Test::Fatal(3)						User Contributed Perl Documentation					    Test::Fatal(3)

NAME
Test::Fatal - incredibly simple helpers for testing code with exceptions VERSION
version 0.010 SYNOPSIS
use Test::More; use Test::Fatal; use System::Under::Test qw(might_die); is( exception { might_die; }, undef, "the code lived", ); like( exception { might_die; }, qr/turns out it died/, "the code died as expected", ); isa_ok( exception { might_die; }, 'Exception::Whatever', 'the thrown exception', ); DESCRIPTION
Test::Fatal is an alternative to the popular Test::Exception. It does much less, but should allow greater flexibility in testing exception-throwing code with about the same amount of typing. It exports one routine by default: "exception". FUNCTIONS
exception my $exception = exception { ... }; "exception" takes a bare block of code and returns the exception thrown by that block. If no exception was thrown, it returns undef. Achtung! If the block results in a false exception, such as 0 or the empty string, Test::Fatal itself will die. Since either of these cases indicates a serious problem with the system under testing, this behavior is considered a feature. If you must test for these conditions, you should use Try::Tiny's try/catch mechanism. (Try::Tiny is the underlying exception handling system of Test::Fatal.) Note that there is no TAP assert being performed. In other words, no "ok" or "not ok" line is emitted. It's up to you to use the rest of "exception" in an existing test like "ok", "isa_ok", "is", et cetera. Or you may wish to use the "dies_ok" and "lives_ok" wrappers, which do provide TAP output. "exception" does not alter the stack presented to the called block, meaning that if the exception returned has a stack trace, it will include some frames between the code calling "exception" and the thing throwing the exception. This is considered a feature because it avoids the occasionally twitchy "Sub::Uplevel" mechanism. Achtung! This is not a great idea: like( exception { ... }, qr/foo/, "foo appears in the exception" ); If the code in the "..." is going to throw a stack trace with the arguments to each subroutine in its call stack, the test name, "foo appears in the exception" will itself be matched by the regex. Instead, write this: my $exception = exception { ... }; like( $exception, qr/foo/, "foo appears in the exception" ); Achtung: One final bad idea: isnt( exception { ... }, undef, "my code died!"); It's true that this tests that your code died, but you should really test that it died for the right reason. For example, if you make an unrelated mistake in the block, like using the wrong dereference, your test will pass even though the code to be tested isn't really run at all. If you're expecting an inspectable exception with an identifier or class, test that. If you're expecting a string exception, consider using "like". success try { should_live; } catch { fail("boo, we died"); } success { pass("hooray, we lived"); }; "success", exported only by request, is a Try::Tiny helper with semantics identical to "finally", but the body of the block will only be run if the "try" block ran without error. Although almost any needed exception tests can be performed with "exception", success blocks may sometimes help organize complex testing. dies_ok lives_ok Exported only by request, these two functions run a given block of code, and provide TAP output indicating if it did, or did not throw an exception. These provide an easy upgrade path for replacing existing unit tests based on "Test::Exception". RJBS does not suggest using this except as a convenience while porting tests to use Test::Fatal's "exception" routine. use Test::More tests => 2; use Test::Fatal qw(dies_ok lives_ok); dies_ok { die "I failed" } 'code that fails'; lives_ok { return "I'm still alive" } 'code that does not fail'; AUTHOR
Ricardo Signes <rjbs@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Ricardo Signes. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.16.3 2012-02-16 Test::Fatal(3)
All times are GMT -4. The time now is 02:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy