i/p file
o/p file
i/p file
o/p file
perl -F"\|" -alne '$,="|";print sort {$a cmp $b} @F' file
# nawk -F"|" '{OFS=FS;t=$2;$2=$3;$3=t;print}' infile
123|asd|basdf|nxs
145|awe|fdf
I think you are just swaping the field 2 and 3. But the need here is to sort the fields in each row. I am sorry if I didn't understand your script.
Sort should come then pipe to awk for formating.
sort -t"|" -k 3 infile | awk -F"|" '{OFS=FS;t=$2;$2=$3;$3=t;print}'
The o/p wants each line sorted using the US ascii character set as the collating sequence...not columns 2 and 3 swapped and if you look at the ascii table digits precede uppercase which precede lower case alphabets and here's an awk script to try out...
awk '{
n=split($0, x, "\|")
for (i=1; i<n; i++)
for (j=1; j<=n-i; j++)
if (x[j] > x[j+1]) {
t = x[j]
x[j] = x[j+1]
x[j+1] = t
}
for (k=1; k<=n; k++)
printf("%s%s", x[k], k<n?"|":"\n")
}' file