I have these data in a file:
one,1,/home/steven/Transformation/users.txt
two,2,/home/steven/Transformation/users.txt
And I want ouptut like this:
one,1,users.txt
two,2,users.txt
How to handle this using awk
awk -F '[,/]' '{print $1","$2","$NF}'
Or with using OFS
awk -F '[,/]' -v OFS=',' '{print $1, $2, $NF}'
awk -F '[,/]' -v OFS=',' '{print $1, $2, $NF}'
You can also use sed:
sed 's;/.*/;;' file
Or with cut and sed:
cut -d / -f1,5 file | sed 's;/;;'
cut and tr
tr '/' ',' < file | cut -d , -f1,2,7
Output:
one,1,users.txt
two,2,users.txt
For this input:
/one,/1,/home/steven/Transformation/users.txt
/two,/2,/home/steven/Transformation/users.txt
The command:
sed -E 's;(.*,)/.*/;\1;' file
Output:
/one,/1,users.txt
/two,/2,users.txt
Method1
awk -F "," 'OFS=","{print $1,$2,substr($NF,29)}' filename
Method2:
sed "s/\/.*\///g" filename
Method3:
#!/usr/bin/python
import re
k=re.compile(r'/.*/')
p=open('p.txt','r')
for i in p:
print re.sub(k,"",i).strip()