awk sub() issue in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk sub() issue in Unix
# 1  
Old 07-11-2012
awk sub() issue in Unix

Hi, I am new to unix shell script and I have some trouble on the awk sub

I would like to pick the Date "July 10 2012" into $corr_date by using sub() function, but it is not successful.

The inputted text file:
pic.*.txt
July 10 2012 20:30:50 , 1234567.jpg
July 10 2012 20:30:52 , 5648978.jpg
July 10 2012 20:30:53 , 6405789.jpg

Code:
#!/bin/ksh

curr_date=`date +"%B %d %Y"`

for i in `ls -1 pic.*.txt`
  do
     Date=`tail -1 $i |awk 'BEGIN {FS=","} {print $1}'`
     corr_date=`awk '{sub(/[0-9]+:[0-9]+:[0-9]+/,"") ; print;}' Date`

     if [ "$corr_date" = "$curr_date"]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done

  exit

The error msg is:
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks
# 2  
Old 07-11-2012
I think you can simplify things a bit by using a single awk to suss out the last record of the file in $i and assign it to corr_date:

Code:
corr_date=$(  awk 'END { print $1, $2, $3; }' $i )

Your code would become:

Code:
#!/bin/ksh

curr_date=`date +"%B %d %Y"`

for i in `ls -1 pic.*.txt`
  do
     corr_date=$(  awk 'END { print $1, $2, $3; }' $i )
     if [ "$corr_date" = "$curr_date"]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done

This User Gave Thanks to agama For This Post:
# 3  
Old 07-11-2012
Thank for your reply but I try to run this script and discover that "corr_date" do not have any value inside.
# 4  
Old 07-11-2012
try running the script in debug mode to find more. Smilie
# 5  
Old 07-11-2012
I have use debug mode already, it does not display anything Smilie
# 6  
Old 07-11-2012
space needed in the below line

Code:
 
if [ "$corr_date" = "$curr_date" ];

Are you getting any error ?
# 7  
Old 07-11-2012
Yes, the corr_date's value is empty
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with awk issue

OK, so I am trying to use awk as a method of accessing a table stored in a file to then provide the capability of a look up table. The table is stored in a file named "/Users/jhaney/Desktop/assetTypeMapping.tsv" and looks like this: aCategory aLetter aNumber AssetCat1 A 123 ... (10 Replies)
Discussion started by: jhaneyzz
10 Replies

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

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

4. Shell Programming and Scripting

issue trying to use awk

Hi Gurus, I am facing a similar issue usiung an awk command. Below is my requirement: ---DATA--- A;F;G A;D;E A;D;E B;Z;P C;Z;Q Expected: A F<TAB>G D<TAB>E D<TAB>E B D<TAB>E (1 Reply)
Discussion started by: rajangupta2387
1 Replies

5. Shell Programming and Scripting

AWK Issue

Hey, this is my code, cat $fulltrpath | while read line do inputfile=$(sed 1q $fulltrpath | awk '{ FS = "\t"; print $2$1}') outputpath=$(sed 1q $fulltrpath | awk '{ FS = "\t"; print $3 }') echo $inputfile echo $outputpath cp $inputfile $outputpath let path++ done if i... (1 Reply)
Discussion started by: inshafccna
1 Replies

6. Shell Programming and Scripting

Issue with AWK

I have this input file 0FB7,1083,Synchronized,FriNov121655,2816_7RAID5,05F:1_10F:1,10000000NoneNone,DC_db00p01 0FB7,1150,Split,MonApr180658,2816_7R5GC,N/A,N/A,N/A 06C4,0710,Synchronized,WedMar91105,2816_7RAID5,04E:1_11E:1,10000000NoneNone,DL_nb00p25... (1 Reply)
Discussion started by: greycells
1 Replies

7. Shell Programming and Scripting

Issue in awk

In the following code, Im trying to imbed many statements in a single awk statement. But it gives an error on that, for i in `less usage_types_dwh.txt` do cd /u01/app/evident/analysis_lab/usg_type grep $i svc_type.txt | head -1 | awk 'BEGIN {FS=","} {print $1 "==" $2 ":" $3 ":" $4;... (2 Replies)
Discussion started by: alishehzadpaul
2 Replies

8. Shell Programming and Scripting

Awk issue

Can someone please explain below code. $LIST|awk ' /^$/ { next } substr($0,1,4)=="Exiting" { mk = 1; next } mk==1 { print $3,$7,$10,$14; exit } Cheers, gehlnar (5 Replies)
Discussion started by: gehlnar
5 Replies

9. Shell Programming and Scripting

Awk issue

Hi, # grep "^Listen" httpd.conf | awk '{print $2}' FrontEnd_1_IP:8081 FrontEnd_2_IP:8081 8081 8082 8083 # I need to get the values one at a time but I just can't manage to do that. Thanks, Bianca (20 Replies)
Discussion started by: potro
20 Replies

10. Shell Programming and Scripting

Unix Arithmatic operation issue , datatype issue

Hi, I have a shell scripting. This will take 7 digit number in each line and add 7 digit number with next subsequent lines ( normal addition ). Eg: 0000001 0000220 0001235 0000022 0000023 ........... ......... ........ Like this i am having around 1500000 records. After adding... (23 Replies)
Discussion started by: thambi
23 Replies
Login or Register to Ask a Question