-5

Basically I want to get rid of the forward slashes but I'm not as familiar with sed/awk as I should. Can someone help me out here.

Edit: I'd also like to mention that the worlist is over 1000 lines and not all of them have the forward slashes appended to them. So I need to basically loop through it, and when it hits a forward slash, remove it.

-agile/
-agility
-agnosticism
-ahead
-akeneo
-alerts/
-all
-all/
  • 2
    Read the "s" Command in GNU sed manual. Attention to 5th paragraph. – Quasímodo Sep 4 at 22:01
  • I understand how to replace one string with another (after reading the manual). But what confuses me is that I want to replace / with an empty string or nothing. You know what I mean? Do I just leave that part blank? – ntr0v3rt Sep 4 at 22:52
  • @John1024 Unfortunately not – ntr0v3rt Sep 4 at 22:55
  • @ntr0v3rt What did you try? – John1024 Sep 4 at 23:14
  • Pls can you provide a desired output? – Mreyes Sep 4 at 23:14
1

Removing a character can be done with several different tools. Here's one version

tr -d '/' <wordlist
| improve this answer | |
0

Welcome to Unix & Linux forum.

roaima's answer it works.

This is mine using sed.

sed -e 's#/$##' file

edit: If you want to remove them permanently (carefully) only add the g parameter like:

sed -i -e 's#/$##g' file

Remember always creating a backup before delete patterns or modify; As a good habit.

| improve this answer | |
  • 2
    Some parts of this are kind of misleading. Use of g is completely meaningless, since the pattern can only match at most once per line. The permanence is achieved through -i. Bear in mind, not all sed have that switch, and on some systems this doesn't work as intended — your command as written produces a new file called file-e on macOS for example – Fox Sep 5 at 1:08
  • Woah, I didn't realize it. Thanks for suggestion. – Mreyes Sep 6 at 2:03
0

Done by 3 methods

command

awk method

awk '{sub(/\//,"",$0);print }' file


sed method

sed "s/\///g" file


Python

#!/usr/bin/python
k=open('file','r')
for i in k:
    print i.replace("/","").strip()

output

-agile
-agility
-agnosticism
-ahead
-akeneo
-alerts
-all
-all
| improve this answer | |
  • Even tho I'm not very good at python I still know the basics, and can write simple scripts. So it crossed my mind that python might be the best option, however I didn't know exactly what to write because my mind blanked when I got started so seeing this gives me hope. Bash has yet to produce positive results so I'll see if this works, and then comment yea or nae. – ntr0v3rt Sep 6 at 1:23
  • 1
    @ntr0v3rt you didn't ask for a bash solution and you haven't received any bash answers. All answers so far use other UNIX tools (e.g. awk, sed, tr), not bash. Bash would be a poor choice to do this. As you can see from all of the answers you got using standard UNIX tools, what you're trying to do is trivial with any of them and can be done far more concisely with them than with python so idk why you're thinking none of them work or that python would be a better approach. – Ed Morton Sep 6 at 13:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.