Text Manipulation
26 Feb 2016WHILE loop:
while read line; do (echo $line); done
FOR loop:
for name in {1..10}.txt; do touch $name; done
Read A Line On A File:
$ cat file.txt | while read line; do (echo $line); done
cut -d":" -f1 -s /etc/group | (while read line; do sudo chown -R $line.$line ./$line; done)
cut -d":" -f1 -s /home/ec2-user/passwd | while read line; do sudo chown -R $line.$line /home/$line ; done
cut -d"/" -f3 DreamPunsDBSST.list | while read line; do mv ./DreamParkDB/$line ./DreamPunsDBSST/$line; done
Read the mailboxes lines and copy to archive directory
cat mailboxes.txt | while read line; do cp ./mail/$line ./mail_archives/ ; done
Substitution:
replace ‘cat’ by ‘dog’ in file in.txt and output it to file out.txt the ‘g’ means replace all matches, not just the first on a given line
sed -e 's/cat/dog/g' in.txt > out.txt
Batch File Rename:
for file in *.* ; do mv [SOURCE_NAME] [DEST_NAME]; done
Batch File Rename Ext:
for file in *.bkp
do
mv $file `basename $filename .bkp`;
done