Help in outputting the result in log files

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?

The third filename is inconsistent. Is that a typo or is it actually like that? And do the quotes actually exist in the file?

Assuming ss.409.sh is a typo:

awk '{rec=$0;gsub(/"/,"",$1);print rec>("ss"$1".sh")}' FS="," filename

Use nawk or /usr/xpg4/bin/awk on Solaris.

Hi,
I test on my pc(solairs), it is ok.

nawk 'BEGIN{FS="\""}
{
	if (cur=="")
	{
		cur=$2
		system("touch "$2".txt")
		system("echo "$0" >>"$2".txt")
	}
	else
	{
		if ($2!=cur)
		{
			cur=$2
			system("touch "$2".txt")
			system("echo "$0" >>"$2".txt")			
		}
		else
		{
			system("echo "$0" >>"$2".txt")
		}
	}
}' filename

Wonderful. You are my idol. radoulov

:slight_smile: Wonderfull!!!!!!!!

It is working.But I cant understand.Can you explain in detail?

ss.409.sh should be ss.49.sh.It was mistakenly typed as ss.409.sh.Anyway your answer is correct.You have given exactly what i expected.

Can i write this using using for loop and if loop?

What is rec and FS in your solution?

awk '{rec=$0;gsub(/"/,"",$1);print rec>("ss"$1".sh")}' FS="," filename
  1. rec=$0 - save the current record, before modifying it with gsub (global substitution).

  2. gsub(/"/,"",$1) - modify the first field ($1), remove the quotes: "40" becomes 40 and so on.

  3. 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".

  4. FS is Field Separator(,).

# awk -F"\"" '{print $0>"ss"$2".sh"}' "file"
# more ss4*sh
::::::::::::::
ss40.sh
::::::::::::::
"40","1G1AL55 ",30482,9000
"40","1G1ZT58 ",29098,10600
"40","1G1AL15 ",29222,9400
::::::::::::::
ss46.sh
::::::::::::::
"46","1G6KD57 ",3083,28400
"46","1G6KD57 ",27909,25200
::::::::::::::
ss49.sh
::::::::::::::
"49","1G1ZU57 ",16391,13900
"49","1G2ZG58 ",28856,1240

Good catch ghostdog74 :b:

I would go with:

awk  -F'"' '{print>"ss"$2".sh"}' file

But you still will need parenthesis for some awk implementations.

I mean:

$ nawk -F'"'  '{print>"ss"$2".sh"}' file
nawk: syntax error at source line 1
 context is
         >>> {print>"ss"$2 <<< ".sh"}
nawk: illegal statement at source line 1
$ nawk -F'"' '{print>("ss"$2".sh")}' file
$

awk -F"\"" '{print $0>"ss"$2".sh"}' "file"

Why you have added "\"" in your code?

Why we are using $2 for file name.Actually it is the first field.Then how it is taking?

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.

Do you really think we need it :slight_smile:
Did you try it?

Nope! The awk redirection is different from the shell redirection :slight_smile:
I would suggest reading Effective AWK Programming (it's free):
http://www.gnu.org/software/gawk/manual/gawk.pdf

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".

P.S. Feel free to correct my english:) HTH.

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. :smiley:

Of course, not :slight_smile:
Thank you, blowtorch!