Tag Info

Hot answers tagged

12

Using GNU sed: sed -i '1d;$d' Element_query How it works : -i option edit the file itself. You could also remove that option and redirect the output to a new file or another command if you want. 1d deletes the first line (1 to only act on the first line, d to delete it) $d deletes the last line ($ to only act on the last line, d to delete it) Going ...


9

You should use grep --line-buffered percentage or else it will take a very long time for the grep stdout buffer to be filled by its output.


9

Use script(1) to log everything sent to the terminal: $ script Script started, file is typescript $ # do your work ... $ # then exit with ^D $ exit Script done, file is typescript You can later look at the output with less: $ less -r typescript Beware that the logs will contain all control characters sent to the terminal, such as ANSI colours or ...


7

In addition to Marco d'Itri's answer, not every tool supports custom buffering. In case the tool you're running doesn't, or in case you want to run it using a custom buffer size, you can use stdbuf to override the tool's buffering behavior; in this case, for example, to force the output to be line-buffered: while true ; do upower -d ; sleep 1 ; done | ...


6

Your understanding is correct. The sequence command1 | command2 is sending the output (STDOUT) of command1 to the input (STDIN) of command2. The reason your evince command didn't work is that evince doesn't accept a filename on STDIN.


5

The default field delimiter for cut is a tab. Since your file has a space instead, you need to specify the delimiter: -d ' ' And you really don't need to use cat or a pipe at all. Just read the file directly. cut -f 1 -d ' ' /proc/uptime


5

head; head { head -n[num] >/dev/null head -n[num] } <infile >outfile With the above you can specify the first number of lines to strip off of the head of the output w/ the first head command, and the number of lines to write to outfile with the second. It will also typically do this faster than sed - especially when input is large - ...


4

Okay, let's break this down. A subshell executes its contents in a chain (i.e., it groups them). This actually makes intuitive sense as a subshell is created simply by surrounding the chain of commands with (). But, aside from the contents of the subshell being grouped together in execution, you can still use a subshell as if it were a single command. That ...


3

You can read from stdout by redirecting input from file descriptor 1. Stdout is file descriptor 1 by definition. The fact that file descriptor 1 is used for output is a matter of convention, not a technical obligation. However it's a bizarre thing to do which is bound to confuse the people who use your script. read line <&1 If you want to read a ...


3

A pipe sends its output to the program that has it open for reading. In a shell pipeline, that's the program on the right-hand side of the pipe symbol, i.e. evince in your example. You're sending the file name tmp.pdf to evince on its standard input. However evince doesn't care about its standard input. Like every program that acts on a file, it expects the ...


3

Make the target log file a named pipe, with your filtering script on the other side.


3

ed is 'the standard text editor' and should be available on systems that don't have GNU sed. It was originally designed as a text editor, but it is well-suited to scripting. printf '%s\n' 1d '$d' w q | ed Element_query 1d deletes the first line of the file, $d (quoted so that the shell doesn't think it's a variable) deletes the last line, w writes the ...


2

Using awk: < inputfile awk 'NR>1 {print r}; {r=$0}' > outputfile < inputfile: redirects the content of inputfile to awk's stdin > outputfile: redirects the content of awk's stdout to outputfile NR>1: executes the following actions only if the number of the record being processed is greater than 1 {print r}: prints the content of the ...


2

You'd be far better served by cutting away the SQL commands. You can do this in two ways: If you are absolutely sure that the sequence "SQL>" does not occur anywhere else in the output, grep -v -F 'SQL> ' < infile > outfile If you aren't as sure, grep -v '^SQL> .*;$' < infile > outfile The second version is slower but more ...


2

I'm not going to answer how to delete a number of lines. I'm going to attack the problem this way: grep -v '#SQL>' Element_query >outfile Instead of counting lines, it eliminates the SQL commands by recognizing the prompts. This solution can then be generalized for other output files of SQL sessions with more commands than just two.


2

sort can take multiple input files (and has a built-in uniq equivalent -u). Combine that with a fancy bash process substitution to result in: sort -u <(tshark -r sample.pcap -T fields -e eth.src -e ip.src) <(tshark -r sample.pcap -T fields -e eth.dst -e ip.dst) > hello_uniq


2

(tshark -r sample.pcap -T fields -e eth.src -e ip.src; tshark -r sample.pcap -T fields -e eth.dst -e ip.dst) | sort | uniq > hello_uniq


2

The yash shell has unique features (pipeline redirection and process redirection) that make that easier there: cat myfile | ( exec 3>>|4 tee /dev/fd/3 3>(commanda) | commandb 3>&- | paste -t',' /dev/fd/4 - 3>&- ) 3>>|4 (pipeline redirection) creates a pipe where the writing end is on fd 3 and the reading end on fd ...


2

Yes, OpenSolaris includes the Bourne Shell source but that source is not portable. A maintained and highly portable version of the Bourne Shell source can be found here in the schily-*.tar.bz2 archives. Here is the related part of the source in cmd.c: /* * ^ is a relic from the days of UPPER CASE ONLY tty model 33s */ if ((t = item(TRUE)) != 0 ...


2

Scheduling doesn't matter much at all. If you consider piping an hour long job, the pipe remains open continuously from when the first program is started until it finishes. The second program runs exactly the same time as the first one and both finish at the same time (in general).. In other words, ps does not get the output then send it to grep - the two ...


2

This question is a duplicate and belongs to unix.stackexchange.com. To sum up, still, the OpenGroup's Shell Command Language doc is relatively vague on details regarding "pipelines": A pipeline is a sequence of one or more commands separated by the control operator '|'. The standard output of all but the last command shall be connected to the standard ...


2

With any POSIX sed you can achieve line-buffered output w/ the write command. while sleep 1 do upower -d done | sed -n /percentage/w\ /dev/fd/1 | uniq ...will work on systems supporting the /dev/fd/[num] links (such as practically any linux system). But you could just do: while sleep 1 do upower -d done | sed '/percentage/!d;H;x ...


2

You are almost there. In your last command, you can use -I to do the ls correctly -I replace-str Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1. So, with find . ...


2

You can select the lines between a range in awk (this assumes you know how many lines there are): awk 'NR>1 && NR < 3' file Or in Perl: perl -ne 'print if $.>1 && $.<3' file If you don't know how many lines there are, you can calculate it on the fly using grep (note that this will not count blank lines, use grep -c '' file ...


2

You don't have to get the whole lump if you don't want to - you can chunk out stdin and work it as a stream if you like. I did some googling, though, and I don't think I can offer you any advice on how to change the sex of your script, or whatever it is you meant by polymorphic. In any case, I typically find that putting the whole of input aside in some ...


2

If you want to read all of stdin into a shell script, usually you just capture it into a temp file: TMPFILE=$(mktemp /tmp/$0.$$.XXXXXX) cat > "$TMPFILE" # Script works with $TMPFILE and its contents, # ultimately writing everything to stdout. rm -f "$TMPFILE" Even system utilities do things very much like this. sort has to have all of stdin before it ...


2

That's the effect of MULTIOS. echo foo >&2 | grep foo will write foo to stderr and also pipe foo to grep. Because stderr defaults to terminal, you will see two foo lines, one from echo, one from the grep result. { echo foo >&2 | grep foo } >/dev/null Here, you saw one line because stdout was redirected to /dev/null, you only saw the ...


2

The second awk reads two inputs, one after the other - from the piped output of the first awk and then from the file itself. One way to identify the start of the second input is that NR (the Number of the current input Record, overall) no longer matches FNR ( the current File's record number). Note that - as a FILE arg means tells awk to get the data from ...


1

I can't find a way with the pipe but you can try this: obexftp -b 10:68:3F:57:7D:B6 - p $(cat inputFile.tar) Return the sdout of the command in the $( )


1

The commands you're using can only extract data from the first entry in a ZIP archive; this is mentioned explicitly in the funzip manpage: funzip without a file argument acts as a filter; that is, it assumes that a ZIP archive (or a gzip'd(1) file) is being piped into standard input, and it extracts the first member from the archive to stdout. ...



Only top voted, non community-wiki answers of a minimum length are eligible