Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk csv file, pass output to bash variables, use variables to make sed replacements & new file n

Status
Not open for further replies.

jfer0x01

IS-IT--Management
Jun 7, 2012
2
US
Hi,

I need to
1. read various csv files in a hard-coded folder ( 2 columns)
2. Save the output of columns 1 and column 2 to var1 and var2
3. Copy a template file I have and generate a new file name based on var2.
4. Make substitutions inside the new file for instances of Field1 = (var1) and Field2 = (var2)

For the CSV file:

Column1 has a 4 to 6 digit value
Column2 has names with "\", spaces and "-", but no commas

So far, I have most of the code pseudo-code

#!/bin/bash
FILES=/mydir/*.txt
for f in $FILES
do
echo "Processing $f file..."
var1=$(awk -F ',' ' {print $1}' $f )
var2=$(awk -F ',' ' {print $2}' $f )
echo "Doing $var1 and $var2"
cp /myotherdir/Skeleton-File /myotherdir/"$var2"-"$var1"-newfile
sed -e "s/Field1/"$var1"/g" -i /myotherdir/"$var2"-"$var1"-newfile
sed -e "s/Field2/"$var2"/g" -i /myotherdir/"$var2"-"$var1"-newfile
done

I have had mixed results attempting nested do loops, but my main problem seems to be with var2, attempting $var2, bash treats spaces as a new line (even though awk prints it ok) and "$var2" treats the variable as a single long line and generates a combined file name of all the contents.

Any ideas on how to accomplish this?

Thanks



 
I'd try to do all the stuff in awk, eg (typed, untested)
Code:
awk -F, 'NR==FNR{t[NR]=$0;next}
{out="/myotherdir/"$2"-"$1"-newfile"
 for(i=1;(i in t);++i){
  x=t[i];gsub(/Field1/,$1,x);gsub(/Field2/,$2,x)
  print x >out;close(out)
 }
}' /myotherdir/Skeleton-File /mydir/*.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

I have begun dissecting your code, but I receive an error on the print.

Does it need to be in { print x} ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top