Sed stops parsing at the match and so is much more effecient than piping head into tail or similar. Grab a line range using
sed '999995,1000005!d' < my_massive_file
The hyphen tells vim to open from STDOUT - saves having to create temporary files.
wraps text lines at the specified width (90 here). -s option is to force to wrap on blank characters -b count bytes instead of columns
this method should be the fastest
Prints the 4th line and then quits. (Credit goes to flatcap in comments: http://www.commandlinefu.com/commands/view/6031/print-just-line-4-from-a-textfile#comment.)
It's works only when you replace '\n' to ONE character.
Even shorter. Stolen from comment posted by eightmillion.
Use this BASH trick to create a variable containing the TAB character and pass it as the argument to sort, join, cut and other commands which don't understand the \t notation.
sort -t $'\t' ...
join -t $'\t' ...
cut -d $'\t' ...
Show Sample Output
Very useful when the ssh key of a host has changed and ssh refuses to connect to the machine, while giving you the line number that has changed in ~/.ssh/known_hosts.
You can then switch from a file to another with ^W^W
There is a common command for outputting a field or list of fields from each line in a file. Why wouldn't you just use cut?
This is the solution to the common mistake made by sudo newbies, since
sudo echo "foo bar" >> /path/to/some/file
does NOT add to the file as root.
Alternatively,
sudo echo "foo bar" > /path/to/some/file
should be replaced by
echo "foo bar" | sudo tee /path/to/some/file
And you can add a >/dev/null in the end if you're not interested in the tee stdout :
echo "foo bar" | sudo tee -a /path/to/some/file >/dev/null
Show Sample Output
Note that this assumes the application is an SVN checkout and so we have to throw away all the .svn files before making the substitution.
random(6) - random lines from a file or random numbers
This command turns a multi-line file into a single line joined with <SOMETEXT>. To skip blank lines, use:
perl -pe '(eof()||s/^\s*$//)||s/\n/<SOMETEXT>/g' file.txt
Show Sample Output
Filter comments and empty lines in files. I find this very useful when trying to find what values are actually set in a very long example config file. I often set an alias for it, like : alias nocomment='grep -v "^\($\|#\)"' Show Sample Output
Randomizes a file. The opposite of sort is sort -R!
better integration. works on all Unices works one bash and ksh. Show Sample Output
The above is an example of grabbing only the first column. You can define the start and end points specifically by chacater position using the following command:
while read l; do echo ${l:10:40}; done < three-column-list.txt > column-c10-c40.txt
Of course, it doesn't have to be a column, or extraction, it can be replacement
while read l; do echo ${l/foo/bar}; done < list-with-foo.txt > list-with-bar.txt
Read more about parameter expansion here:
http://wiki.bash-hackers.org/syntax/pe
Think of this as an alternative to awk or sed for file operations
awk version of 7210. Slightly longer, but expanding it to catch blank lines is easier:
awk 'BEGIN{RS="\0"}{gsub(/\n+/,"<SOMETEXT>");print}' file.txt
Show Sample Output
See which files differ in a diff, and how many changes there are. Very useful when you have tons of differences. Show Sample Output
The sort utility is well used, but sometimes you want a little chaos. This will randomize the lines of a text file.
BTW, on OS X there is no
| sort -R
option! There is also no
| shuf
These are only in the newer GNU core...
This is also faster than the alternate of:
| awk 'BEGIN { srand() } { print rand() "\t" $0 }' | sort -n | cut -f2-
Show Sample Output
print the lines of a file in randomized order Show Sample Output
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: