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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:25:26+00:00 2026-06-03T00:25:26+00:00

Given a file, for example: potato: 1234 apple: 5678 potato: 5432 grape: 4567 banana:

  • 0

Given a file, for example:

potato: 1234
apple: 5678
potato: 5432
grape: 4567
banana: 5432
sushi: 56789

I’d like to grep for all lines that start with potato: but only pipe the numbers that follow potato:. So in the above example, the output would be:

1234
5432

How can I do that?

  • 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-03T00:25:28+00:00Added an answer on June 3, 2026 at 12:25 am
    grep 'potato:' file.txt | sed 's/^.*: //'
    

    grep looks for any line that contains the string potato:, then, for each of these lines, sed replaces (s/// – substitute) any character (.*) from the beginning of the line (^) until the last occurrence of the sequence : (colon followed by space) with the empty string (s/...// – substitute the first part with the second part, which is empty).

    or

    grep 'potato:' file.txt | cut -d\   -f2
    

    For each line that contains potato:, cut will split the line into multiple fields delimited by space (-d\ – d = delimiter, \ = escaped space character, something like -d" " would have also worked) and print the second field of each such line (-f2).

    or

    grep 'potato:' file.txt | awk '{print $2}'
    

    For each line that contains potato:, awk will print the second field (print $2) which is delimited by default by spaces.

    or

    grep 'potato:' file.txt | perl -e 'for(<>){s/^.*: //;print}'
    

    All lines that contain potato: are sent to an inline (-e) Perl script that takes all lines from stdin, then, for each of these lines, does the same substitution as in the first example above, then prints it.

    or

    awk '{if(/potato:/) print $2}' < file.txt
    

    The file is sent via stdin (< file.txt sends the contents of the file via stdin to the command on the left) to an awk script that, for each line that contains potato: (if(/potato:/) returns true if the regular expression /potato:/ matches the current line), prints the second field, as described above.

    or

    perl -e 'for(<>){/potato:/ && s/^.*: // && print}' < file.txt
    

    The file is sent via stdin (< file.txt, see above) to a Perl script that works similarly to the one above, but this time it also makes sure each line contains the string potato: (/potato:/ is a regular expression that matches if the current line contains potato:, and, if it does (&&), then proceeds to apply the regular expression described above and prints the result).

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

Sidebar

Related Questions

For example given an input file like below: sid|storeNo|latitude|longitude 2|1|-28.03õ720000 9|2 10 jgn 352|1|-28.03¿720000
Given this example XML file: <doc> <tag> Hello ! </tag> <tag> My name is
Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why
Sometime, links to imgur are not given with the file extension. For example: http://imgur.com/rqCqA
Given a postscript file that has the following header %!PS-Adobe-3.0 I would like to
How do you yank all matching lines into a buffer? Given a file like:
Given a file path URL such as http://example.com/video.flv?start=5 , I want to get out
Given the following example file: host wallmount { option root-path iscsi:10.0.0.202::::iqn.2012-01.com.enactpc:storage.wall_mount; hardware ethernet 00:15:c5:71:b9:68;
Given a file looks like this: 1440927 1 1727557 3 1440927 2 9917156 4
For example, say I have a text file example.txt that reads: I like dogs.

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.