To get or print specific value in result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To get or print specific value in result
# 8  
Old 02-04-2018
Quote:
Originally Posted by mirwasim
I tried this but it is not working for me, i tried with nawk and it is printing all the files name available in same directory and then below error message

when i pass file name and copy paste script then below is happening

Code:
-bash-4.1$ nawk -F, '
> BEGIN {OFS = ", "
> }
> /Test failed/ {
> match($(NF - 3), "http:.*/see/")
> URL = substr($(NF - 3), RSTART, RLENGTH)
> if(match($0, /"Test failed[^"]*"/))
> 
file1  file2  file3 file4
> print $3, substr($0, RSTART, RLENGTH), URL
> elseprint $3, $8, URL
> }' file
nawk: syntax error at source line 9
 context is
        elseprint >>>  $3, <<< 
nawk: illegal statement at source line 9
-bash-4.1$

Do you see any difference between the code you ran above and the code I suggested below?
Code:
awk -F, '
BEGIN {OFS = ", "
}
/Test failed/ {
	match($(NF - 3), "http:.*/see/")
	URL = substr($(NF - 3), RSTART, RLENGTH)
	if(match($0, /"Test failed[^"]*"/))
		print $3, substr($0, RSTART, RLENGTH), URL
	else	print $3, $8, URL
}' FILE

None of the code marked in red matches between the two. In addition to that you have removed all of the indentation which makes the code I suggested easy to read and the code you used hard to read. In particular, on the line that generated the diagnostic, my code had a tab between else and print which is not at all compatible with the code you used with those three words changing to the two words elseprint $3.

If you try running the code I suggested, you will probably find that it works much better than the abomination of the code I suggested that you tried running. Smilie

The only change that should have been made to the code I suggested would be to change FILE at the end of the script to the actual pathname of the file you want to process.

Last edited by Don Cragun; 02-04-2018 at 03:07 AM.. Reason: Add note at end of post.
# 9  
Old 02-04-2018
Respected Sir,

trust me i use same piece of code what you gave and my file name is also same
but it is not giving space in screen between else and print
no i break the line after else and it worked
# 10  
Old 02-04-2018
Quote:
Originally Posted by mirwasim
Respected Sir,

trust me i use same piece of code what you gave and my file name is also same
but it is not giving space in screen between else and print
no i break the line after else and it worked
If the code you paste onto your screen doesn't look like the code you copied from this site, then the code you are running is not the same piece of code that you were given.

So now you know that copying and pasting code into a shell when filename completion is enabled in that shell DOES NOT WORK. Even if filename completion is disabled, pasting multi-line code or code containing tab characters might not do what you expect depending on what options are enabled in your shell.

When sample code is suggested for you in this forum, the proper way to handle it is to copy into a file and then tell your shell to execute that file. If you use an editor to copy code into a file, be sure that auto-indentation is turned off and that your editor is set to use UNIX text file format (with each line terminated by a <newline> character; not DOS format with <carriage-return><line-feed> character pairs as line terminators).
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 02-05-2018
Quote:
Originally Posted by Don Cragun
If the code you paste onto your screen doesn't look like the code you copied from this site, then the code you are running is not the same piece of code that you were given.

So now you know that copying and pasting code into a shell when filename completion is enabled in that shell DOES NOT WORK. Even if filename completion is disabled, pasting multi-line code or code containing tab characters might not do what you expect depending on what options are enabled in your shell.

When sample code is suggested for you in this forum, the proper way to handle it is to copy into a file and then tell your shell to execute that file. If you use an editor to copy code into a file, be sure that auto-indentation is turned off and that your editor is set to use UNIX text file format (with each line terminated by a <newline> character; not DOS format with <carriage-return><line-feed> character pairs as line terminators).
Please close this thread
# 12  
Old 02-05-2018
Quote:
Originally Posted by mirwasim
Please close this thread
I have tagged the thread with "solved" and "awk" tags which indicate that awk was used to provide a working solution to your problem (which you could have easily done yourself).

Why do you want to close the thread now? Do you really want to forbid someone else with a better solution from adding a post to this thread with their suggestion?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print result of mv -n

I am trying to move files which donot have same filename using find /Users/ParijatMac/desktop/unix/new_dir -type f -mmin +"$HRS" -exec mv -n "{}" /Users/ParijatMac/desktop/unix/old_dir \; -print but i am getting all filenames including the ones with duplicate names.Please help me to sort... (5 Replies)
Discussion started by: parijat guh
5 Replies

2. Shell Programming and Scripting

Print if found non-desired result

I have a result like this root@server # grep -rl maldet /etc/cron* /etc/cron.d/maldet_daily /etc/cron.d/malcron /etc/cron.d/malcrondaily /etc/cron.d/malcronweekly What I need is, I need an if/else condition such that, if there is any output other than /etc/cron.d/maldet_daily in the... (8 Replies)
Discussion started by: anil510
8 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

5. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

6. Shell Programming and Scripting

Shifting result of echo by specific amount

I am using echo "HELLO" I want to specify a number shiftWt so that I move hello forward by shiftWt charcaters. Is there a way to do this? (2 Replies)
Discussion started by: kristinu
2 Replies

7. Shell Programming and Scripting

Print Specific lines when found specific character

Hello all, I have thousand file input like this: file1: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ | | | |$$ $$ UERT | TTYH | TAFE | FRFG |$$ $$______|______|________|______|$$ $$ | | | |$$ $$ 1 | DISK | TR1311 | 1 |$$ $$ 1 |... (4 Replies)
Discussion started by: attila
4 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

print out result from data file

i got a data file which contains all the pid,ppid,user,command,pcpu,start_time,status. I wanted to display out the pcpu which is greater than 0. i uses awk'{if($5 > 0){print}}' filename.txt but is printing out result which not i wanted. Is there any way which i can print out those pcpu which is... (8 Replies)
Discussion started by: thms_sum
8 Replies

10. Shell Programming and Scripting

print a function result in new file

Hi, i have a function which return a variable . serach ( paramatere) when i excute this function i get the result in the shell, i want to print this result in a file by calling just the function. how can i do it.. the code example is like that: search ( ) { .. } the call... (0 Replies)
Discussion started by: kamel.seg
0 Replies
Login or Register to Ask a Question