assorted sed commands

Remove spaces from filenames

removing spaces from a filename: mv -v "$FILE" `echo $FILE | tr ' ' '_' | tr -d '[{}(),\!]' | tr -d "\'" | tr '[A-Z]' '[a-z]' | sed 's/_-_/_/g'` mv -v "$FILE" `echo $FILE | tr ' ' '_' | tr -d '[{}(),\!]' | tr -d "\'" | sed 's/_-_/_/g'` add year on files less than a year old: ls -l | grep -v ^d | sort | awk '{print "mv "$9" "$8"_"$6"_"$7"_"$9}' | sed -e s'$..:..$2014$g' \ ~

Remove Blank Lines from file

cat file-blanklines-2-be-deleted | sed -e '/^$/d' | tee newfile-no-blanklines

some "sed-like" tricks from within vi:

insert # at beginning of lines

:%s/.*/# &/g - inserts a # at the beginning of every line :15,$s/.*/# &/g - inserts a # at the beginning of line 15, until the end of the file :15,250s/.*/# &/g - inserts a # from line 15 through 250.

turn one or more blank spaces into one space

:%s/ */ /g

remove blank lines, or lines with only spaces or tabs

:g/^[ t]*$/d --> WHERE t=tab

redo entire line adding parentheses:

at command line: sed s/.*/( & )/ file > new.file or from within vi: :%s/.*/( & )/g

create a list of commands from a directory listing:

  1. ls > list cat list: list sed.html file.txt
  2. sed 's/.*/mv & &.save/' list > list2mv cat list2mv: mv list mv sed.html mv file.txt
  3. rm list
  4. chmod 750 list2mv
  5. ./list2mv (to move files to *.save version, could use cp to make backup copies)

delete blank spaces:

FROM within vi: :%s/^ *\(.*\)/\1/g OR :%s$^ *\(.*\)$\1$g

delete blank lines:

sed /^$/d file > newfile FROM within vi: :g/^$/d or :%s/^$/d

some "sed-like" tricks using perl at the command line:

THIS WILL NOT WORK: perl -pi -e 's/.*/# &/g' - for two reasons: 1) need to place the variables in parens e.g. (.*) 2) the ampersand will not work, need to use $1 instead. THIS WORKS: perl -pi -e 's/(.*)/# $1/' filename - inserts a # at the beginning of every line --------------------------------------------------------------------------------------------------- note: to do specific lines in perl gets a bit ugly... (as far as I know at this point) perl -pi'*.bak' -e 'if ($.==3){s/$_/Replacement text/;}' file.txt 3 is the line affected (this might work at the command line, ymmv - tested on HP) perl -pi'*.bak' -e 'if ($.==3..20){s/$_/Replacement text/;}' file.txt 3 through 20 would be affected --------------------------------------------------------------------------------------------------- To help reduce the harvesting of emails, one can use perl to replace the at sign with -at-: find . *.txt -exec perl -pi -e 's/@/-at-/g' {} \; ---------------------------------------------------------------------------------------------------

search for:
on the internet, or:
Bible overview



"Jeep is America's
only real sports car."
-Enzo Ferrari

Diesels +

One Page Overview

Intro to Linux

AMSOIL product guide,
or, AMSOIL web, or 1-800-956-5695,
use customer #283461

Amsoil dealer since 1983
purchase AMSOIL
at Midway Auto on SR9 in Snohomish,
or at Northland Diesel in Bellingham, WA

SJ - 1962-1991

XJ - 1984-2001

WJ - 1999-2004

KJ - 2002-2007

WK - 2005-2010

Find the recommended
AMSOIL synthetics
for your Jeep

CJ-10A - 1984-1986


MJ - 1984-1992

Willys - 1946-1965

Other Jeeps (FC)