Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9283327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:35:56+00:00 2026-06-18T18:35:56+00:00

I am extracting characters from string at certain positions with this command: left=1; right=4;

  • 0

I am extracting characters from string at certain positions with this command:

left=1; right=4; grep ETM21CH01EPU3Z -A1 SLf_454.fna | tail -n 1 | awk '{print substr($0,1,4)}'

and getting this correct output:

GTTG

Now I would like to replace 1 and 4 by values in bash variables left and right.

I tried this:

left=1; right=4; grep ETM21CH01EPU3Z -A1 SLf_454.fna | tail -n 1 | awk -v l=$left r=$right '{print substr($0,l,r)}'

awk: cmd. line:1: fatal: cannot open file `{print substr($0,l,r)}’ for reading

Any suggestions, please? Thanks.

EDIT: copied from comment below so we can actually read it:

Well, I also have a file, where is written what to search (for example ETM21CH01EPU3Z) and at which positions. So the whole code is:

while read -r line; do
   echo $line;
   name=`echo $line | cut -d' ' -f 1`;
   left=`echo $line | cut -d' ' -f 2`;
   right=`echo $line | cut -d' ' -f 3`;
   grep $name -A1 SLf_454.fna | tail -n 1 | awk -v l=$left -v r=$right '{print substr($0,l,r)}';
done < CL6o_blastoutput_SLf_454.fna_out 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:35:58+00:00Added an answer on June 18, 2026 at 6:35 pm

    You’re missing a -v before your right variable. Every variable that you want to pass into awk must be preceded by a -v.

    Change it to:

    awk -v l=$left -v r=$right '{print substr($0,l,r)}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm extracting information from a webpage in Swedish. This page is using characters like:
struggle extracting necessary data from this complex json stored inside php variable. Via php
I'm struggle extracting necessary data from this json stored inside php variable. Not yet
I have to parse an input string in python and extract certain parts from
For example I'm extracting a text String from a text file and I need
I am extracting some text from html which is passed as a string. The
The bit of code below is extracting ASCII codes from characters. When I convert
I am extracting string from an byte array. The string is a sql script.
I have a compressed string value I'm extracting from an import file. I need
From extracting the values using grep Reading:RG1:+ /user/reading-2/Monday:12 Reading:RG1:- /user/**/Friday:12 Reading:RG1:- /user/**/*.txt:12 Reading:RG1:- /user/tet-23/**/*.txt:12

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.