George Murphy
George Murphy

Reputation: 1097

Bash line breaks

I am using Git Bash to recursively find all of the file extensions in our legacy web site. When I pipe it to a file I would like to add line-breaks and a period in front of the file extension.

find . -type f -name "*.*" | grep -o -E "\.[^\.]+$" | grep -o -E "[[:alpha:]]{1,12}" | awk '{print tolower($0)}' | sort -u

Upvotes: 0

Views: 166

Answers (1)

Walter A
Walter A

Reputation: 20002

You have different ways.
When you do not want to change your existing commands I am tempted to use

printf ".%s\n" $(find . -type f -name "*\.*" | grep -o -E "\.[^\.]+$" |
  grep -o -E "[[:alpha:]]{1,12}" | awk '{print tolower($0)}' | sort -u ) # Wrong

This is incorrect. When a file extension has a space (like example.with space), it will be split into different lines.
Your command already outputs everyring into different lines, so you can just put a dot before each line with | sed 's/^/./' You can skip commands in the pipeline. You can let awk put a dot in front of a line with

find . -type f -name "*\.*" | grep -o -E "\.[^\.]+$" | grep -o -E "[[:alpha:]]{1,12}" | awk '{print "." tolower($0)}' | sort -u

Or you can let sed ad the dot, with GNU sed also convert in lowercase.

find . -type f -name "." | sed -r 's/..([^.])$/.\L\1/' | sort -u

In the last command I skipped the grep on 12 chars, I think it works different than you like:

echo test.qqqwwweeerrrtttyyyuuuiiioooppp | grep -o -E "\.[^\.]+$" | grep -o -E "[[:alpha:]]{1,12}"

Adding a second line break for each line, can be done in different ways.
When you have the awk command, swith the awk and sort and use

awk '{print "." tolower($0) "\n"}'

Or add newlines at the end of the pipeline: sed 's/$/\n/'.

Upvotes: 1

Related Questions