use awk and cut

Have a file where I need to look at the first line and then look at a postion 30 - 45 on that line and cut the value to be use in a variable.

I have
testname=$(awk 'NR==1|{cut -c30-45}' $file )

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

testname=$(awk 'NR==1 {print substr($0,30,16)}' $file)
echo $testname

"awk" does not have "cut". "awk" has "substr()".
Of course you can use "cut" outside of "awk", but this would mean forking two external programs instead of one:

testname=$(awk 'NR==1' $file | cut -c30-45)
testname=$(cut -c30-45 <<< $(awk 'NR==1' $file))

echo $testname

And there are still "expr" and ("head" or "awk"):

testname=$(expr substr $(head -1 $file) 30 16)
testname=$(expr substr $(awk 'NR==1' $file) 30 16)

echo $testname

"head"and "cut" can also be combined, of course:

testname=$(cut -c30-45 <<< $(head -1 $file))
testname=$(head -1 $file | cut -c30-45)

echo $testname

Still two forks, though.

One fork ("head" or "awk"), works under bash only:

L=$(head -1 $file)
L=$(awk 'NR==1' $file)

echo $testname

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bjeAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.