Hi Team, I need to know how to displayer letter 'a' from any word from field 5 based on script below:
EmpID:Name:Designation:UnitName:Location:DateofJoining:Salary
1001:Thomson:SE:IVS:Mumbai:10-Feb-1999:60000
1003:Jackson:DM:IMS:Hyderabad:23-Apr-1985:90000
1005:Alice:PA:::26-Aug-2014:25000
If you see, the last line does not conatin any value in field 5. I have been trying with
cat file.txt | sort -t ':' -rk5 | grep 'a. *a'
but nothing happens. I am learning Unix. Thanks a lot for your support
Another question very like its predecessors. You should already know that the sort
does not select fields, it merely resequences whole lines, and also that Linux commands almost all read their own files without piping from cat
.
Do you expect to print just the "a" characters (so line 3 will show "aa"), and include the header line's "a"? Or do you want to print field 5 of every line if that field contains "a", or the whole line if field 5 contains at least one "a", and/or omit the header line? Did you consider whether "Antrim" qualifies as having the letter "a"?
Your grep pattern actually matches at least two "a", separated by at least one character plus any number of spaces, which is different to your stated requirement.
The last line is a poor test case, being completely empty. A word not containing "a" would be better -- perhaps "TheQuickBrownFoxJumpsOverTheSupineDog".
If you have a free choice of commands, and need to detect header lines, and to work with fields, then Awk is the perfect tool. Just look up FNR
and ~
in the excellent manual.
If you are restricted to the core utilities, this uses tail
to skip the header line, cut
to isolate field 5, grep
to select versions of $5 that contain any "a", and tr
to delete any characters that are not "a" or newline, and then squeeze out consecutive multiple "a". All those are very useful utilities in their own right, and you should explore their capabilities if you wish to know Linux.
tail -n +2 ismael.data | cut -d ':' -f 5 | grep 'a' | tr -cds 'a\n' 'a'