shell scripting using sed

2019-08-04 03:59发布

问题:

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?

回答1:

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


回答2:

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'


回答3:

Nothing... as shown below:

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


标签: shell