user1015196
user1015196

Reputation: 637

Getting substring using ksh script

I'm using ksh script to determine what the delimiter in a file is using awk. I know this delimiter will always be in the 4 position on the first line. The issue I'm having is that the character being used as in a delimiter in a particular file is a * and so instead of returning * in the variable the script is returning a file list. Here is sample text in my file along with my script:

text in file:

XXX*XX*     *XX*XXXXXXX.......

here is my kind of what my script looks like (I don't have the script in front of me but you get the jist):

delimiter=$(awk '{substr $0, 4, 1}' file.txt)
echo ${delimiter} # lists files in directory..file.txt file1.txt file2.txt instead of * which is the desired result

Thank you in advance,
Anthony

Upvotes: 1

Views: 611

Answers (2)

blackSmith
blackSmith

Reputation: 3154

Birei is right about your problem. But your AWK expression doesn't seem to be interested in the 1st line only. You can replace it with :

'NR==1 {print substr($0, 4, 1)}'

Then you can do a simple:

echo "$delimiter"

Upvotes: 1

Birei
Birei

Reputation: 36262

The shell is interpreting the content of the delimiter variable. You need to quote it to avoid this behaviour:

echo "${delimiter}"

It will print *

Upvotes: 0

Related Questions