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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:31:23+00:00 2026-06-07T20:31:23+00:00

suppose I have a file: its format should be : number, string1 , [string2]

  • 0

suppose I have a file:

its format should be :
number, string1 , [string2] ,….

here string1 should not contain ‘,’ ,because we use ‘,’ to separate each column
but due to some reason ,string1 now contain some ‘,’ inside it,
so we need to replace it with other symbol ,such as ‘-‘

1,aaa,bbb,ccc,[x,y,z],eee,fff,ggg
2,q,w,[x],f,g
3,z,[y],g,h
4,zzz,xxx,ccc,vvv,[z],g,h
....

should be revised to :

1,aaa-bbb-ccc,[x,y,z],eee,fff,ggg
2,q-w,[x],f,g
3,z,[y],g,h
4,zzz-xxx-ccc-vvv,[z],g,h
....

what’s the best way to do it without programming , I mean we just use awk,sed,vim rather than shell programming,python,c++,etc

Thanks

  • 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-07T20:31:24+00:00Added an answer on June 7, 2026 at 8:31 pm
    $ awk -F, 'BEGIN{OFS=FS} {two=$0;sub($1 FS,"",two);sub(/,[[].*/,"",two);gsub(/,/,"-",two); rest=$0;sub(/^[^[]*/,"",rest); print $1,two,rest}' input.txt 
    1,aaa-bbb-ccc,[x,y,z],eee,fff,ggg
    2,q-w,[x],f,g
    3,z,[y],g,h
    4,zzz-xxx-ccc-vvv,[z],g,h
    $ 
    

    Let’s break out the awk script for easier commenting.

    $ awk -F, '
      BEGIN { OFS=FS }
      {
        two=$0;                # Second field is based on the line...
        sub($1 FS,"",two);     # Remove the first field,
        sub(/,[[].*/,"",two);  # Remove everything from the [ onwards,
        gsub(/,/,"-",two);     # Replace commas in whatever remains.
    
        rest=$0;               # Last part of the line, after "two"
        sub(/^[^[]*/,"",rest); # Strip everything up to the [
    
        print $1,two,rest;     # Print it.
      }
    ' input.txt 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have file which goes like this : a void measure() { a
Suppose I have a file containing some lines: line 1 ... line 2 ...
Suppose I have a file X.h which defines a class X, whose methods are
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
Suppose I have the following list: ID3_tag = ['Title','Artist','Album','Track'] And I have a file
I'm interested in knowing how revision-control systems do merging. Suppose you have a file
Suppose I have a Javascript file function js_main(args){ /* some code */ var x
Suppose I have an XSD file having below lines of code; <xsd:simpleType name=test> <xsd:restriction
Suppose I have a source file open and I launch a shell. I can
Suppose I have a source file that is 18218 bytes. I open the file

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.