Visit Our UNIX and Linux User Community


Printing "END" before a new loop in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing "END" before a new loop in AWK
# 1  
Old 12-07-2007
Printing "END" before a new loop in AWK

First off, I have been learning AWK by trial and error over the last week or so, and there are some gaps in my basic understanding of the language.

Here is my situation: I am coding and outputting results from an experiment I conducted in Psyscope, which has all been compiled into a master file. The AWK script reads through the results and outputs the encoded information into individual files for each subject that was run. This output looks like this:

#Filename: syll 36970.awk

seed syll36970 #Identifies subject
c677 #Response 1
01611221 #Information about condition
i675 #Response 2
01612121 #Information about condition

And that pattern continues. Each file is like this with its unique information. The only thing that is missing is I want AWK to print "END" at the end of each file. For some reason, it either sticks it in with the response information or at the very beginning.

If you want to help me, respond and I can send you a copy of my script.

Thank you!
# 2  
Old 12-07-2007
As always when asking for help with data formatting, post samples of input and desired output, along with what in the input corresponds with what in the output.

As a guess because I'm, not sure exactly what is wanted:

Code:
awk '$1 == "seed" { if ( filename )  { print "END" > filename ;  close(filename)} ; filename = $2 ".out" } { print > filename } END { print "END" > filename }'

# 3  
Old 12-08-2007
Question Clarification

I have been thinking about how to clarify, and due to a lack of familiarity I am struggling. My input is fairly complex:

PsyScope 1.2.5 PPC started: 11/07/07 12:35:10
Script file: FL 1024x768 v24 NEW NEW
Run on: Power Macintosh
Random Seed: 549174

RunNumber: 1
SubjectNumber: 14
SubjectName: 37492
Native Language: English
Do you speak this language regularly?: Yes

Input devices active: MOUSE KEY
Timing Device: Macintosh

440 Item2 TY 1 L UNR INCORRECT 1120 0
441 Item5 TY 1 R UNL INCORRECT 704 0
442 Item1 TY 1 L GOL CORRECT 577 0

This is the header and first 3 responses for one subject. Now picture a file with 20 subjects with 60 responses each, and it is one large txt file. My program reads through that file and prints out all this information in a a code that can be analyzed by another program very easily, in the format shown in my first post. I am finding it difficult to have awk print END each time it gets to a new subject entry at the end of the previous file (so it is actually at the end). This is just another bit of the code that needs to be there for the analysis.

Each subject is being saved to a new file by my awk script, so instead of one mondo file, in the end I have 20 files that are titled ("Syll"SubName".out"), where subject name is the variable that contains each subjects ID code. All of those individual files follow the format that I presented before.

Because I could not do it within my original script, performed this work around. I had my original script generate a list of all the subject codes in another output file, and then created another script to read from that file and append "END" to all the "Syll" output files.

This is the file I generate with the codes...
#List of Subject Codes
001: 37492
002: 36970
003: 37255
004: 36826
005: 37522
006: 37420
007: 37465
008: 37417
009: 36946
010: 37531

Here is my code for the script END.awk
# -- END.awk

{FS=":"}
{print "END" >> ("Syll" $2 ".out")}

And that actually does the trick. The only thing is that I am going to be giving this script back to my professor to use, and I want it to be as neat as possible, so if I can do this all in one script that would be awesome.

Alright, I hope this is all the information you need and more! I tried saving the subject code to a new field within the original input file, and letting it post there every loop, but it didnt work out as planned at all. Although I am learning the usefulness of awk through this project, I am definitely learning its weaknesses, so if I should be going about this with a different scripting language all together, please let me know!

THANK YOU FOR YOUR HELP!Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #478
Difficulty: Medium
The first commercially available TB hard drive was released in 2005.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: Performing "for" loop within text block with two files

I am hoping to pull multiple strings from one file and use them to search within a block of text within another file. File 1PS001,001 HLK PS002,004 MWQ PS004,002 RXM PS004,006 DBX PS004,006 SBR PS005,007 ML PS005,009 DBR PS005,011 MR PS005,012 SBR PS006,003 RXM PS006,003 >SJ PS006,010... (11 Replies)
Discussion started by: jvoot
11 Replies

2. Shell Programming and Scripting

Using "for" loop within "awk"

Hi Team. I am trying to execute a simple for loop within an awk but its giving a different result. Below is the main code: awk '{for(i=1;i<=6;i++) print $i}'The result should be 1 2 3 4 5 6 but its not giving this result. Can someone please help? (3 Replies)
Discussion started by: chatwithsaurav
3 Replies

3. Shell Programming and Scripting

Removing "^M" from the end of a String (i.e. "Ctrl+M")?

Hello All, I have an Expect script that ssh's to a remote server and runs some commands before exiting. One of the commands I run is the "hostname" Command. After I run this command I save the output using this line in the code below... Basically it executes the hostname command, then I... (2 Replies)
Discussion started by: mrm5102
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

Get values from 2 files - Complex "for loop and if" awk problem

Hi everyone, I've been thinking and trying/changing all day long the below code, maybe some awk expert could help me to fix the for loop I've thought, I think I'm very close to the correct output. file1 is: <boxes content="Grapes and Apples"> <box No.="Box MT. 53"> <quantity... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

6. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

append "awk command" to the end of each line

hi; this is my qqq.mos: l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-' l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-' l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-' ... i want to append;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r"... (4 Replies)
Discussion started by: gc_sw
4 Replies

9. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

Featured Tech Videos