Your better option here is actually sed
, because it edits the stream on a line by line basis.
Try the following:
sed 's/\/.*//' foo.txt > foo_filter.txt
This tells sed
that - per line - replace anything after the /
with nothing. You then redirect the output to the new file with the >
. You can read more from the sed manual here.
Note: because sed
is greedy, you might need to specify the first slash with a 1
at the end of the command:
sed 's/\/.*//1' foo.txt > foo_filter.txt
You definitely can use awk
if you have strings with multiple slashes:
awk -F"/" '{print $1"/"}' foo.txt > foo_filter.txt
The -F"/"
sets the field delimiter to forward slash, and '{print $1"/"}'
prints the first field followed by a slash (since it's the field delimiter, it gets removed on print and has to be re-included).
unix.stackexchange.com
? and not somethingunix.stackexchange.com/questions
for example. then what output you are expecting? skip those lines or print the URL? – αғsнιη Feb 10 '23 at 04:02https://
likehttps://unix.stackexchange.com/help/someone-answers
? – αғsнιη Feb 10 '23 at 04:13/
string, there up to.com
string) and in both they wanted that strings to be included in the result. so no doubt from me that these are exact duplicates. – αғsнιη Feb 10 '23 at 07:31.com
). please read the Q there slowly then you will find it. in addition you can compare your answer with answers there. both Q and A are duplicates except you have an extra cut approach which that answer even is not what OP wanted here but later approaches (printing the split character) is – αғsнιη Feb 10 '23 at 07:52