Text Manipulation
Insert text at the top of a file
echo -e "text to insert\n$(cat file-to-edit.txt)" > file-to-edit.txt
Search file for text
Searches all files in the current directory for "category: linux" which can be saved to a text file
grep -iRl "category: linux" ./
The listoffiles from above can be passed to the mv or cp command
for file in cat listoffiles; do mv "$file" /path/of/destination ; done
Rename to remove space in file names
rename "s/ //g" or rename "s/-- //g" to fill the spaces with -
Removes space and insert
If you don't have rename or prefer to use just the shell:
for f in \ ; do mv "$f" "${f// /-}"; done
Broken down:
*\ * selects all files with a space in their name as input for the the for loop. The quotes around "$f" are important because we know there's a space in the filename and otherwise it would appear as 2+ arguments to mv. ${f//str/new_str} is a bash-specific string substitution feature. All instances of str are replaced with new_str.
Removes ctl-m
To get the ^M character, type Control-v and hit Enter :%s/\r//g or %s/ //gc
Adding a line of text to multiple files
I have a bunch of files in a directory, and I need to insert a line of text into each of them. They have essentially the following format:
And I'd like to insert author: Flo before the tags:
for i in ; do sed -i 's/tags:/author: Flo\ntags:/' "i"; done
vim add charcter to end of line
:%norm A*
This is what it means:
% = for every line
norm = type the following commands
A = append '' to the end of current line
## find and replace ## find . -type f -name 'words-to-be-replaced*' | while read FILE ; do newfile="$(echo ${FILE} |sed -e 's/words-to-be-replaced/replacedment-words-here/')" ; mv "${FILE}" "${newfile}" ;done