Skip to main content
PradNiFi1236
Gold ⭐️
December 3, 2024
Question

Sorting on fixed width file

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

cloude
Gold ⭐️
Gold ⭐️
January 20, 2025
#!/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"
Translate