Replace var if conditon is met


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace var if conditon is met
# 1  
Old 07-25-2007
Replace var if conditon is met

HPUX 11i v2 #!/bin/sh

Hi all.

I am using a read to split a file into variables:

cat filename | while read VAR1 VAR2 ETC ETC
then a do to work each line

$VAR2 is always 9 characters. If the first character is NOT an A or a 9, then I need to mask the first 5 characters with *****.

Any ideas?

TIA!!!!!
# 2  
Old 07-25-2007
Code:
awk -F" " '{ x=substr($2, 0, 1); if ( x !~ "A" && x !~ "9" ) { $2 = "*****" substr($2, 6, length - 5); print } }' filename

# 3  
Old 07-25-2007
Code:
mVar2='123456789'
mFirst=`echo $mVar2 | cut -c1`
if [ "$mFirst" != "A" -a "$mFirst" != "9" ]; then
  mOutLine='*****'`echo $mVar2 | cut -c6-`
else
  mOutLine=$mVar2
fi
echo "OutLine = "$mOutLine

# 4  
Old 07-26-2007
why don't you build your variable file like so:

Code:
var1="this"
var2="that"
var3="the other"

then all you need to do is:

. var_file

job done
# 5  
Old 07-26-2007
Quote:
Originally Posted by matrixmadhan
Code:
awk -F" " '{ x=substr($2, 0, 1); if ( x !~ "A" && x !~ "9" ) { $2 = "*****" substr($2, 6, length - 5); print } }' filename

This works great, except....if VAR2 begins with A or 9 it doesn't print the line at all. How can I keep the the line intact if VAR2 begins with A or 9?

TIA!!!!!!!!
# 6  
Old 07-26-2007
I would say that was not mentioned before.

Else part needs to be added

this would do Smilie

Code:
awk -F" " '{ x=substr($2, 0, 1); if ( x !~ "A" && x !~ "9" ) { $2 = "*****" substr($2, 6, length - 5); print } else { print } }' filename

# 7  
Old 07-26-2007
Matrix

You're Awksome!

Thanks!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

2. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

sed to replace a line with multi lines from a var

I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file. myVar=`cat myfile` sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file myfile: cat dog boy girl mouse house test.file: football hockey Replace_Flag baseball ... (4 Replies)
Discussion started by: bblondin
4 Replies

4. Shell Programming and Scripting

Find and replace folder of files with $var

I want to scan through all the files in the folder and replace all instances of $file_X within the file with the variable $X defined in my bash script on my debian 6.0 install. For example, if the file contains $file_dep I want it to be replaced with the value of the variable $dep defined in my... (1 Reply)
Discussion started by: Spadez
1 Replies

5. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

6. Solaris

/var/adm & /var/sadm

what is the difference between tha /var/adm and /var/sadm files in solaris 10 Os please can any one respond quickly thanking you (2 Replies)
Discussion started by: wkbn86
2 Replies

7. Shell Programming and Scripting

Replace a var in sql file with shell script variable

I have a requirement where i have a sql file (filetext.sql). This file contains a variable ss_code. Now in a shell script im trying to replace the variable ss_code with a value contained in the shell script variable MTK_DC..tried the below in the script MTK_DC="mit,cit,bit" OUT=`awk -v... (4 Replies)
Discussion started by: michaelrozar17
4 Replies

8. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

9. Shell Programming and Scripting

replace <Address> with a var input by user

Hi, All here is not a full version script, just post a sample. #/bin/bash read -p 'Addr: ' addr sed 's/<Address>/'"$addr"'/' pf.mod>$1 If the input string include `/', sed will return error message. I know that s/// can be replaced with s::: or s!!!, etc, but the input var can accept... (4 Replies)
Discussion started by: r2007
4 Replies
Login or Register to Ask a Question