The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Major OS errors/Bash errors help!!!! wcmmlynn UNIX for Dummies Questions & Answers 12 11-13-2007 04:50 AM
Adapter Errors and Link Errors mcastill66 AIX 2 08-02-2005 06:51 PM
Adapter Errors and Link Errors mcastill66 UNIX for Advanced & Expert Users 0 08-02-2005 06:11 PM
errors nobody HP-UX 1 12-14-2003 10:08 AM
Help with errors Phaedirean Filesystems, Disks and Memory 5 08-12-2003 02:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-21-2005
billy5 billy5 is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 9
awk errors

Hello,
Can you please let me know how to find all lines that don't begin with pattern1, pattern2, pattern3 and pattern4?
Here is my awk script that gives an error.

awk 'BEGIN { NAME="$FILE"
GSNO=0 }
/^ISA/ { FIRST=$0;
LAST="IEA*1*" + "substr($0,90,9}" }
/^GS/ { GSNO=++;
NAME="$FILE"$GSNO;
print "$FIRST" >> $NAME;
print $0 >> $NAME}
[/!^ISA/[,/!^GS/[,/!^GE/[,/!^IEA/]]]]{
print $0 >> $NAME
}

/^GE/ { print $0 >> $NAME;
print $LAST >> $NAME }
/^IEA/ {} ' $FILE

The red part should mean 'if it does not begins with ISA, GS, GE, IEA. I don't know the syntax.

Thanks.
  #2 (permalink)  
Old 06-21-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,716
I'm having trouble reading your code, please use code tags.

Can you write a sentence that says what you are trying to do with the whole code block? Not just the part of pattern matching?
  #3 (permalink)  
Old 06-21-2005
billy5 billy5 is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 9
OK, this is what I want.
There is a file like this:

ISAtext1
GStext2
text3
text4
...
textn
GE
IEA

I want to do this:

if line begins ISA - do command1.
if line begins GS -do command2.
all other lines (means all except ISA, GS, GE, IEA) - do command3. - how to write this in awk???
if line begins GE - do commad4
if line begins IEA -do command5.

Hope I was more clear.

Thanks
  #4 (permalink)  
Old 06-21-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
I have fixed some of the syntax errors...

awk 'BEGIN { NAME="$FILE" <-- use -v to pass command line parameters
LAST="IEA*1*" + "substr($0,90,9}" } <-- use space for concatenation and don't surround functions by double quotes
/^GS/ { GSNO=++; <-- use either GSNO++ or GSNO+=1
NAME="$FILE"$GSNO; <-- there is no dollar sign when referring to variables
print "$FIRST" >> $NAME; <-- when writing to a new file just use > not >> (unless appending to an existing file)

Code:
awk -v FILE="NewFile" '
  BEGIN {
      NAME = FILE
      GSNO = 0
  }
  {
      if ($0 ~ /^ISA/) {
              FIRST = $0
              LAST = "IEA*1*" substr($0, 90, 9)
      } else if ($0 ~ /^GS/) {
              GSNO++
              NAME = FILE GSNO
              print FIRST > NAME
              print $0 > NAME
      } else if ($0 !~ /^(I[SE]A|G[SE])/) {
              print $0 > NAME
      } else if ($0 ~ /^GE/) {
              print $0 > NAME
              print LAST > NAME
      } else if ($0 ~ /^IEA/) {
              one = 1
      }
  } ' $FILE
  #5 (permalink)  
Old 06-22-2005
billy5 billy5 is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 9
Thank you very much Igor.
It works!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0