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 8624219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:28:13+00:00 2026-06-12T07:28:13+00:00

Using shell script I want to read a word from text file and return

  • 0

Using shell script I want to read a word from text file and return next column word.

For eg, my input file will be like

AGE1 PERSON1
AGE2 PERSON2
AGE3 PERSON3
AGE4 PERSON4

I have variable in Sh file having PERSON’s name.
I want read input text file and get value of person’s age.

Please help, i’m beginner in Shell Scripting

  • 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-12T07:28:15+00:00Added an answer on June 12, 2026 at 7:28 am

    Building upon shellter‘s comment:

    age=$(grep "$person_name" people_file.txt | cut -f1 -d' ')
    

    I’ll try to explain everything. First, I assume somethings (but you can change them on your script):

    1. Your file with the data you entered is called people_file.txt.
    2. The person’s name you want to find is in the variable $person_name.
    3. The variable you want to store the result is $age.

    Firstly, because we need to use commands to generate the value of the $age variable, we must use $( and ) to run a command (or a series of commands), and replace itself with the text it captures from executing the command (or commands).

    We first need to find the line which contains the person’s name. For that we use grep: grep regex file. Grep will search file line by line until it finds a line that matches the regular expression regex. In our case we can simply search for the person’s name directly (assuming it doesn’t contain special characters, like the period or an asterisk). Note that we must place the variable between double quotes, otherwise a person’s name that has a space in it might be split in the command line so that its first name is used as the regular expression and the surname as the file. If you want to search in a case insensitive manner (like for example: John will find a line with JOHN or john), you can use the -i flag: grep -i regex file. The selected lines will be printed by grep into its output, but we will pump those lines into the input of the next command with the pipe operator |.

    Finally, we have a line (or many lines) with the results. Now we must extract the age. The cut command will split each line it reads from the input into fields, and only print the fields you ask it to. In this case, we ask for the first field with the -f1 option. Also, we specify that the space character is to be used as the delimeter (ie. the character that separates the fields) with the -d1 command.

    If you have more than one line with the same person’s name, we need to pipe the output of grep into a head command, so that we can have only the number of lines we want. We can tell head how many lines we want with the -n N option. So if you only want the first match:

    age=$(grep "$person_name" people_file.txt | head -n 1 | cut -f1 -d' ')
    

    Hope this helps a little =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read a string from a file with shell script variables, and want to
I want to read a file using shell script and here want to calculate
I want to run script file on android Shell using Native C program. I
I want to call shell script on windows environment using java code. I am
I tried using $(date) in my bash shell script, however, I want the date
How to compare two property files in two different folders using shell script input
I want to write a shell script which will find the occurence of multiple
Hello i want a simple shell script that find the name of the file
i want to create a new db of sqlite using A SHELL SCRIPT on
I want to read a string from a user (using read builtin or something

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.