If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > AWK Substring Error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-02-03, 15:32
smodem smodem is offline
Registered User
 
Join Date: Sep 2003
Posts: 4
Exclamation AWK Substring Error

Hi,

I am running a small awk script to get the substring of
a string. But I am getting "invalid character in expression"
error. I played around with all single quotes and double
quotes but I could not figure it out.

Here is my script
File Name : test
-------------------------------
#!/bin/bash
echo $1
HEADER=`head -n 1 $1`
echo Header is $HEADER
HEADERDATE=`awk "BEGIN {print substr('$HEADER',1,5)}"`
echo $HEADERDATE

My Error
-------------------------------

[oracle@ODW3 unifi]$ ./test source*/wrapmainUPD.txt
sourcefiles/wrapmainUPD.txt
Header is HDR, Master Table Data, Date [9,10,2003]
awk: cmd. line:1: BEGIN {print substr('HDR, Master Table Data, Date [9,10,2003]',1,5)}
awk: cmd. line:1: ^ invalid char ''' in expression



Any help is greatly appreciated.


Thanks,
Modem
Reply With Quote
  #2 (permalink)  
Old 10-02-03, 15:56
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hello smodem,
try this :

#!/bin/bash
echo $1
HEADERDATE=`head –n 1 $1 | awk '{
dummy=substr($1, 1, 5)
print (dummy)
}'`
echo $HEADERDATE

Greetings from Germany
Peter F.
Reply With Quote
  #3 (permalink)  
Old 10-02-03, 16:44
smodem smodem is offline
Registered User
 
Join Date: Sep 2003
Posts: 4
Exclamation AWK Substring

Hi Peter,

Thanks for the solution. The script is not getting the complete string. It is getting only 4 characters. This is the output.

Script
---------------------------
#!/bin/bash
echo $1

HEADERDATE=`head -n 1 $1 | awk '{dummy=substr($1,1,10); print(dummy)}'`
echo Header Date is $HEADERDATE
HEADERLENGTH=`head -n 1 $1 | awk '{dummy=length($1); print(dummy)}'`
echo Header Length is $HEADERLENGTH


Output
----------------------

$ ./test1 source*/wrapmainUPD.txt
sourcefiles/wrapmainUPD.txt
Header Date HDR,
Header Length is 4

Thanks,
Modem.
Reply With Quote
  #4 (permalink)  
Old 10-02-03, 17:55
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
gelöscht

Last edited by fla5do; 10-03-03 at 06:54.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On