Support Questions

Find answers, ask questions, and share your expertise

Sorting on fixed width file

avatar
Expert Contributor

Hi Team,

how to sort this below output in Ascending order  by maintaining same space in both header values. I've tried using query record but couldn't able to add spaces using SPACE function. but space which i've given is considering as SPACE(numeric) value instead of INT.

input:

NO CUSTOM.;NO INCSUPB;NO ALLBPBM ;IMPORTE BRUTO;DESCUENTO;IMPORTE NETO
0000000023;123548    ;999888     ;875,92       ;176,88   ;699,04
0000000012;123547    ;777666     ;2347,04      ;591,84   ;1755,2

 

Expected Output:

NO CUSTOM.;NO INCSUPB;NO ALLBPBM ;IMPORTE BRUTO;DESCUENTO;IMPORTE NETO
0000000012;123547    ;777666     ;2347,04      ;591,84   ;1755,2
0000000023;123548    ;999888     ;875,92       ;176,88   ;699,04



1 REPLY 1

avatar
Contributor
#!/bin/bash

# Input data
input_file="input.txt"

# Separate the header and the data
header=$(head -n 1 "$input_file")
data=$(tail -n +2 "$input_file")

# Sort the data numerically by the first column
sorted_data=$(echo "$data" | sort -t';' -k1,1n)

# Combine the header with the sorted data
echo "$header"
echo "$sorted_data"