I have a file with
aaa,bbb,ccc,dddd,eee,xyz
aaa,bbb,ccc,dddd,eee,xyz,12345,rty
aaa,bbb,ccc,dddd,eee,xyz,12345,rty,tsrt
- line columns are not fixed
- all words are seperated by comma
what i want is always the string after last comma.
regards,
Senthil Kumar Siddhan.
sed 's/.*,\(.*\)/\1/' myFile
could you please explain the command
.* - 0 or more characters
, - followed by a ','
\(.*\) - followed by 0 or more characters - \(...\) - sed's "capture"
\1 - print the FIRST "capture"
'man sed' for complete details.
Other way..
awk -F\, '{print $NF}' ${FILE}
i want last word after comma
$ cat kkl
aaa,bbb,ccc,dddd,eee,xyz
aaa,bbb,ccc,dddd,eee,xyz,12345,rty
aaa,bbb,ccc,dddd,eee,xyz,12345,rty,tsrt
$ awk -F\, '{print $NF}' kkl
xyz
rty
tsrt
thanks a lot Klashxx.
i did a mistake by typing ` instead of ' in the command.
it helped me a lot. thanks a lot.
...or, just using built-ins, to remove all characters up to and including the last comma on each line:
$ cat z.dat
aaa,bbb,ccc,dddd,eee,xyz
aaa,bbb,ccc,dddd,eee,xyz,12345,rty
aaa,bbb,ccc,dddd,eee,xyz,12345,rty,tsrt
while read s; do
print ${s##*,}
done <z.dat
cheers
can please someone explain me the diff between
It works perfectly fine to give last word
sed -e 's/.*,\([^,]\)/\1/' dat1
xyz
rty
tsrt
Shouldn't this also give the last word..i accidently tried it..but could get it to work.
why it only removes comma.should it keep on removing the whole word before comma..can someone plz explain this..
sed -e 's/[.]*,\([^,]\)/\1/g' dat1
aaabbbcccddddeeexyz
aaabbbcccddddeeexyz12345rty
aaabbbcccddddeeexyz12345rtytsrt