In this example we extract pages 14-17
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?
It uses the following GNU grep options: "-o" which shows only the matching part of the line and "-P" which allows the use of Perl regular expressions. Show Sample Output
wrestool can be found in icoutils (http://www.nongnu.org/icoutils) 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
Part of the "atool" package.
http://manpages.ubuntu.com/manpages/hardy/man1/icotool.1.html There is also wrestool command. Show Sample Output
Part of the "atool" package
Something I do a lot is extract columns from some input where cut is not suitable because the columns are separated by not a single character but multiple spaces or tabs. So I often do things like: ... | awk '{print $7, $8}' ... which is a lot of typing, additionally slowed down when typing symbols like '{}$ ... Using the simple one-line function above makes it easier and faster: ... | col 7 8 How it works: The one-liner defines a new function with name col The function will execute awk, and it expects standard input (coming from a pipe or input redirection) The function arguments are processed with sed to use them with awk: replace all spaces with ,$ so that for example 1 2 3 becomes 1,$2,$3, which is inserted into the awk command to become the well formatted shell command: awk '{print $1,$2,$3}' Allows negative indexes to extract columns relative to the end of the line. Credit: http://www.bashoneliners.com/oneliners/oneliner/144/ Show Sample Output
this command extracts an initrd files into the "tmp" directory
extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.tar.xz) tar Jxvf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "don't know how to extract '$1'..." ;; esac read -r -p "Delete the compressed file? [Y/N] " response response=${response,,} # tolower if [[ $response =~ ^([Yy]es|YES|[Yy])$ ]]; then echo "rm '$1'" rm $1 fi else echo "'$1' is not a valid file!" fi }
the 'aunpack' command is part of atool
The function had to be cut down to meet the maximum command length requirements. The full version of the function is:
extract()
{
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
Note: This is not my original code. I came across it in a forum somewhere a while ago, and it's been such a useful addition to my .bashrc file, that I thought it worth sharing.
Show Sample Output
The example uses hashicorps packer as the sample file, but any .zip file which only contains a single file will work. Show Sample Output
Plain old `unzip` won't unzip output coming from STDOUT the ZIP file format includes a directory (index) at the end of the archive. This directory says where, within the archive each file is located and thus allows for quick, random access, without reading the entire archive. This would appear to pose a problem when attempting to read a ZIP archive through a pipe, in that the index is not accessed until the very end and so individual members cannot be correctly extracted until after the file has been entirely read and is no longer available. As such it appears unsurprising that most ZIP decompressors simply fail when the archive is supplied through a pipe. The directory at the end of the archive is not the only location where file meta information is stored in the archive. In addition, individual entries also include this information in a local file header, for redundancy purposes. From the `jar` manpage: > The jar command is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. JAR is smart enough to know how to handle these local file headers when the index is unavailable when reading through the pipe. (Most of the explanation in this description is taken from https://serverfault.com/a/589528/314226 , though they recommend using `bsdtar`, but that is not always available on systems) Show Sample Output
Image to text converter. Convert your scanned book in image format like .png, .jpg into editable text format. OCR ==> Optical Code Reader 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: