Difference between revisions of "BASH File Manipulation"

From Indie IT Wiki
imported>Plittlefield
 
m
Line 1: Line 1:
== HOWTO: Create Directory From Photo Creation Date ==
+
== CSV Files: ==
  
sudo aptitude install libimage-exiftool-perl
+
=== Delete Characters On Just The First Line ===
  
'''YEAR/MONTH/DAY'''
+
This deletes all the quotation (") characters on just the first line of a CSV file...
  
  IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
+
  sed '1s/"//g' /tmp/oldfile.csv > /tmp/newfile.csv
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F' ' '{print $4}' | tr ':' '/');
 
echo "${IMGDATE}";
 
mkdir -p "~/Pictures/Photos/${IMGDATE}"
 
  
'''YEAR/MONTH'''
+
http://sed.sourceforge.net/sed1line.txt
  
IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
+
== Text Files: ==
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
 
echo "${IMGDATE}";
 
mkdir -p "~/Pictures/Photos/${IMGDATE}"
 
 
 
Tweaks...
 
 
 
== '''HOWTO: TEXT FILES:''' ==
 
  
 
=== Number Beginning Of Lines ===
 
=== Number Beginning Of Lines ===
Line 73: Line 63:
 
  done
 
  done
  
Thanks - http://stackoverflow.com/questions/32062159/how-retrieve-the-creation-date-of-photos-with-a-script
+
Thanks to [http://stackoverflow.com/questions/32062159/how-retrieve-the-creation-date-of-photos-with-a-script Stack Overflow]
 +
 
 +
=== Trim Leading White Space ===
 +
 
 +
sed 's/^ *//g'
 +
 
 +
e.g.
 +
 
 +
lynx -nolist -dump <nowiki>http://wiki.indie-it.com/wiki/Television_Paully</nowiki> |sed -n '/What I Am Currently Watching/,/What I Have Been Watching/p' |sed 's/^ *//g' |egrep '^[0-9]'
 +
 
 +
Thanks to [http://stackoverflow.com/questions/9309882/delete-whitespace-in-each-begin-of-line-of-file-using-bash Stack Overflow].
 +
 
 +
=== Print Lines Between Two Patterns Using SED and AWK ===
 +
 
 +
To print (display) only text between two lines that contain patterns ‘BEGIN’ and ‘END’ in the following text file called info.txt.
 +
 
 +
I Love Linux
 +
***** BEGIN *****
 +
BASH is awesome
 +
BASH is awesome
 +
***** END *****
 +
I Love Linux
 +
 
 +
==== Using SED ====
 +
 
 +
Allows the specifying of the starting pattern and the ending pattern, to print the lines between strings with these patterns.
 +
 
 +
sed -n '/StartPattern/,/EndPattern/p' FileName
 +
 
 +
Option Description:
 +
 
 +
*-n, –quiet, –silent - Suppress automatic printing of pattern space
 +
*p - Print the current pattern space
 +
 
 +
Example:
 +
 
 +
sed -n '/BEGIN/,/END/p' info.txt
 +
 
 +
==== Using AWK ====
 +
 
 +
AWK works in a similar way to the sed command specifying the starting pattern and the ending pattern.
 +
 
 +
awk '/StartPattern/,/EndPattern/' FileName
 +
 
 +
Example:
 +
 
 +
awk '/BEGIN/,/END/' info.txt
 +
 
 +
Thanks to [https://www.shellhacks.com/sed-awk-print-lines-between-two-patterns/ Shell Hacks]
 +
 
 +
== Image Files: ==
 +
 
 +
=== Create Directory From Photo Creation Date ===
 +
 
 +
sudo aptitude install libimage-exiftool-perl
 +
 
 +
'''YEAR/MONTH/DAY'''
  
== HOWTO: Show The Resolution / DPI Of An Image ==
+
IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
 +
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F' ' '{print $4}' | tr ':' '/');
 +
echo "${IMGDATE}";
 +
mkdir -p "~/Pictures/Photos/${IMGDATE}"
 +
 
 +
'''YEAR/MONTH'''
 +
 
 +
IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
 +
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
 +
echo "${IMGDATE}";
 +
mkdir -p "~/Pictures/Photos/${IMGDATE}"
 +
 
 +
Tweaks...
 +
 
 +
=== Show The Resolution / DPI Of An Image ===
  
 
  identify -verbose photo.jpg |grep 'Resolution'
 
  identify -verbose photo.jpg |grep 'Resolution'
 
   
 
   
 
  Resolution: 72x72
 
  Resolution: 72x72
 +
 +
== Filename Manipulation: ==
  
== Add A Zero To The Beginning Of A Filename ==
+
=== Add A Zero To The Beginning Of A Filename ===
  
 
  $ renamexm -t -v -s/^/0/r [0-9] *.mp3
 
  $ renamexm -t -v -s/^/0/r [0-9] *.mp3
Line 93: Line 155:
 
  rename 8 Countdown.mp3      => 08 Countdown.mp3    : tested
 
  rename 8 Countdown.mp3      => 08 Countdown.mp3    : tested
  
== Delete Characters On Just The First Line ==
 
  
This deletes all the quotation (") characters on just the first line of a CSV file...
+
=== Mass File Rename Incrementing Number in File Name ===
 
 
sed '1s/"//g' /tmp/oldfile.csv > /tmp/newfile.csv
 
 
 
http://sed.sourceforge.net/sed1line.txt
 
 
 
== HOWTO: Mass File Rename Incrementing Number in File Name ==
 
  
 
  export j=0
 
  export j=0
 
  for i in *.mkv ; do let j+=1 ; mv -v $i "Willow the Wisp S01E$j.mkv" ; done
 
  for i in *.mkv ; do let j+=1 ; mv -v $i "Willow the Wisp S01E$j.mkv" ; done
  
== HOWTO: Mass File Rename Deleting Part Of Name ==
+
=== Mass File Rename Deleting Part Of Name ===
  
 
e.g.
 
e.g.
Line 116: Line 171:
 
  renamexm -v -s/" .*.mp4"/".mp4"/e *.mp4
 
  renamexm -v -s/" .*.mp4"/".mp4"/e *.mp4
  
== HOWTO: Rename All Files Add File Extension ==
+
=== Rename All Files Add File Extension ===
  
 
  for f in *; do mv "$f" "$f.jpg"; done
 
  for f in *; do mv "$f" "$f.jpg"; done
 +
 +
== Finding Information Within A File ==
  
== HOWTO: Find Out Length Of Longest Line In A File ==
+
=== Length Of Longest Line In A File ===
  
 
  wc -L file
 
  wc -L file
  
== HOWTO: Find Line Exceeding N Characters ==
+
=== Line Exceeding N Characters ===
  
 
  grep '.\{N\}' file
 
  grep '.\{N\}' file
  
== HOWTO: Trim Leading White Space ==
 
 
sed 's/^ *//g'
 
 
e.g.
 
 
lynx -nolist -dump <nowiki>http://wiki.indie-it.com/wiki/Television_Paully</nowiki> |sed -n '/What I Am Currently Watching/,/What I Have Been Watching/p' |sed 's/^ *//g' |egrep '^[0-9]'
 
 
Thanks to [http://stackoverflow.com/questions/9309882/delete-whitespace-in-each-begin-of-line-of-file-using-bash Stackoverflow].
 
 
== HOWTO: Pattern Match Between 2 Lines ==
 
 
I Love Linux
 
***** BEGIN *****
 
BASH is awesome
 
BASH is awesome
 
***** END *****
 
I Love Linux
 
  
sed -n '/BEGIN/,/END/p'
+
To print (display) only text between two lines that contain patterns ‘BEGIN’ and ‘END’ in the following text file called info.txt.
  
Thanks to [http://www.shellhacks.com/en/Using-SED-and-AWK-to-Print-Lines-Between-Two-Patterns Shell Hacks]]
+
I Love Linux
 +
***** BEGIN *****
 +
BASH is awesome
 +
BASH is awesome
 +
***** END *****
 +
I Love Linux
  
 
[[Category:BASH]]
 
[[Category:BASH]]
 
[[Category:Linux]]
 
[[Category:Linux]]
 
[[Category:Software]]
 
[[Category:Software]]

Revision as of 08:57, 29 November 2024

CSV Files:

Delete Characters On Just The First Line

This deletes all the quotation (") characters on just the first line of a CSV file...

sed '1s/"//g' /tmp/oldfile.csv > /tmp/newfile.csv

http://sed.sourceforge.net/sed1line.txt

Text Files:

Number Beginning Of Lines

cat file.txt |nl

Convert Text To Lowercase

echo "FISH" | tr '[:upper:]' '[:lower:]'
cat file.txt | tr '[:upper:]' '[:lower:]'

View A Text File With NO Word Wrap

less -S file.txt

Remove Blank Lines

sed -i '/^$/d' file.txt
or
cat myfile.txt |sed '/^$/d'

Convert Tabs To Spaces In a File

expand file.txt

COPY FILE TO NEW DIRECTORY

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
IMGDIR="~/Pictures/Photos/${IMGDATE}";
mkdir -p "$IMGDIR";
cp -a -v "${IMG}" "${IMGDIR}"

MOVE FILE TO NEW DIRECTORY

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
IMGDIR="~/Pictures/Photos/${IMGDATE}";
mkdir -p "$IMGDIR";
mv -v "${IMG}" "${IMGDIR}"

COMPLETE WORKING SCRIPT

#!/bin/bash
PHOTOS=/home/paully/Pictures/PhotosFromPhone/*.jpg;
for P in ${PHOTOS}
do
IMG=${P};
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
IMGDIR="/home/paully/Pictures/Photos/${IMGDATE}";
mkdir -p "$IMGDIR";
cp -a -v "${IMG}" "${IMGDIR}"
done

Thanks to Stack Overflow

Trim Leading White Space

sed 's/^ *//g'

e.g.

lynx -nolist -dump http://wiki.indie-it.com/wiki/Television_Paully |sed -n '/What I Am Currently Watching/,/What I Have Been Watching/p' |sed 's/^ *//g' |egrep '^[0-9]'

Thanks to Stack Overflow.

Print Lines Between Two Patterns Using SED and AWK

To print (display) only text between two lines that contain patterns ‘BEGIN’ and ‘END’ in the following text file called info.txt.

I Love Linux
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****
I Love Linux

Using SED

Allows the specifying of the starting pattern and the ending pattern, to print the lines between strings with these patterns.

sed -n '/StartPattern/,/EndPattern/p' FileName

Option Description:

  • -n, –quiet, –silent - Suppress automatic printing of pattern space
  • p - Print the current pattern space

Example:

sed -n '/BEGIN/,/END/p' info.txt

Using AWK

AWK works in a similar way to the sed command specifying the starting pattern and the ending pattern.

awk '/StartPattern/,/EndPattern/' FileName

Example:

awk '/BEGIN/,/END/' info.txt

Thanks to Shell Hacks

Image Files:

Create Directory From Photo Creation Date

sudo aptitude install libimage-exiftool-perl

YEAR/MONTH/DAY

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F' ' '{print $4}' | tr ':' '/');
echo "${IMGDATE}";
mkdir -p "~/Pictures/Photos/${IMGDATE}"

YEAR/MONTH

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
echo "${IMGDATE}";
mkdir -p "~/Pictures/Photos/${IMGDATE}"

Tweaks...

Show The Resolution / DPI Of An Image

identify -verbose photo.jpg |grep 'Resolution'

Resolution: 72x72

Filename Manipulation:

Add A Zero To The Beginning Of A Filename

$ renamexm -t -v -s/^/0/r [0-9] *.mp3
rename 1 Subdivisions.mp3   => 01 Subdivisions.mp3  : tested
rename 2 The Analog Kid.mp3 => 02 The Analog Kid.mp3 : tested
rename 3 Chemistry.mp3      => 03 Chemistry.mp3     : tested
rename 4 Digital Man.mp3    => 04 Digital Man.mp3   : tested
rename 5 The Weapon.mp3     => 05 The Weapon.mp3    : tested
rename 6 New World Man.mp3  => 06 New World Man.mp3 : tested
rename 7 Losing It.mp3      => 07 Losing It.mp3     : tested
rename 8 Countdown.mp3      => 08 Countdown.mp3     : tested


Mass File Rename Incrementing Number in File Name

export j=0
for i in *.mkv ; do let j+=1 ; mv -v $i "Willow the Wisp S01E$j.mkv" ; done

Mass File Rename Deleting Part Of Name

e.g.

Scooby_Doo_Mystery_Incorporated_S02E25 Through The Curtain 720p x264.mp4 => Scooby_Doo_Mystery_Incorporated_S02E25.mp4

Do...

renamexm -v -s/" .*.mp4"/".mp4"/e *.mp4

Rename All Files Add File Extension

for f in *; do mv "$f" "$f.jpg"; done

Finding Information Within A File

Length Of Longest Line In A File

wc -L file

Line Exceeding N Characters

grep '.\{N\}' file


To print (display) only text between two lines that contain patterns ‘BEGIN’ and ‘END’ in the following text file called info.txt.

I Love Linux

          • BEGIN *****

BASH is awesome BASH is awesome

          • END *****

I Love Linux