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
.
To reverse a String in Bash, iterate over the characters of string from end to start using a For loop or While Loop, and append characters such that the order of characters is reversed. Or we can also use rev command to reverse a string.
grep just filters, man grep : print lines matching a pattern , to change order another tool must be used, as there's only two items maybe adding |sort or |sort -r (sort reverse) can help. Show activity on this post.
Approach to find The Solution Find the length of the given string. Traverse the string from [length - 1]th index to 1 using a for a loop. Append every character to the reversed_string variable. Finally, print the reversed_string using echo.
The cut command is used to extract the specific portion of text in a file. Many options can be added to the command to exclude unwanted items. It is mandatory to specify an option in the command otherwise it shows an error.
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 :
grep -oP 'puddle2_1557936:\K.*' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2
or still with look around regex
grep -oP '(?<=puddle2_1557936:).*' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2
or with perl :
perl -lne '/puddle2_1557936:(.*)/ and print $1' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2
or using ruby (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 :
awk -F'puddle2_1557936:' '{print $2}' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2
or with python :
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 bash :
IFS=: read _ a <<< "puddle2_1557936:/home/rogers.williams/folderz/puddle2"
echo "$a"
/home/rogers.williams/folderz/puddle2
or using js in a shell :
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 php in a shell :
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With