This is the file am having:
"40","1G1AL55 ",30482,9000
"40","1G1ZT58 ",29098,10600
"40","1G1AL15 ",29222,9400
"46","1G6KD57 ",3083,28400
"46","1G6KD57 ",27909,25200
"49","1G1ZU57 ",16391,13900
"49","1G2ZG58 ",28856,12400
I want to display the output in three files namely:ss40.sh,ss46.sh,ss.409.sh which are created automatically using the script.
ss40.sh should contain the corresponding details of all records with the fist column as "40".Likewise for the other two files also.
How can i write script for this?Which command should i use?
rec=$0 - save the current record, before modifying it with gsub (global substitution).
gsub(/"/,"",$1) - modify the first field ($1), remove the quotes: "40" becomes 40 and so on.
Print the previously saved record in a file named ss $1(from the modified current record, 40, not "40") .sh (you have to quote the literal strings - "ss" and ".sh".
radoulov, you have missed "$0" after print, i.e print $0 > ......
and dave_nithis, '\' character is used to override the behavior of shell's meta character like double quotes. The character followed by '\' inside the double quotes will be taken as the character itself.
Note:
And awk -F"\"" is equal to awk -F'"' is equal to awk 'BEGIN{FS="\""}' is equal to awk '....' FS="\""
But I have a doubt here, how your script checks whether 40 should go to the "ss40.sh", 46 to the "ss46.sh" and so on. And also the redirection operator is in "overwrite mode", so each time it would clear the file and injects the record afresh, so how here the records are appended correctly? Please correct me, if I am wrong.
sorry radoulov, I don't have "nawk" in my system so I didn't try it. But it works perfectly fine in presence and absence of "$0" as you mentioned for "awk". Sorry for my wrong comment posted earlier. But can you answer me for my question regarding checking of the value like 40, 46 .. etc.,? I will definitely go through the PDF document.
It doesn't check anything: $2 is variable (so the filename to append varies),
it changes with the record, so if the record's second field is 40,
it goes to file named "ss"$2".sh" -> "ss"40".sh".
Just beat me to it! Was going to give almost exactly the same answer. BTW, since you said to correct your English, I did fix a spelling mistake in your post. Hope you don't mind.