Tag Info

Hot answers tagged

21

There's a general buffering rule followed by the C standard I/O library (stdio) that most unix programs use. If output is going to a terminal, it is flushed at the end of each line; otherwise it is flushed only when the buffer (8K on my Linux/amd64 system; could be different on yours) is full. If all your utilities were following the general rule, you would ...


8

There are four ways to include the single quote that you need. One cannot escape a single-quotes string within a single-quoted string. However, one can end the quoted string, insert an escaped single-quote, and then start a new single-quoted string. Thus, to put a single quote in the middle of 'ab', use: 'a'\''b'. Or, using the sed command that you need: ...


6

awk -F":" '{OFS=":"; print $1,$2,$3,$4-5,$5+5}' filename Output: 1314:Battery:1.90:45:35


6

You can use this syntax: "${var:-word}" This will substitute the value of the variable $var if it is set and not empty and, if not, will substitute with whatever is given by as word. For example: $ var=foo $ echo "${var:-bar}" foo $ var= $ echo "${var:-bar}" bar So, in your specific case, you can use: echo "${var:--}" Or, the safer and more ...


6

It's a lot easier done with perl. To change the 3rd occurrence: perl -pe 's{is}{++$n == 3 ? "us" : $&}ge' To change every 3rd occurrence: perl -pe 's{is}{++$n % 3 ? $& : "us"}ge'


5

perl -ane 'print if grep {$_ >= 16} ($F[5] =~ /(\d+)S/g)' file outputs c 256 gene3 55 0 6S27M17S * d 16 gene4 110 9 19S25M6S * e 272 gene5 141 9 23S21M6S * f 272 gene6 139 9 24S20M6S * That finds all the digits followed by "S" in the 6th field. If any are greater than or equal to 16, it prints the line. Look ...


5

Remove while loop and make use of shell brace expansion and also FNR, a built-in awk variable: awk 'FNR==2{print $0 > "output.dat"}' file{1..80}.dat


5

sed ":a;/\r$/{N;s/\r\n//;b a}" This will match all lines that have '\r' at the end (followed by '\n'). On these lines it will first append the next line of input (while putting the '\n separator in between), then replace the resulting "\r\n" with an empty string, and then goes back to the beginning to see, whether the new contents of pattern space doesn't ...


5

You can use c flag when doing search and replace: :%s/foo/bar/gc Each time vim found foo, it will prompt yes/no to confirm replacing or not. Or if you want to search for the nth occurrence of foo, you can: n/foo Then vim will jump to nth occurrence of foo, so you can decide to replace or not.


4

For calculations awk is more suitable awk 'NR!=1{print a,b-$2};{a=$1;b=$2}' If you'd like have a third column with difference just add it: awk 'NR!=1{print a,b,b-$2};{a=$1;b=$2}'


4

If I read you correctly: Search /fooEnter Next by n “Oh, this one I want to delete” dw Continue by n “Oh, this one I want to change to bar” cwbarEsc Continue by n ... “Oh, this one I want to change to baz” cwbazEsc Continue by n ... Edit / Correction: For convenience I have added: imap <C-d> <ESC> to .vimrc - had forgotten about ...


4

awk '{print $NF}' sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//' That would still print an empty line for every blank line. To avoid it: awk 'NF{print $NF}' sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//;/./!d'


4

This actually took me some thought to understand and even more to answer. Great question (I'll upvote it next). You neglected to try tr | sed in your debugging items above: >tr '[:lower:]' '[:upper:]' | sed 'p' i am writing still writing now ctrl-d I AM WRITING I AM WRITING STILL WRITING STILL WRITING NOW CTRL-D NOW CTRL-D > So evidently tr ...


3

With perl: perl -i -F: -lape '$F[3]-=5;$F[4]+=5;$_=join ":",@F' the-file With sh (assuming those numbers are always decimal integers without leading zeros): IFS=: read -r a b c d e < the-file && printf '%s\n' "$a:$b:$c:$((d-5)):$((e+5))" > the-file With recent versions of GNU awk: gawk -i inplace -F: -vOFS=: '{$4-=5;$5+=5}1' the-file


3

You could use sed to filter the output (of any command/script) and print every other line and double space the final output: print only odd-numbered lines: command | sed n\;g print only even-numbered lines: command | sed g\;n command can be for ... done


3

ps xao pid,ppid,s | grep '^ *[5-8]' If the PID is five digits, you don't have a space at the beginning of the line, hence the ' *' part after grep. '^'^ only searches at the beginning (thereby not selecting PPID is starting with 5,6,7,80 and. '[5-8]' handles the range of numbers you wanted (could also do '[5678]')


3

sed -e 's/& PID=\$\!;$//' The $ toward the end anchors it to the end of the string.


3

sed would be enough: sed -sn 2p file{1..80}.dat > output.dat -s option is needed to print 2nd line from each file, otherwise only 2nd line of first file will be printed.


3

The pattern argument to grep is in fact a newline-separated list of patterns. Thus grep $'\r\n' searches either a CR or the empty pattern (which matches every line). This is the same as grep $'\n' which searches the empty pattern or the empty pattern. To search for a CRLF sequence, search for a CR at the end of the line. grep -c $'\r$' file


3

That's typically where you'd use the hold space: ls | sed ' /\.png$/!d; # discard everything but lines ending in .png s///; # remove that .png h; # store on the hold space s/_//g; # remove underscores H; # append (with a newline) to the hold space g; # retrieve that hold space s|\n|/|; # substitute the ...


3

The awk variable $NF is the last field of every record; you can use it to print only the last fields of your file like so: awk '{print $NF}' file


3

To expand on what @Bratchley said in the comments, if you have your program's output printing to a file, then you can run then watch command in the terminal to get near-real-time view of the output by including the -n flag like so: watch -n 0.1 "cat yourprograms.log | sort | uniq -c | sort -rn" Note: The ' -n ' flag sets the refresh interval. The ...


3

This looks more like a python job to me. A general rule of thumb is: if your task requires only a "flat" and content-blind processing, core utilities (preferably gnu) are the way to go. This goes for string replacement, deletion, line-based processing, simple sorting, counting, filtration and so on... these tools allow you to very quickly write a one-liner ...


3

Stéphane gave you the sed solution: sed -n '\|file /etc|=' file If you're open to using other tools, you can also do grep -n 'file /etc' file That will also print the line itself, to get the line number alone try: grep -n 'file /etc' file | cut -d: -f 1 Or, you can use perl: perl -lne 'm|file /etc| && print $.' file Or awk: awk '$0 ~ ...


3

In a context address, you have to escape the opening delimiter, unless you're using the default /. Any following occurrences that are escaped are treated as the literal character, not as a delimiter. See the POSIX docs: In a context address, the construction \cREc where c is any character other than a backslash or newline character, is identical to ...


2

Maybe a more straight forward, purely awk answer would be to use split. Split takes a string and turns it into an array, the return value is the number of array items generated + 1. The following code will print out the number of times " appears on each line. awk ' {print (split($0,a,"\"")-1) }' file_to_parse more info on split ...


2

Using paste paste -d'\n' - - /dev/null <file


2

The following script ought to do an outer join on column (field) 1 of all the tab-delimited files passed as arguments. It uses the join command, which does an outer join on sorted files, 2 files at a time. It will join every line in the files, including the header lines. If you want the headers to be excluded, change the two sort commands to something that ...


2

You can use GNU grep's Context Line Control, from man grep: -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given. ...


2

When you work globally you've got to consider how sed reads. A global substitution is going to divide up a pattern space into individual fields delimited per your specifications and operate on each. The delimiters are recognized from left to right - in the order they are read - and sed will apply each action as soon as it might. Here is a single ...



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