miguel morfin
miguel morfin

Reputation: 1

How to create files with content, from a list

Trying to figure out how to create files(.strm) with specific content using a file list.

Example:

List.txt(content)

American Gangster (2007).strm,http://xx.x.xxx.xxx:8000/movie/xxxx/xxx/2017
Mi movie trailer (2019).strm,http://xx.x.xxx.xxx:8000/movie/xxxx/xxx/123
Peter again (2020).strm,http://xx.x.xxx.xxx:8000/movie/xxxx/xxx/5684

etc.

I was able to create the .strm files using the below script:

#!/bin/bash
filename="path/list.txt"
while IFS= read -r  line
do
#       echo "$line"
        touch "$line"
done < "$filename"`

but this leaves me with empty files, how can I read and append the content?

Desire output:

Filename: AmericanGangster(2007).strm

Inside the file: http://xx.x.xxx.xxx:8000/movie/xxxx/xxx/2017

Thanks

Upvotes: 0

Views: 238

Answers (1)

Jetchisel
Jetchisel

Reputation: 7791

You need to put the contents of the file to the file names that you created. Try

filename=path/list.txt
while IFS= read -r line; do
  if [[ -e $line ]]; then  ##: The -e tests if the file does exists.
    echo "${line##*,}" > "${line%,*}"
  fi
done < "$filename"

Since there can't be a filename with a '/' in it the -e is not needed.

filename=path/list.txt
while IFS= read -r line; do
  echo "${line#*,}" > "${line%,*}"
done < "$filename"

It can be done by setting IFS to IFS=,

while IFS=, read -r file contents; do 
  echo "$contents" > "$file"
done < "$filename"

... Or awk.

awk -F, '{print $2 > $1}' "$filename"
  • The , must not appear anywhere except where it is now, otherwise all solution will break.

Upvotes: 1

Related Questions