Search code examples
bashshellsortingsplittr

Shell command - tr,sort,paste,nl


I have this assignment

The FILE variable is the name of a text file that contains exactly 30 lines. Each line consists of just one word, consisting of uppercase and lowercase letters and numbers.

Change this file so that all digits disappear from the words, all letters change to lowercase, and the modified words are sorted alphabetically in ascending order. The modified file will have the format of three columns of separate tables, where the first column will contain the first 10 words after sorting, the second column the second 10 and the rest the third. Number the lines of the file.

input:

hUeY65u
9sjKdfh
A56oi4
22cfe4HW
Gws6K
zh71p6t
...

output:
     1  aoi     gwsk    sjkdfh
     2  cfehw   hueyu   zhpt
     ...

I created this script, it works for me, but not on the school server. You don't know where the problem might be. Thank you

cat $FILE | tr '[:upper:]' '[:lower:]' | tr -d "[0-9]" | sort >file1.txt;  
split -l 10 file1.txt; paste xaa xab xac >file2.txt; nl file2.txt; 



    Stderr mismatch:
-----expected-----

--------got-------
/home/blxrpcdcgb/Student: line 1: $FILE: ambiguous redirect
paste: xaa: No such file or directory

------------------
'"$HOME"/file1.txt' is excessive.
'"$HOME"/file2.txt' is excessive.
'"$HOME"/kfekd  jojny' has incorrect content:
-----expected-----
     1  azkxbj  ikrmp   rynlv
     2  bledfoa jttknol sipei
     3  cdquayr jxphn   tatuqun
     4  csglb   llnsly  vjyqfl
     5  dwuzei  loxrha  vpjjhqf
     6  egs moxia   wbbvk
     7  enzvxg  ozaww   wmiik
     8  fuvks   ph  wpy
     9  gnbjr   phcupa  xsovhv
    10  hdjufth pisza   zuumxy

--------got-------
1sipei
Vpjjhqf
moxIA
wmiik96
9ph32
ozaWW
egs30
Pisza09
jttknol
hdjufTH
ZuumXY
llnsLY
Tatuqun
ikrMP
loxrHA
dwuzei
wbbvk
Xsovhv
PhcuPA
2fuvKS
Wpy09
jxpHN
Bledfoa
VjyqFL
gnbjr
Cdquayr
csglb
8rynLV
azkxBJ
0enzvxg

------------------

Solution

  • You can use pr for arranging the columns. No intermediate files are needed:

    tr [:upper:] [:lower:] < "${FILE}" \
    | tr -d [:digit:] \
    | sort \
    | pr -t3 \
    | nl
    

    or in one line:

    tr [:upper:] [:lower:] < "${FILE}" | tr -d [:digit:] | sort | pr -t3 | nl
    

    See: https://linux.die.net/man/1/pr