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

  • Home
  • SEARCH
  • 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 6682871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:45:21+00:00 2026-05-26T04:45:21+00:00

I’m a bit stuck with a unix command. I have a file called file.txt

  • 0

I’m a bit stuck with a unix command.

I have a file called file.txt with the following content:

Customer1

name=Alice

pass=unix

dob=

Customer2

name=Destroyer

pass=windows

dob=

What I basically want is to use a unix command that would allow me to grep for the customer (i.e Customer1) then give its dob a value, say for example dob=1912

so the file.txt will become like

Customer1

name=Alice

pass=unix

dob=1912

I’ve been working with sed, here’s the command that I formulated:

sed “s/$(awk ‘c–>0;/Customer1/{c=3}’ file.txt | tail -1)/dob=1234/g” -i file.txt

This however will replace ALL occurrences of dob with dob=1234 (quite obvious).

Can someone give me a hint on how to lock on the specific dob for each customer and replace it?

Thanks in advance.

  • 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-26T04:45:22+00:00Added an answer on May 26, 2026 at 4:45 am

    awk can do it by itself. see the one liner below:

     awk 'BEGIN{RS="dob=";OFS="\n\n"}$1~/Customer1/{$0=$0"dob=1234";print}' input
    

    test : change customer1’s dob value to 7777:

    kent$  cat t.txt
    Customer1
    
    name=Alice
    
    pass=unix
    
    dob=
    
    Customer2
    
    name=Destroyer
    
    pass=windows
    
    dob=
    
    kent$  awk 'BEGIN{RS="dob=";OFS="\n\n"}$1~/Customer1/{$0=$0"dob=7777";print}' t.txt
    
    Customer1
    
    name=Alice
    
    pass=unix
    
    dob=7777
    

    updated:

    kent$  awk 'BEGIN{RS="dob=";OFS="\n\n"}$1~/Customer1/{$0=$0"dob=7777";print > "t.txt"}' t.txt
    
    kent$  cat t.txt
    Customer1
    
    name=Alice
    
    pass=unix
    
    dob=7777
    

    updated again

    kent$  awk '/Customer1/{a++;}/dob=/&&a{$0="dob=7777";a=0;}1' t.txt > tmp.txt && mv tmp.txt t.txt
    
    kent$  cat t.txt
    Customer1
    
    name=Alice
    
    pass=unix
    
    dob=7777
    
    Customer2
    
    name=Destroyer
    
    pass=windows
    
    dob=
    

    updated with sed
    actually, using sed (with convenient -i option) to handle this is also possible. I didn’t understand your requirement correctly, I thought you only want the specific Customer Block. so I picked awk. Now comes the sed version:

    kent$  sed -ir '/Customer1/{x;s/.*/a/;x;} 
    /dob=/{x;
            /a/{s/a//g; x;
                    s/.*/dob=1234/; x;};
            x}' t.txt
    
    kent$  cat t.txt
    Customer1
    
    name=Alice
    
    pass=unix
    
    dob=1234
    
    Customer2
    
    name=Destroyer
    
    pass=windows
    
    dob=
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.