Hanna Gabby
Hanna Gabby

Reputation: 57

shell scripting using sed

I want to read from standard input, delete all '/', and write the output to standard output. So, a file that contain:

/ab1/1a6/ 17
/a/b/1

will have output:

ab11a6 17
ab1

I think it should be something like this:

read input
sed -r 's/\/[^\/]*\/[^\/]*\/.*/"I not sure what do I need to put in here"/g'
echo $input

I don't really know what do I need to put the the "replace" section. any suggestion?

Upvotes: 1

Views: 2228

Answers (3)

jim mcnamara
jim mcnamara

Reputation: 16399

The simplest program to do what you've asked is:

 tr -d '/' 

If you enter that you will get what you wanted for output - no / characters You do not need a read or echo statement

sed will behave the same as others have shown, tr & sed read from stdin and write to stdout by default:

sed 's/\///g'

Upvotes: 2

Adrian Pronk
Adrian Pronk

Reputation: 13926

sed 's./..g'

This will delete all / characters on each line, so you don't need a replace section. You can either use the / as the delimiter and escape it in the text: s/\///g or choose a different punctuation symbol as a delimiter: s./..g

So if you want to transform a file called input.txt and write the output to output.txt:

sed 's./..g' input.txt > output.txt
# or
sed 's./..g' < input.txt > output.txt

Upvotes: 3

hd1
hd1

Reputation: 34677

Nothing... as shown below:

% echo "/ab1/1a6/ 17" | sed 's/\///g'
ab11a6 17

Upvotes: 1

Related Questions