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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:09:29+00:00 2026-05-31T13:09:29+00:00

Is it possible to search for a string and if it exists keep that

  • 0

Is it possible to search for a string and if it exists keep that line only…the full line and not just the string? I have two comma delimited data variables. What I would like to do is loop through my $data variable and use col2 of it as a search item. Then search the $example variable and if it finds the match then prepend some data to that line only. Below this prepends the data to the correct thing but it is printing out twice because in this case there are two items. It would be nice to say don’t print anything unless it is a match and then print that whole line

#!/bin/bash
data="item,thing
item2,thing2"

example="a,lot,of,thing,in,this,csv
big,foot,lives,next,to,me,yikes"

echo "$data" | while IFS=, read -r col1 col2
do
echo "$example" | sed "s/$col1/$col2,$col1/i"                                                                            
done

I have tried making col2 an array key but I wasn’t successful with it. Maybe because I’m on a Mac and from what I read it ships with an older version of Bash.

  • 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-05-31T13:09:30+00:00Added an answer on May 31, 2026 at 1:09 pm

    If you don’t want sed to print unless you request it to, then you write:

    ... | sed -n "/$col1/{s/$col1/$col2,$col1/i; p}"
    

    I’m assuming that the i suffix is meaningful to your dialect of sed; it isn’t standard. The -n means ‘do not print automatically’. The /$col1/ looks for the pattern; when it is found, the commands in the braces are executed — your substitute operation and then ‘print the line’.

    Note that your pattern item would match nitemare if that appeared in $example.

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

Sidebar

Related Questions

I have a question about IPTC metadata. Is it possible to search images that
I have a Stored Proc query below which involves returning partial delimited search string.
I have the need to search for an item that exists in one of
In eclipse, is it possible to use the matched search string as part of
Is it possible to search and replace all occurrences of a string in all
Possible Duplicate: How to see if a substring exists inside another string in Java
Possible Duplicate: Search for string allowing for one mismatch in any location of the
I want to parse a search string similar to that provided by Gmail using
I have a simple Core Data model with two string attributes (size and category).
I have some code that I've inherited, and it's not the greatest in the

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.