Checking for certain characters


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Checking for certain characters
# 1  
Old 07-17-2008
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.
# 2  
Old 07-17-2008
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.
# 3  
Old 07-17-2008
can't seem to get the above to work could you explain further please
# 4  
Old 07-17-2008
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
# 5  
Old 07-17-2008
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.
# 6  
Old 07-17-2008
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.
# 7  
Old 07-17-2008
Quote:
Originally Posted by phemanth24
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

Checking the user input in perl for characters and length

My question is basically as the title says. How can I check a user inputted string is only certain characters long (for example, 3 characters long) and how do I check a user inputted string only contains certain characters (for example, it should only contain the characters 'u', 'a', 'g', and 'c')... (4 Replies)
Discussion started by: Eric1
4 Replies

3. Shell Programming and Scripting

awk - checking last three characters

Hello, I am working with some very large files (upwards of 1M records). I have written code to parse out a lot of the data and am using awk rather than a built-in "while read LINE" for performance (I have tested both ways). That said, I now need to read each of these incoming lines, check the ninth... (2 Replies)
Discussion started by: dagamier
2 Replies

4. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

5. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

6. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

7. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

8. Shell Programming and Scripting

Help with checking that 2 variables contain matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario eg ... (3 Replies)
Discussion started by: lsecer
3 Replies

9. Shell Programming and Scripting

How to replace characters with random characters

I've got a file (numbers.txt) filled with numbers and I want to replace each one of those numbers with a new random number between 0 and 9. This is my script so far: #!/bin/bash rand=$(($RANDOM % 9)) sed -i s//$rand/g numbers.txtThe problem that I have is that it replaces each number with just... (2 Replies)
Discussion started by: hellocatfood
2 Replies

10. UNIX for Dummies Questions & Answers

Ksh Checking if string has 2 characters and does not contain digits?

How could I check if a string variable contains at least (or only) 2 characters, and check and make sure that the string does not contain any numeric digits?...I need to know how to do this as simple as possible. and I am using the Ksh shell. Thanks. (1 Reply)
Discussion started by: developncode
1 Replies
Login or Register to Ask a Question