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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:00:33+00:00 2026-06-14T02:00:33+00:00

I want to use a bash script to process 1 input file into 2

  • 0

I want to use a bash script to process 1 input file into 2 output files, each containing the same number of lines as the input file but with different parts of the input line. In particular one of the output files has to contain a md5hash of a selection of the input line, (hash calculated per line, not per file!):

So

Input_file.txt: ** 3 fields, separated by space

12347654 abcdfg 1verylongalpha1234numeric1

34543673 nvjfur 2verylongalpha1234numeric2

75868643 vbdhde 3verylongalpha1234numeric3

output file_1.txt would have to look like this: (left field is MD5sum, right field is field3 from input file which is also contained in the MD5hash):

12df5j754G75f738fjk3483df3fdf9 1verylongalpha1234numeric1

3jf75j47fh4G84ka9J884hs355jhd8 2verylongalpha1234numeric2

4hf7dn46chG4875ldgkk348fk345d9 3verylongalpha1234numeric3

output file_2.txt would have to look like this: (field1 and field2 from input file + MD5HASH)

12347654 abcdfg 12df5j754G75f738fjk3483df3fdf9

34543673 nvjfur 3jf75j47fh4G84ka9J884hs355jhd8

75868643 vbdhde 4hf7dn46chG4875ldgkk348fk345d9

I already have a script that doesthe job but it performs very badly: (script below may not work, this is from the top of my head, no linux here where I write this, sorry)

#!/bin/bash

While read line

do   MD5_HASH=${sed -nr 's/^[[:digit:]]*\s[[:alpha:]]*\s([[:alnum:]]*)/\1/p' <<<$line     | md5sum} 
read $line DATA_PART1 DATA_PART2 DATA_PART3

echo "$MD5_HASH $DATA_PART3" >> file_1.txt    ##append file_2.txt in loop THIS IS WHERE IT GETS HORRIBLY SLOW!

echo "$DATA_PART1 $DATA_PART2 $MD5_HASH" 
done < input_file.txt > file_2.txt

exit 0

I think that the “redirect stdout to file with append construct” ‘>>’ is responsible for the slow performance, but I can’t think of another way. Its in the loop because I have to calculate the md5hash per line.

(and oh, the sed command is necessary because in reality the part that goes into the MD5SUM can only be captured with regex and a quite complex pattern)

So anyone have a suggestion?

  • 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-14T02:00:34+00:00Added an answer on June 14, 2026 at 2:00 am

    This is one case in which I’d use a fully-featured language, such as Python.

    Although you might find a way to do this by using only the standard gnu tools, you’d very likely end up with a solution that will be:

    • very complex, hard to read and maintain
    • inefficient, as the tools don’t provide a straight-forward way to do this.

    1. Creating the first file in Python

    from hashlib import md5
    with open('input.txt', 'r') as infile:
        for l in infile:
            if not l.strip(): continue
            parts = l.strip().split()
            print md5(parts[2]).hexdigest(), parts[2]
    

    2. Creating the second file in Python

    from hashlib import md5
    with open('input.txt', 'r') as infile:
        for l in infile:
            if not l.strip(): continue
            parts = l.strip().split()
            print parts[0], parts[1], md5(parts[2]).hexdigest()
    

    I’m not sure about on which fields you calculated the checksum; however, of course, you can calculate it on whichever field(s) you want; you could also perform more complex regexp-based matching on the lines; and you can speed up things by outputting the two files at once, thus avoiding calculating the md5 twice.

    3. Creating the two files at once

    from hashlib import md5
    with open('infile.txt','r')  as infile, open('out1.txt','w') as out1, open('out2.txt','w') as out2:
        for l in infile:
            if not l.strip(): continue
            parts = l.strip().split()
            _checksum = md5(parts[2]).hexdigest()
            out1.write("%s\n" % " ".join([ _checksum, parts[2] ]))
            out2.write("%s\n" % " ".join([ parts[0], parts[1], _checksum ]))
    

    4. Same as #1 but reading from standard input

    import sys
    from hashlib import md5
    for l in sys.stdin:
        if not l.strip(): continue
        parts = l.strip().split()
        print md5(parts[2]).hexdigest(), parts[2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use php's strip tags in a bash script. I figured I
I want to know if there is any way to use Bash(Shell Script) to
I want to read a file by shell script, and process it line by
I want to use the system command in a bash shell script. Being more
I want to write a bash script that (recursively) processes all files of a
I want to write a bash script which will use a list of all
I use the following Bash Shell script to list the .txt files recursively under
I am making a bash script where I want to find files that are
I want to edit a file via a sed script in a bash script.
I want to use both bash alias and bash function with several arguments. I

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.