I have a file which contains space separated field and its value. I want to sort this file based the the fields alphabetically.
v_party_id="49529957" v_party_default_status_cd="NOTDFLT" v_src_system_id="VTX" v_pd_percent="0.0127" d_report_ref_date="2021-03-31" v_accounting_standard="IFRS"
v_party_id="49530058" v_party_default_status_cd="NOTDFLT" v_src_system_id="VTX" v_pd_percent="0.00285" d_report_ref_date="2021-03-31" v_accounting_standard="IFRS"
After all the fields are sorted on field name (not value) alphabetically. Expected output is-
d_report_ref_date="2021-03-31" v_accounting_standard="IFRS" v_party_id="49529957" v_party_default_status_cd="NOTDFLT" v_src_system_id="VTX" v_pd_percent="0.0127"
d_report_ref_date="2021-03-31" v_accounting_standard="IFRS" v_party_id="49530058" v_party_default_status_cd="NOTDFLT" v_src_system_id="VTX" v_pd_percent="0.00285"
I am trying to do it via bash-
filecontent=( `cat "file1" `)
for t in "${filecontent[@]}"
do
echo $t
done|sort
And this gives me output as -
d_report_ref_date="2021-03-31"
d_report_ref_date="2021-03-31"
v_accounting_standard="IFRS"
v_accounting_standard="IFRS"
v_party_default_status_cd="NOTDFLT"
v_party_default_status_cd="NOTDFLT"
v_party_id="49529957"
v_party_id="49530058"
v_pd_percent="0.00285"
v_pd_percent="0.0127"
v_src_system_id="VTX"
v_src_system_id="VTX"
How can I get the desired output which is only two rows with sorted fieldnames?
Thanks in advance!!!
CodePudding user response:
Assuming there are no spaces in values, you should be able to use the following:
# read a file line by line and split on spaces into an array
while IFS=' ' read -r -a elements; do
# output array elements on separate lines, sort them and join with spaces
printf "%s\n" "${elements[@]}" | sort | paste -sd' '
done < inputfile.txt
If there are spaces between "... ..." you have to write your own parser to tokenize the line.
CodePudding user response:
You may use this gnu-awk solution:
awk '
BEGIN {PROCINFO["sorted_in"] = "@val_str_asc"}
{
for (i=1; i<=NF; i)
a[i] = $i
j = 0
for (i in a)
printf "%s", a[i] ( j<NF ? OFS : ORS)
}' file
d_report_ref_date="2021-03-31" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="49529957" v_pd_percent="0.0127" v_src_system_id="VTX"
d_report_ref_date="2021-03-31" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="49530058" v_pd_percent="0.00285" v_src_system_id="VTX"
Details:
PROCINFO["sorted_in"] = "@val_str_asc"sorts an array with values in ascending strings order- 1st for loop stores each field in array
awith key as field position abd value as field value - Due to presence of
PROCINFOarray will be sorted in alphabetic values order - 2nd for loop prints each sorted entry
CodePudding user response:
With your shown samples, please try following awk command. Written and tested in GNU awk.
awk '
BEGIN {PROCINFO["sorted_in"] = "@val_str_asc"}
{
split($0,a,OFS)
j=0
for (i in a){
printf "%s", a[i] ( j<NF ? OFS : ORS)
}
}' Input_file
