22

So I have a file with the text:

puddle2_1557936:/home/rogers.williams/folderz/puddle2

I want to use the grep command

grep puddle2_1557936

Mixed in with the cut command (or another command if neccessary) to display just this part:

/home/rogers.williams/folderz/puddle2

So far, I know that if do this

 grep puddle2_1557936 | cut -d ":" -f1

then it will display

puddle2_1557936

So is there anyway to kind of "inverse" the delimiter cut command?

NOTE: The solution must start off with grep puddle2_15579636.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
DeaIss
  • 2,525
  • 7
  • 27
  • 37
  • 4
    minimal fix is to use `-f2` with `cut`. Btw why must it start with `grep puddle2_15579636`, will something bad happen if it doesn't? – doubleDown Jun 19 '13 at 22:14
  • Thank you very much! Didn't know the fix was that easy...haha. Also it had to start with grep puddle2_15579636 because I'm writing quite a long script, and at that point in the script, the only information available would be that! Its kind of hard to explain unless you see the whole project! – DeaIss Jun 19 '13 at 22:21

1 Answers1

56

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 ":" -f2

Another solutions (adapt it a bit) if you want fun :

Using :

grep -oP 'puddle2_1557936:\K.*' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'                                                                        
/home/rogers.williams/folderz/puddle2

or still with look around

grep -oP '(?<=puddle2_1557936:).*' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'                                                                    
/home/rogers.williams/folderz/puddle2

or with :

perl -lne '/puddle2_1557936:(.*)/ and print $1' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'                                                      
/home/rogers.williams/folderz/puddle2

or using (thanks to glenn jackman)

ruby -F: -ane '/puddle2_1557936/ and puts $F[1]' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2

or with :

awk -F'puddle2_1557936:' '{print $2}'  <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2

or with :

python -c 'import sys; print(sys.argv[1].split("puddle2_1557936:")[1])' 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2

or using only :

IFS=: read _ a <<< "puddle2_1557936:/home/rogers.williams/folderz/puddle2"
echo "$a"
/home/rogers.williams/folderz/puddle2

or using in a :

js<<EOF
var x = 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
print(x.substr(x.indexOf(":")+1))
EOF
/home/rogers.williams/folderz/puddle2

or using in a :

php -r 'preg_match("/puddle2_1557936:(.*)/", $argv[1], $m); echo "$m[1]\n";' 'puddle2_1557936:/home/rogers.williams/folderz/puddle2' 
/home/rogers.williams/folderz/puddle2
Community
  • 1
  • 1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223