The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ftp checking ust Shell Programming and Scripting 0 07-09-2008 11:30 PM
Ksh Checking if string has 2 characters and does not contain digits? developncode UNIX for Dummies Questions & Answers 1 04-08-2008 12:19 PM
EOF checking the below ramkrix High Level Programming 10 03-10-2008 10:43 PM
Checking for PXE maestro@altiris SUN Solaris 5 05-24-2004 09:06 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-17-2008
Registered User
 

Join Date: Jun 2008
Posts: 18
Checking for certain characters

Could anyone help with the following enquiry.. i have a file in the following format:

ID .... VALUE
A001 .... 100
B002 .... 200
A004 .... 300
B006 .... 100
A997 .... 200
B776 .... 400

It is in a column format, but I want to check that the ID field always begins with with and A or B character this is my logic thus far:


If Character 1 DOES NOT equal A or B
then
display error message
else
carry on doing what you want
fi

not really sure how to chech that character 1 of each line does not equal A or B
habe tried following with no no joi

If [ ! grep '^A' $file || ! grep '^B' $file ] then
print "error"
else
print "it works"
fi

but the above does not work i believe it to logic as my unix understanding not great, please assist.
Reply With Quote
Forum Sponsor
  #2  
Old 07-17-2008
Registered User
 

Join Date: Apr 2008
Location: Hyderabad, India
Posts: 14
Try this:

/(^A)|(^B)/ {num++}
END {if(num > 0);printf("%d instances of A,B exist\n", num);}

You can modify the output the way you want.
Reply With Quote
  #3  
Old 07-17-2008
Registered User
 

Join Date: Jun 2008
Posts: 18
can't seem to get the above to work could you explain further please
Reply With Quote
  #4  
Old 07-17-2008
Registered User
 

Join Date: Apr 2008
Location: Hyderabad, India
Posts: 14
Hi Samz. I should have elaborated further.
I put my code into a script and ran it with awk.

Code:
awk -f <script name> <file>
Currently it prints the number of instances of A and B
Reply With Quote
  #5  
Old 07-17-2008
Registered User
 

Join Date: Apr 2008
Location: Hyderabad, India
Posts: 14
BTW, you could also bypass putting this into a script.

Code:
awk '/(^A)|(^B)/ {num++} END {if(num > 0);printf("%d instances of A,B exist\n", num);}' column
'column' is the file where I have the format you specified.
Reply With Quote
  #6  
Old 07-17-2008
Registered User
 

Join Date: Mar 2002
Posts: 26
The input file:

Code:
$ cat ttt
ID .... VALUE
-------------
A001 .... 100
C003 .... 800
B002 .... 200
corrupt
data
A004 .... 300
C003 .... 800
foo .... bar
The script:
Code:
#!/bin/ksh

INPUT=ttt

{ while read LINE
do
  echo $LINE |egrep "^A|^B" > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "Processing $LINE"
  else
    echo "Skipping $LINE"
  fi
done } < $INPUT
The output:
Code:
$ ./ttt.ksh
Skipping ID .... VALUE
Skipping -------------
Processing A001 .... 100
Skipping C003 .... 800
Processing B002 .... 200
Skipping corrupt
Skipping data
Processing A004 .... 300
Skipping C003 .... 800
Skipping foo .... bar
You could do a single string of commands using awk for the pattern matching, but I'm not sure how you want to process the line once you verify it's good... so this may offer the most flexibility.

Let us know if you need anything in the script explained.
Reply With Quote
  #7  
Old 07-17-2008
Registered User
 

Join Date: Jun 2008
Posts: 18
Quote:
Originally Posted by phemanth24 View Post
BTW, you could also bypass putting this into a script.

Code:
awk '/(^A)|(^B)/ {num++} END {if(num > 0);printf("%d instances of A,B exist\n", num);}' column
'column' is the file where I have the format you specified.
Ok the above only tell me how many times A or B where in there. I require it to send an error message if a C exist or otherwise its fine to continue processing file.. hope that makes more sense
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0