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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:18:48+00:00 2026-05-23T00:18:48+00:00

How can i insert lines into a file using shell script such that they

  • 0

How can i insert lines into a file using shell script such that they are inserted in alphabetical order, following would explain better.

input file contains :

1 hsdfd
2 bsdfd
3 ekksdf
.
.

i want to insert these contents into another file using shell script such that they are as below,sorted on the alphabetical order:

< root > <br>
< devices name="bsdfd" value=2 > <br>
 < details info="xxxxxxxxxxxxxxxxx"> <br>
 < details info="yyyyyyyyyyyyyyyyy"> <br>
< /devices > <br>
< devices name="ekksdf" value=3 > <br>
 < details info="xxxxxxxxxxxxxxxxx"> <br>
 < details info="yyyyyyyyyyyyyyyyy"> <br>
< /devices > <br>
< devices name="hsdfd" value=1 > <br>
 < details info="xxxxxxxxxxxxxxxxx"> <br>
 < details info="yyyyyyyyyyyyyyyyy"> <br>
< /devices > <br>
< /root>
  • 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-23T00:18:49+00:00Added an answer on May 23, 2026 at 12:18 am
    echo "1 hsdfd
    2 bsdfd
    3 ekksdf " | sort -k 2 
    
    2 bsdfd
    3 ekksdf 
    1 hsdfd
    

    So if there is a second file, and you want the result being sorted:

    cat file1 file2 | sort -k 2 > sorted
    

    updated: After your edit, I would use sed for the main work, after invoking sort:

    echo "1 hsdfd
    2 bsdfd
    3 ekksdf" | sort -k 2  | sed -r 's/(.*) (.*)/foo \2 bar \1/;a\nyell\nprok'
    
    foo bsdfd bar 2
    nyell
    prok
    foo ekksdf bar 3
    nyell
    prok
    foo hsdfd bar 1
    nyell
    prok
    

    Some cosmetics with cat in the end, for footer and header. Done. 🙂

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

Sidebar

Related Questions

I know that I can insert multiple rows using a single statement, if I
Is there a way that I can insert values into a VB.NET Dictionary when
I am trying to insert data from a csv file into mysql using BigDump.
I'm try to insert values into my sqlite table using a python script. It
Using the following (incomplete) code, I am attempting to insert 10 strings into a
I know that you can insert multiple rows at once, is there a way
How i can insert data into more than one table of an Access database
I'm looking to develop a small script where users can insert their schedule. However,
Is '' a valid piece of XML? Can '' insert into oracle xblob column
I have the folling Perl script that I am using to import data directly

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.