-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/
Ed Morton
  • 31,617

3 Answers3

1

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

tr -d '/' <wordlist
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
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.

Mauricio Reyes
  • 359
  • 2
  • 5
  • 19
  • 3
    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 05 '20 at 01:08
  • Woah, I didn't realize it. Thanks for suggestion. – Mauricio Reyes Sep 06 '20 at 02:03
  • in addition to what @Fox said, there is also no need for the ´-e´ option, since your first non-option parameter is already the script and the following non-option parameter is already the input file (refer to the manual for more information). what I do like is that this answer is providing an alternative to escaping the forward slash, but I could only upvote if the issues are fixed – nonthevisor Feb 22 '21 at 08:42
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
  • 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 06 '20 at 01: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 06 '20 at 13:04