问题描述:

*the purpose of using sed because i want to read it line by line streaming.

I have few questions about unix bash and ruby, I am really confused about the syntax of bash.If someone can offer me some examples, it really helps people to learn. Appreciate every responses.

1.let say I want to read in a server.txt on the command line, and then sort it by the server contents aplhabetically

here is how server.txt looks like



pacific

asia

northamerica

america

Here what I typed

sed -e server.txt |sort| > done.txt

but it seems not working well, how to fix it?

2.And my second question is how to I combine the use of unix command and ruby script.

like i want to stream in a file and then input line by line input to my ruby file. and then output to some files.

I tried

sed -e server.txt ruby filter.rb > done.txt

this doesn't work too.

3.what is $stdin $stdout mean?

4.if server.txt is like 10gb which doesn't fit in memory, can we still use sort? or sort is like automatically doing the streaming and merging for us?

网友答案:
  1. If what you typed is precisely:

    sed -e server.txt |sort| > done.txt
    

    you should be getting a syntax error from sed complaining that the third 'e' is missing from 'server.txt' (because it will treat that as an attempt at formulating a substitute command with 'e' as the expression delimiter; the -e option means the following argument is a command expression, not a file name).

    The second pipe should be omitted. As written, you've invoked a null command that creates a file done.txt and then does nothing. If you wanted the output written to the file, you'd need to put a cat or tee command in there. Or, better, omit the second pipe so that sort writes to the file.

    Can you give an example of how to use cat or tee to make first example work?

    sed -e s/a/b/g server.txt | sort | cat > done.txt  # UUOC award
    sed -e s/a/b/g server.txt | sort | tee   done.txt
    sed -e s/a/b/g server.txt | sort       > done.txt  # No UUOC award
    sed -e s/a/b/g server.txt | sort      -o done.txt  # No UUOC award
    

    I've added an expression for sed so it will read the file server.txt. UUOC is the 'Useless Use of Cat' award. There's absolutely no need to use cat in the example. Using tee might make sense if you're planning to read all 10 GiB of data as it scrolls past; otherwise, one of the last two options is more sensible; they're essentially equivalent in this context.

  2. Your second command:

    sed -e server.txt ruby filter.rb > done.txt
    

    should be written as:

    sed -e s/a/b/g server.txt | ruby filter.rb > done.txt
    

    I've provided a silly action for the -e option to sed; the output of sed is piped to the ruby script, and the output is then written to done.txt. This is a very standard sort of idiom.

  3. Without more context, I can't tell you what $stdin and $stdout mean. In different languages, it means slightly different things, but typically would be a reference to a variable of some sort. I'm not sure whether any languages treat those two names as special; probably there are some, but neither Perl nor shell has a special meaning for those names.

相关阅读:
Top