|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
Multiple file processing - 1st line only to process - search/match substring
Hi People.
I am not a script programmer, so apologies for any bad techniques used here. I am fighting to write my script to run which needs to be run within a unix environment, so it must be a shell (ksh) script or awk script or combination of both to: (a) Run at unix level in a filesystem with may ascii files in it (b) Run by say, the command: ./check_files *.TXT (it can obviously be called anything) It needs t 1. read each ascii file present that matched the passed file format parameter (*.TXT in above example). 2. read only the 1st line (header record) of every file, effectively checking characters 34-39 using the substr command 3. check that 6 character field against an array to see if they match, if not display the filename as not having the correct 6-character code as needed. The online command which does the heart of it is head -n 1 $1 | awk '{print substr( $0, 34, 6) }' where $1 is the filename, but I have not been at all successful in creating this as part of a script (.awk) file. specifically the following association fails (syntax error): demo_extracted=head -n 1 $1 | awk '{print substr( $0, 34, 6) }' (see copy of code below) I have been trying to run this awk script by using the command awk -f check_file.awk listfile.txt where the above command is in the check_file.awk script and the listfile.txt has a list of the files to process. so, codewise/logicwise, which fails is (NOTE script is incomplete and obviously not working)- check_file.awk: #!/bin/ksh BEGIN { } # main logic { demo[1]= "10-19 "; demo[2]= "18-39 "; demo[3]= "18-49 "; demo[4]= "1839AK"; demo[5]= "1849AK"; demo[6]= "25-54 "; demo[7]= "2554$+"; demo[8]= "2554AK"; demo[9]= "25AK60"; demo_count=9 demo_found="false" demo_extracted=head -n 1 $1 | awk '{print substr( $0, 34, 6) }' # check for blank trading demo if <check made with demo_extracted ='' is true> print $1 has no trading demo" else # loop and check if valid demo for (x=1; x<= demo_count; ++x) if demo[x]=demo_extracted then print demo[x] demo_found=true endfor if demo_found="false" print $1 has invalid trading demo } I am struggling with this (been googling etc.), so your help is very much appreciated. of course any better way would also be gratefully recieved. Gosh, I didnt think it would be so complicated! ![]() cheers T |
| Sponsored Links |
|
|
|
|||
|
I home u need this one only
awk -f sample.awk *.txt
Code of sample.awk BEGIN {demo[1]= "10-19 "; demo[2]= "18-39 "; demo[3]= "18-49 "; demo[4]= "1839AK"; demo[5]= "1849AK"; demo[6]= "25-54 "; demo[7]= "2554$+"; demo[8]= "2554AK"; demo[9]= "25AK60"; } { if(FNR==1) { found=0; for(i in demo) if(substr($0,34, 6)==demo[i]) { found=1; break; } if(!found) print FILENAME,"has invalid trading demo" } else next; } |
|
|||
|
ranjithpr
many thanks! you are fabulous. kindest regards T ![]() |
| Sponsored Links |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl: Match a line with multiple search patterns | Juha | Shell Programming and Scripting | 10 | 04-09-2008 02:43 AM |
| Multiple line match using sed | SiftinDotCom | Shell Programming and Scripting | 15 | 03-28-2008 02:12 PM |
| Concatenating multiple lines to one line if match pattern | phixsius | Shell Programming and Scripting | 13 | 01-24-2008 11:02 PM |
| processing line in file | fablef00 | Shell Programming and Scripting | 8 | 01-23-2006 11:41 AM |
| multiple file processing | jagannatha | UNIX for Dummies Questions & Answers | 4 | 05-03-2003 10:22 AM |