Reputation: 61
I need to GREP second column (path name) from the text file. I have a text file which has a md5checksum filepath size . Such as:
ce75d423203a62ed05fe53fe11f0ddcf kart/pan/mango.sh 451b
8e6777b67f1812a9d36c7095331b23e2 kart/hey/local 301376b
e0ddd11b23378510cad9b45e3af89d79 yo/cat/so 293188b
4e0bdbe9bbda41d76018219f3718cf6f asuo/hakl 25416b
the above is the text file, I used grep -Eo '[/]' file.txt
but it prints only / , but i want the output like this:
kart/pan/mango.sh
kart/hey/local
yo/cat/so
asuo/hakl
Lastly I have to use GREP.
Upvotes: 0
Views: 6844
Reputation: 19982
When you are allowed to combine grep
with other tools and your input file only has slashes in the second field, you can use
tr " " "\n" < file.txt | grep '/'
Upvotes: 0
Reputation: 5395
If you can live with spaces before and after, you can use:
grep -o "\s[[:alnum:]/]*\s"
If you need the spaces removed, you will need some zero-width look-ahead/look-behind which is only available with -P (perl regexes), if you have that you can use:
grep -Po "(?<=\s)[[:alnum:]/]+(?=\s)"
(?<=\s)
- look-behind to see if there is a space preceding the string, but not capture it(?=\s)
- look-ahead to see if there is a space after the match, but not capture it[:alnum:]
- match alpha numeric chars[[:alnum:]/]
- match alphanumeric chars and /
+
- match one or moreHowever, grep is not the right tool for this, cut/sed/awk are way better
Upvotes: 3
Reputation: 731
cut -d ' ' -f 2
-d ' ' means your delimiter is the space -f 2 meant you want to only print field number two
Upvotes: 1