So I have a file with the text:
puddle2_1557936:/home/I want to use the grep command
grep puddle2_1557936Mixed in with the cut command (or another command if neccessary) to display just this part:
/home/So far, I know that if do this
grep puddle2_1557936 | cut -d ":" -f1then it will display
puddle2_1557936So is there anyway to kind of "inverse" the delimiter cut command?
NOTE: The solution must start off with grep puddle2_15579636.
1 Answer
You don't need to change the delimiter to display the right part of the string with cut.
The -f switch of the cut command is the n-TH element separated by your delimiter : :, so you can just type :
grep puddle2_1557936 | cut -d ":" -f2Another solutions (adapt it a bit) if you want fun :
Using grep :
grep -oP 'puddle2_1557936:\K.*' <<< 'puddle2_1557936:/home/
/home/or still with look around regex
grep -oP '(?<=puddle2_1557936:).*' <<< 'puddle2_1557936:/home/
/home/or with perl :
perl -lne '/puddle2_1557936:(.*)/ and print $1' <<< 'puddle2_1557936:/home/
/home/or using ruby (thanks to glenn jackman)
ruby -F: -ane '/puddle2_1557936/ and puts $F[1]' <<< 'puddle2_1557936:/home/
/home/or with awk :
awk -F'puddle2_1557936:' '{print $2}' <<< 'puddle2_1557936:/home/
/home/or with python :
python -c 'import sys; print(sys.argv[1].split("puddle2_1557936:")[1])' 'puddle2_1557936:/home/
/home/or using only bash :
IFS=: read _ a <<< "puddle2_1557936:/home/"
echo "$a"
/home/js<<EOF
var x = 'puddle2_1557936:/home/
print(x.substr(x.indexOf(":")+1))
EOF
/home/php -r 'preg_match("/puddle2_1557936:(.*)/", $argv[1], $m); echo "$m[1]\n";' 'puddle2_1557936:/home/
/home/ 7