Search code examples
shelltextreplacesplitconcatenation

How to edit a text file with shell script (split, replace, concatenate..)


I am trying to make a script for text editing. My text file have data like

3c26 d3ac 27d4 c03f 5dc4 5242 1d96 bbbf
4b7c 84bf 51fb c73f f784 7a8e bee9 c6bf
32df aafb bcf9 6ebf 099b de28 5714 bcbf
7ada f876 320a c9bf 9afc 7ea2 f4b4 c93f

I want to concatenate each 4 blocks of numbers after that write each one in a line proceeded by 0x and finishes by ,

I want the text to look like this

0x3c26d3ac27d4c03f,
0x5dc452421d96bbbf,
0x4b7c84bf51fbc73f,
0xf7847a8ebee9c6bf,
0x32dfaafbbcf96ebf,
0x099bde285714bcbf,
0x7adaf876320ac9bf, 
0x9afc7ea2f4b4c93f,

Solution

  • Simple Sed and Awk will work here

    #cat your_file_name|sed '/^ *$/d' |awk '{print ("0x"$1$2$3$4",""\n""0x"$5$6$7$7",")}'
    0x3c26d3ac27d4c03f,
    0x5dc452421d961d96,
    0x4b7c84bf51fbc73f,
    0xf7847a8ebee9bee9,
    0x32dfaafbbcf96ebf,
    0x099bde2857145714,
    0x7adaf876320ac9bf,
    0x9afc7ea2f4b4f4b4,
    

    This is the simplest way to implement, Please take refernce and optimize with your logic.