Operating System - HP-UX
1848459 Members
8227 Online
104029 Solutions
New Discussion

rows for which the last letter ends

 
SOLVED
Go to solution
ram kumar_4
New Member

rows for which the last letter ends

Please help me out

I want a unix shell script command to do the following:
- Read this x1.txt and create 3 separate files a.dat, b.dat and
c.dat
- a.dat should have all the rows for which the last letter ends
with
"A"
- b.dat should have all the rows for which the last letter ends
with
"D"
- c.dat should have all the rows for which the last letter ends
with
"C"

Thanks
3 REPLIES 3
Simon Hargrave
Honored Contributor
Solution

Re: rows for which the last letter ends

grep "A$" x1.txt > a.dat
grep "D$" x1.txt > b.dat
grep "C$" x1.txt > c.dat

The $ in the expression matches end of line and is anchored to your letter.
Hein van den Heuvel
Honored Contributor

Re: rows for which the last letter ends


The simple multiple greps should do.
But if somehow (pipe!) you can only go over the data once, then you coudl use perl or awk like so:


awk 'BEGIN {f["A"]="a.dat";f["D"]="b.dat";f["C"]="c.dat"} {print $0 >> f[substr($0,length($0))]}' all.dat


Hein.

Muthukumar_5
Honored Contributor

Re: rows for which the last letter ends

We can identify the pattern on a file which occurs on the input file with $

Example:


x1.txt
------

power script eA
bye for now
Test enviginee yeB
ncie choke C
test byeD
over


grep 'A$' x1.txt > a.dat
grep 'D$' x1.txt > b.dat
grep 'C$' x1.txt > c.dat

You can get multiple patterns as,

grep 'A$|B$' x1.txt > ab.txt

HTH.
+Muthu+
Easy to suggest when don't know about the problem!