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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:49:04+00:00 2026-06-11T10:49:04+00:00

I have a CSV file which uses a highly customized format. Here, each number

  • 0

I have a CSV file which uses a highly customized format. Here, each number represents a the data in each of the 4 columns:

1 2 [3] 4

I need to restrict sed to only search and modify data appearing in the fourth column. Essentially, it must ignore all data on the line appearing before the first occurrence of a closing square bracket and space, ] and only modify data appearing after. E.g., file1.txt might contain this:

penguin bird [lives in Antarctica] The penguin lives in cold places.
wolf dog [lives in Antarctica with penguins] The wolf likes to eat penguins.

The replacement might be sed 's/penguin/animal/g' file1.txt. After running the script, the output would look like this:

penguin bird [lives in Antarctica] The animal lives in cold places.
wolf dog [lives in Antarctica with penguins] The wolf likes to eat animal.

In this case, all appearances of penguin were ignored prior to the first ] and were only changed on lines appearing after.

  • Additional closing brackets might appear later in the line, but only the first should be regarded as the division.

How can I have sed ignore the first three columns of this custom CSV format while it finds and replaces text?

I have GNU sed version 4.2.1.

  • 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-11T10:49:05+00:00Added an answer on June 11, 2026 at 10:49 am

    Normally I’d do it the way shelter described (if I was just typing in a quick sed command line) but it has the disadvantage that once you start matching part of the input to retain it (with \1 etc) you have to match and replace everything and can no longer use simple replacements like s/penguin/animal/. If you are willing to add some boilerplate around the replacement you can stash away the beginning of the line in the hold buffer and then get it back:

    sed -e 'h' \
        -e 's/.*\] //' \
        -e 's/penguin/animal/' \
        -e 'x' \
        -e 's/\] .*/] /' \
        -e 'G' \
        -e 's/\n//'
    

    The h saves the original line in the hold space. Then we remove the prefix and do any substitution (picking your example here) or series of substitutions on the end of the line. Then x swaps the end and the saved copy. We remove the original end from the saved copy and use G to put them back together. The G adds a newline we don’t want, so we remove that.

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

Sidebar

Related Questions

I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have a csv file with 48 columns of data. I need to open
I have a CSV file which contains data seperated with tabs. I need to
I have a Testing project that contains a csv file which a webtest uses
Little Background: I have csv file which has lots of rows and each row
I have a System.Data.DataTable which is populated by reading a CSV file which sets
I have a CSV file which the code below uses to put the contents
I have a csv file which has two columns, a numeric ID ( IDVAR
I have a CSV file which contains an ID and several other columns. I
I have a csv file which has 3 columns. I am trying to search

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.