sed doesn't work from within bash script

2019-06-21 15:42发布

I've searched for hours looking for the answer to this question which seems frustratingly simple...

I have a bash script which I've simplified to find the line that's stopping it from working and am left with:

#!/bin/bash
#
sed -i -e "s/<link>/\n/g" /usb/lenny/rss/tmp/rss.tmp

If I run this script, nothing happens to the file rss.tmp - but if I call this exact same sed command from the terminal, it makes all the replacements as expected.

Anyone have any idea what I'm doing wrong here?

标签: bash replace sed
4条回答
▲ chillily
2楼-- · 2019-06-21 16:18

Based on the discussion the issue sounds like it is a cygwin shell problem. The issue is that shell scripts may not have \r\n line terminations - they need \n terminations. Earlier versions of cygwin behaved differently. The relevant section from a Cygwin FAQ at http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html

Q: Mysterious errors in shell scripts, .bashrc, etc


    A: You may get mysterious messages when bash reads
    your .bashrc or .bash_profile, such as
        "\r command not found"
    (or similar).  When you get rid of empty lines, the
    complaints about "\r" disappears, but probably other
    errors remain.  What is going on?

    The answer may lie in the fact that a text file (also
    called ASCII file) can come in two formats:
    in DOS format or in UNIX format. 
    Most editors can automatically detect the formats
    and work properly in either format.
    In the DOS format, a new line is represented by two characters:
    CR (carriage return or ASCII code 13) and LF (line feed or ASCII code 15).
    In the UNIX format, a new line is represented by only
    one character, LF.  When your .bashrc file is read,
    bash thinks the extra character is the name of a command,
    hence the error message.

    In Cygwin or unix, you can convert a file INFILE in DOS format
    to a file OUTFILE in Unix format by calling:

        > tr -d '\15'  OUTFILE

    NOTE:
    If you now compare the number of characters in INFILE and OUTFILE,
    you will see that the latter has lost the correct
    number of characters (i.e., the number of lines in INFILE):

        > wc INFILE OUTFILE
查看更多
来,给爷笑一个
3楼-- · 2019-06-21 16:21

to feed a file to the command you use "<", while to make a file u use ">" and sed is used as text formater not editor as far as i know maybe something like this should work

cat < /usb/lenny/rss/tmp/rss.tmp | sed -i -e "s/<link>/\n/g" > /usb/lenny/rss/tmp/rssedit.tmp

cat gets the file and with sed editing it and ouput goes to rssedit.tmp

than check if rssedit.tmp has what u wanted

if it does and only if it does

next line of the your skript should be

mv /usb/lenny/rss/tmp/rssedit.tmp /usb/lenny/rss/tmp/rss.tmp 

which will replace made 1 with original, with renameing to original

查看更多
做自己的国王
4楼-- · 2019-06-21 16:27

You need to give an output file or the result will be only shown on the screen.

sed -e 's/<link>/\n/g' /usb/lenny/rss/tmp/rss.tmp > /usb/lenny/rss/tmp/output.tmp
查看更多
我想做一个坏孩纸
5楼-- · 2019-06-21 16:37

Try using that instead:

sed -i -e "s/\<link\>/\n/g" /usb/lenny/rss/tmp/rss.tmp
查看更多
登录 后发表回答