awk command issue


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk command issue
# 8  
Old 10-20-2016
Code worked. thanks
# 9  
Old 10-20-2016
Looking at it again, the looong pipe could be reduced to one single awk command, although without decent input sample this is impossible to verify. Try
Code:
awk -v var=$i '
/upd_/  {a = $0}                        # keep last line containing "upd_" before the one -
$0 ~ var {exit}                         # containing the pattern in "var"; when found, leave
                                        #      script jumping to END section
END     {split (a, T, "/")              # spread "a" variable into "T" array on "/"
         sub (/\..*$/, "", T[6])        # remove everything after first dot "."
         sub (/_[^_]*$/, "", T[6])      # remove the last part after last "_" in element
         print T[6]                     # print result in element
        }
' $filename

and come back with the results.


EDIT: / added after abhii's comments/errors...

Last edited by RudiC; 10-20-2016 at 08:20 AM.. Reason: corrected typo
# 10  
Old 10-20-2016
Quote:
Originally Posted by RudiC
Looking at it again, the looong pipe could be reduced to one single awk command, although without decent input sample this is impossible to verify. Try
Code:
awk -v var=$i '
/upd_/  {a = $0}                        # keep last line containing "upd_" before the one -
$0 ~ var {exit}                         # containing the pattern in "var"; when found, leave
                                        #      script jumping to END section
END     {split (a, T, "/")              # spread "a" variable into "T" array on "/"
         sub (/\..*$/, "", T[6])        # remove everything after first dot "."
         sub (/_[^_]*$, "", T[6])       # remove the last part after last "_" in element
         print T[6]                     # print result in element
        }
' $filename

and come back with the results.
for me
Code:
awk -v var=$i  '1; $0 ~ var {exit}' $filename | awk '/upd_/ {a=$0} END{print a}' | cut -d ' ' -f3

did work to get the filename with date format (what was required)

though the code you have posted given me the error:
Code:
awk -v var=$i '/upd_/  {a = $0} $0 ~ var {exit} END {split (a, T, "/") sub (/\..*$/, "", T[6]) sub (/_[^_]*$, "", T[6]) print T[6] }' $filename

Code:
awk: /upd_/  {a = $0} $0 ~ var {exit} END {split (a, T, "/") sub (/\..*$/, "", T[6]) sub (/_[^_]*$, "", T[6]) print T[6] }
awk:                                                                                       ^ unterminated regexp
awk: cmd. line:1: /upd_/  {a = $0} $0 ~ var {exit} END {split (a, T, "/") sub (/\..*$/, "", T[6]) sub (/_[^_]*$, "", T[6]) print T[6] }
awk: cmd. line:1:                                                                                                                      ^ unexpected newline or end of string

worked output: maybe the awk has to modified accordingly
Quote:
Code:
awk -v var=$i  '1; $0 ~ var {exit}' $filename | awk '/upd_/ {a=$0} END{print a}'

Unpack complete upd_0_pluz_20161014 Fri Oct 14 16:41:11 EDT 2016
Quote:
Actually the sample file is not in standard format and reading or getting dummy data is extremely difficult (the lines in between two patterns itself is somewhere around 10k and full file is having millions of the lines); u can use this; if it is helpful to rewrite


Moderator's Comments:
Mod Comment Please use CODE (not QUOTE) tags as required by forum rules!

Last edited by RudiC; 10-20-2016 at 08:05 AM.. Reason: Added CODE tags.
# 11  
Old 10-20-2016
You can't stuff all statements from a structured, indented script into a single line just one after the other and expect it to work; at least semicolons have to be added to separate the statements, and a logics check should be done afterwards as well.

But, you are right, a / was missing in the command. I updated the post accordingly, thanks.

Besides that, not having any data to work upon, above proposal was an approximation to be tested and adapted - it was a starting point to show the concept.
BTW, your specification in post#1 did not match what was coded in your script. Starting with a bad specification will never end with good results.

Last edited by RudiC; 10-20-2016 at 08:21 AM..
# 12  
Old 10-20-2016
Sorry for the confusion caused.

have replaced code of Post #1

Code:
updatefile=$(awk '1;/$i/{exit}' $filename | awk '/upd_/ {a=$0} END{print a}' | cut -d '/' -f6 | cut -d '.' -f1 |  awk 'NF{NF-=1}1' FS='_' OFS='_' ); yupd=`echo $updatefile"_"$ydate`; 
completiontime=( $(grep $yupd /home/load_updates.hst | grep "Unpack complete" | cut -d " " -f5-7) );

with
Code:
updatefile=$(awk -v var=$i  '1; $0 ~ var {exit}' $filename | awk '/upd_/ {a=$0} END{print a}' | cut -d ' ' -f3); 
completiontime=( $(grep $updatefile /home/lim/config/load_updates.hst | grep "Unpack complete" | cut -d " " -f5-7) );

which serve me the purpose of the script.
I am trying to working out the code you have given. This is now my output looks like now:

Quote:
==================================================================================================== ===================================
Symbol - PUAFZ00
Processing_date - 20161017 UpdatePackage - upd_0_pluz_20161017 Processing_Time 17:00:48 EDT 2016
Processing_date - 20161018 UpdatePackage - upd_0_pluz_20161018 Processing_Time 21:20:36 EDT 2016
Processing_date - 20161019 UpdatePackage - upd_0_pluz_20161019 Processing_Time 21:45:32 EDT 2016
Processing_date - 20161013 UpdatePackage - upd_0_pluz_20161013 Processing_Time 16:50:37 EDT 2016
Processing_date - 20161014 UpdatePackage - upd_0_pluz_20161014 Processing_Time 16:41:11 EDT 2016
==================================================================================================== ===================================

Last edited by abhii; 10-20-2016 at 08:43 AM.. Reason: updating the final output
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with awk command

I am new to unix.I have a requirement to get few fields from the log file as below. Log app9/cc-gr_base.log.2017-07-19.gz: hostname 1500523166993 NA:NA:NA http-nio-8080-exec-56 INFO Points balance from MIS for user with userId: 19651069, first name: DEREK RICHARD and last name: BOUDREAU is... (3 Replies)
Discussion started by: nextStep
3 Replies

2. Shell Programming and Scripting

Issue with awk command between Linux and Solaris

Hi, Here is the output using bash profile on Linux uptime 04:59:14 up 16 days, 4:48, 2 users, load average: 1.00, 1.00, 1.20 Here is the output using bash profile on Solaris uptime 4:00am up 84 day(s), 22:21, 3 users, load average: 0.09, 0.10, 0.12 Now,... (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

awk command issue

Hi All, I have one file with below content Post1:uri Post2:urieop Post3:urtei I am trying to read each word seprated by delimiter with below command Value1=$(awk -F":" '{print $1}' $HSFILE) Value2=$(awk -F":" '{print $2}' $HSFILE) echo $Value1 echo $Value2 It is... (5 Replies)
Discussion started by: sharsour
5 Replies

4. Shell Programming and Scripting

For loop, awk command issue

limit.csv data -------------- 5600050 38Nhava 400077 27Bomay rate.txt data ------------- 38NhaVA 27BomaY 27Bomay below is my script: for i in `cat limit.csv` do b=`awk '{print $1}' $i` (4 Replies)
Discussion started by: p_satyambabu
4 Replies

5. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

6. Shell Programming and Scripting

awk issue

Hi all, i am trying to use below command to see the output of hardware inventory, but i only see 2 first line no output of the command. awk '/Hardware/ {print $0}' XXX_result.txt Hardware inventory: Hardware inventory: any idea how to see whatever is under hardware inventory. i... (11 Replies)
Discussion started by: Jared
11 Replies

7. Shell Programming and Scripting

awk NR issue

Hi guys, i am trying to analyze a text file using awk and am not able to solve this issue. This is the piece of code that I have written BEGIN { ## Time to count MACs -> 5 seconds. TIME_LIMIT = 5; k = 50000; } ## For every line. { time_in_seconds = $1... (2 Replies)
Discussion started by: jamie_123
2 Replies

8. Shell Programming and Scripting

awk command issue

Hi, Please could someone advise the issue i have with my awk command ? my command is : export NUM_SCENARIOS=`awk -F= '!/^#/ && /NUM_SCENARIOS/{print $2}' /home/environment.properties` when I echo $NUM_SCENARIOS this comes back with : 100 10 The issue I have is, there is... (9 Replies)
Discussion started by: venhart
9 Replies

9. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

10. Shell Programming and Scripting

command line arg issue with awk

Hi friends, I am trying to pass input from command line and trying to print that column values. (FYI: I am using ksh) My code goes like this... #!/bin/sh column=$1 awk '{print $'$column'}' I execute using command like this --> ls -l | file_name parameter Hope I am clear with my... (2 Replies)
Discussion started by: divzz
2 Replies
Login or Register to Ask a Question