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!

Creating new csvs for multiple csv files

Status
Not open for further replies.

RB81

Technical User
Nov 12, 2009
1
GB
I am inexperienced at scripting and am having trouble writing an awk script which takes as input a csv file of the format:

file_name word_text duration
F3F4F4cv_LN !SIL 14.24
F3F4F4cv_LN mmhm 0.149
F3F4F4cv_LN uhhuh 0.214

and makes another csv file identical to the original but with an extra column titled "word_type", e.g.

file_name word_text duration word_type
F3F4F4cv_LN !SIL 14.24 SIL
F3F4F4cv_LN mmhm 0.149 AGR
F3F4F4cv_LN uhhuh 0.214 AGR

The text in the word_type column depends on what text is in the 2nd column, e.g. if "!SIL" in 2nd then "SIL" in 4th, if "mhmm" in 2nd then "AGR" in 4th etc.

I have been trying to do this using the following commands but the loop to open each file in turn isn't working and I am also wondering if there is a more concise way of doing the column addition:

*****
#create a loop to work through a list of files in a directory

for filename in *.csv
do

#create a new text file with an initial row containing the column headings and save as original filename _type:

'BEGIN { print "file_name word_text duration word_type" > filename_type }'

cat filename |' $2~/SIL/ {print $0, "SIL" }' > filename_type
cat filename |'$2~/mmhm/ {print $0, "AGR" }' > filename_type
cat filename |'$2~/uhhuh/ {print $0, "AGR" }' > filename_type
END

done

****************
Any advice would be greatly appreciated as I'm going around in circles!
 
Hi

Based on what I understood so far :
Code:
awk '{if(NR==1)$4="word_type";else if($2=="!SIL")$4="SIL";else$4="AGR"}1' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

But I am wondering if is there some rule to automate determining $4's value. Maybe a second file with pairs of $2 & $4 ?

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top