Home > Enterprise >  Creating Multiple Workfile using Bash Script
Creating Multiple Workfile using Bash Script

Time:03-31

I m trying to create a multiple workfiles

Source csv file

**Filename,SchemaName,TableName 
Shelf,category,books 
Door,Category,wood**

Expected Output

each Filename from column 1

Shelfy.wrk

\`File content  Shelf.wrk

Your table is category.books\`

Door.wrk

\`File content  Door.wrk

Your table is category.wood\`

I tried to create something like

sed 's/,/\\n/g' file | while read fileName; do touch "$fileName".wrk; done

but not sure how to populate the content by form row by row to seperate file

CodePudding user response:

As user1934428 mentioned, there's no need to use sed at all, but awk.

awk '(NR!=1)' source.csv | awk -F, '{fName=$1".wrk";cat=$2;tbl=$3; print fName"\nFile content "fName"\nYour table is "cat"."tbl}'

First let's skip the first line with awk '(NR!=1)' source.csv Then instruct awk to use comma as field separator with -F, (as IFS=, would do) Assign each column value to a variable. For file name extension .wrk is appended: fName=$1".wrk". For the other two columns: cat=$2;tbl=$3;

Finally you only print the strings print fName"\nFile content "fName"\nYour table is "cat"."tbl

  • Related