Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

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"