## Posts Tagged ‘cat’

### Copy text file without hard-wrap new lines

Friday, February 28th, 2014

I often format small text entries in vim and then copy them into other places like web forms. In vim I like to have a hard 80 character line wrap. But this means that after every 80 characters I have a newline character. If I just copy the file or from the terminal screen then I’ll impose this hard wrap in the submitted text. I’ve noticed that this is especially bad for academic review submissions because the submission system might then additionally impose its own hard wrap at a different width causing a very staggered, ragged appearance.

Here’s my solution to copy a text file without newlines but keeping double newlines which indicate paragraphs.

cat % | perl -pe '$/=""; s/\n([^\n])/ \1/g;' | pbcopy  There must be a way to do this inside of vim properly, but I couldn’t figure it out. ### One liner to add public key to authorized ssh keys Monday, November 5th, 2012 cat ~/.ssh/id_rsa.pub | ssh jacobson@pascal.cs.columbia.edu 'mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys'  ### Split (large) files in half using bash script Monday, June 21st, 2010 Here’s a simple bash script that splits a given file into two equal (up to a byte) halves. It uses split which has different syntax between the GNU and BSD implementations, so see the comments for switching between the two. I tested this on a 6.4 GB dmg file and I was able to reverse the split with cat just fine. I diffed the re-concatenated file and the original and they were byte-wise equal. Save this in a file called half.sh:  #!/bin/bash # # usage: half foo # # This will create foo.aa and foo.ab, where foo.aa is the first half(+1) of foo # and foo.ab is the second half. Split by bytes not lines. # # # To reverse just issue: # cat foo.aa foo.ab > foo # # get file size # GNU: #size=stat -c "%s" "$1"
# BSD:
size=stat -f "%z" "$1" # divide by 2 and round up half_size=echo "($size+1)/2" | bc
split -b $half_size "$1" "$1."  ### Escape code for html <pre> or <code> tag into clipboard: Ruby Version Thursday, November 5th, 2009 I previously posted a bash script to speed up posting code in a <pre> or <code> tag in an html page or blog. The script escapes all less than and greater than symbols (< with &lt; and > with &gt;) in a given file then puts the results in the clipboard to facilitate pasting into a text area or text editor. I have now rewritten this idea in ruby. Now you can either cat in a file to the ruby script using pipe, or enter (paste) the input text directly. Save the following the ruby source in a file called escape-copy.rb:  #!/usr/bin/ruby -w # Usage: # cat input_file | ruby escape-copy.rb | pbcopy # or # ruby escape-copy.rb | pbcopy # (enter or paste your text then CTRL-D on its own line as EOF) print ARGF.read.gsub(/</,"<").gsub(/>/,">")  Note: I of course used escape-copy.rb to make posting the source above super easy! ### Escape code for html <pre> or <code> tag into clipboard Thursday, August 13th, 2009 Here’s a speed up for posting code in a <pre> or <code> tag in an html page or blog. The script escapes all less than and greater than symbols (< with &lt; and > with &gt;) in a given file then puts the results in the clipboard to facilitate pasting into a text area or text editor. Save this in a file called escape-copy.sh:  #!/bin/bash cat "$1" | sed s/\</\\\<\;/g | sed s/\>/\\\>\;/g | pbcopy


Posting the above code was super easy now, I just ran ./escape-copy.sh escape-copy.sh and pasted it in a <pre> tag.

Thursday, July 30th, 2009

For educational purposes only:
Here is a sketch for a script that works with Safari and shell to “click” on all the ads you see while surfing the web. These visits to the advertisers pages (after first being rerouted by the pay-per-click counter, in this case google.com) happen behind the scene. So to the advertiser and the host of the ads it seems as though you clicked on the ad you saw, but to you your surfing went undisturbed.

Save the following in a file called clickads.sh:


#!/bin/bash
SOURCE=cat -
ADS=echo $SOURCE | grep -o "/aclk?[^\"]*" | \ sed s/amp\;//g | sed s/^/http:\\\/\\\/google.com/g for x in$ADS
do
/sw/bin/wget -q -erobots=off -t2 -O - -U "Mozilla/5.0 \
(Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/530.17 \
(KHTML, like Gecko) Version/4.0 Safari/530.17" "\$x" 2>&1 | \
done


Note: change your absolute path to wget accordingly (find what it is by issuing which wget in a terminal).

clickads.sh will accept the html source of a google search, strip out all of the side bar ads and “click” each one. Remove the log and change your -U User agent accordingly (remember this is not for use and for educational purposes only).

Now, so that the background clicker only “clicks” on ads you actually see, save and run this as an Applescript:


repeat
try
tell application "Safari"
set old_current_source to ""
set current_source to ""
repeat until old_current_source is equal to current_source and current_source is not equal to ""
set old_current_source to source of front document as string
delay 1.0
set current_source to source of front document as string
end repeat
set current_url to URL of front document as string
end tell
if current_url starts with "http://www.google.com" then
set newFile to open for access newFileName with write permission
set eof of newFile to 0
write current_source to newFile
close access newFile

The path to the dump temp file ".behindthescenesadclick.temp" has to be in Applescript format not UNIX. And remember to change the path to ~/Bash/clickads.sh to point to your file.