Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...What you have done for people like me is immeasurably helpful."

Geography

Where in the world do Tek-Tips members come from?
DJR999 (Programmer)
1 Mar 11 14:14
I know this should be simple, but sorry I am fairly new to UNIX and can't find the syntax to make this work.

I have following two commands in a script:

x=`expr $(head -1 filename.ext | cut -c16 | grep [A-Z] | wc -l) + 16`
del=`head -1 filename.ext | cut -c$x`

This works fine as I expect it to.  But I want to combine it into one command.  Essentially I just want the entire expression that I am assigning to x in the first command to be evaluated at the end of the second command as my argument to the -c attribute of the cut command.  But I've tried every way I can to get it to evaluate properly and can't get it.  

Thanks for any help.
 
Helpful Member!  p5wizard (IS/IT--Management)
1 Mar 11 15:43
With backquotes that is kind of hard, with $(cmd) construct it is easier and more readable:

del=$(head -1 filename.ext | cut -c$(expr $(head -1 filename.ext | cut -c16 | grep [A-Z] | wc -l) + 16))

But I guess there are easier ways to accomplish the same result.

If an uppercase letter is at position 16 in 1st line, use char at 17th position. I'd probably use awk for that.

HTH,

p5wizard

2rArne (TechnicalUser)
12 May 11 16:44
x will return either 16 or 17

17 if the 16'th character of the first line in file filename.ext is uppercase (range of A-Z), otherwise it will return 16


head -1 filename.ext |cut -c16 |grep [A-Z] && {
  # 16th character is in range A-Z
  head -1 filename.ext |cut -c17
} || {
  # 16th character is not in range of A-Z
  head -1 filename.ext |cut -c16
}

I used the curly brackets to make it more readable as well as show that you can use several lines of command

Compressed into one line:
head -1 filename.ext |cut -c16 |grep -q [A-Z] && head -1 filename.ext |cut -c17 || head -1 filename.ext |cut -c16
 
2rArne (TechnicalUser)
12 May 11 17:00
I forgot to use -q with grep, in the curly brackets example :)

Explanation:
'grep -q'
  - is a quiet grep and will only tell you if it found the expression or not.

Based on the return code from grep I either cut the 17th or the 16th character out from the output of 'head -1 filename.ext'

---- so an appropriate answer to your question could be:
del=`head -1 filename.ext |cut -c16 |grep -q [A-Z] && head -1 filename.ext |cut -c17 || head -1 filename.ext |cut -c16`

/2r

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close