Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting multiline output to multiple files

Tags:

bash

awk

I have a list of URLs, and would like to identify what is a directory and what is not:

https://www.example.com/folder/
https://www.example.com/folder9/
https://www.example.com/folder/file.sh
https://www.example.com/folder/text

I can use grep -e /$ to find which is which, but I'd like to do an inline command where I can redirect the output based on that logic.

I understand that awk may have the answer here, but don't have enough experience in awk to do this.

Something like:

cat urls | if /$ matches write to folders.txt else write to files.txt

I could drop it all to a file then read it twice but when it gets to thousands of lines I feel that would be inefficient.

like image 893
Chris Green Avatar asked Aug 10 '18 22:08

Chris Green


2 Answers

Yes, awk is a great choice for this:

awk '/\/$/ { print > "folders.txt"; next }
           { print > "files.txt" }' urls.txt
  • /\/$/ { print > "folders.txt"; next } if the line ends with a /, write it to folders.txt and skip to the next line
  • { print > "files.txt" } write all other lines to files.txt

You may want to use the expression /\/[[:space:]]*$/ instead of /\/$/ in case you have trailing spaces in your file.

like image 115
codeforester Avatar answered Oct 20 '22 06:10

codeforester


All you need is:

awk '{print > ((/\/$/ ? "folders" : "files")".txt")}' urls.txt
like image 2
Ed Morton Avatar answered Oct 20 '22 07:10

Ed Morton