Problem with Substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Substring
# 1  
Old 10-29-2008
Problem with Substring

Hi,
i have a file from which i extracted 2nd column and put it into a variable Acct_Num . Now i need to extract first 3 digits from it and save in to other variable.
My Code is:

while read Record1
do
Acct_Num=`echo $Record1 | awk '{print $2 }'`
ID=`echo $Record1 | awk '{print substr($Acct_Num, 1, 3 ) }`
echo "ID is $ID"
echo "Acct_Num is $Acct_Num"
done < $REGSeqFileName

Code to extract 2nd column from file is working fine. But the code to extract first 3 digits from Acct_Num (ID) is not working fine .
Can anybody help me with this ASAP.

Thanks
# 2  
Old 10-29-2008
Quote:
Originally Posted by manmeet
Hi,
i have a file from which i extracted 2nd column and put it into a variable Acct_Num . Now i need to extract first 3 digits from it and save in to other variable.
My Code is:

Please put code inside [code] tags.
Quote:
Code:
 
  while read Record1
do
 Acct_Num=`echo $Record1 | awk '{print $2 }'`
 ID=`echo $Record1 | awk '{print substr($Acct_Num, 1, 3 ) }`


The immediate problem is that a shell variable is not expanded inside single quotes. You can pass a variable to awk with -v:

Code:
awk -v Acct_Num="$Acct_Num" '{print substr(Acct_Num, 1, 3 ) }'

The other problem is that your script will run excruciatingly slowly because you are calling awk twice for every line of the file.

Instead of using a shell loop, let awk do all the work:

Code:
awk '{
  printf "AcctNum is %s\n", $2
  printf "ID is %s\n", substr($2,1,3)
   }' "$REGSeqFileName"

# 3  
Old 10-30-2008
Thank you so much for solution...i'll try this way now...Smilie
# 4  
Old 10-30-2008
Can anybody tell me what this following code is doing?? i couldn't understand...

awk -F"," 'NR==FNR {col1[$1]=$3} NR!=FNR { if (col1[substr($2,2,3)]=="") {next} print $1}' $TargetSeqPath/Ref.tmp $TargetSeqPath/Master.tmp >> $TargetSeqPath/$OutFileName.dat


Basically $TargetSeqPath/Ref.tmp and $TargetSeqPath/Master.tmp are two files


Thanks for any help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substring Problem with sed . . .

Greetings. I'm looking to isolate the first occurrence of an arbitrary substring which may be present at any particular line in a given file. The enclosing end markers for the target in our thought problem are string" and ". The complete string and surrounding text could look something like... (3 Replies)
Discussion started by: LinQ
3 Replies

2. Shell Programming and Scripting

Substring

Hi All, In ksh, am trying to get a substring stuff done. Not sure where the problem is.. can you guys guide me on this... for instance, var1=41, and var2=4175894567, then i want to know whether var2 starts with var1.. var1 and var2 can be of any length.. VAR1=41 VAR2=419068567777... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

3. Shell Programming and Scripting

KSH: A PROBLEM awk SUBSTRING

Hi Gurus, I am working with a korn shell script to simplify some operations of creation file of spool for a Bulk-Copy. Thi is my code: In a for cycle: gzcat ${file} |awk '{ print substr($0,1,5)";"substr($0,108,122)";"substr($0,124,131)";"substr($0,139,152)";"substr($0,154,163)";"}' >>... (6 Replies)
Discussion started by: GERMANICO
6 Replies

4. Shell Programming and Scripting

Get the substring

Hi All, I have a ouput string likes 'u8wos' or 'u10acsd' or somthing else 'u{number}{any characters}'and I want to get the number behind the letter 'u' by bash shell. Thanks Damon (11 Replies)
Discussion started by: Damon_Qu
11 Replies

5. UNIX for Dummies Questions & Answers

Need help with substring

I need to check the occurrence of one string within another. code ******************** if ;then do something done ******************** Thanks (7 Replies)
Discussion started by: w020637
7 Replies

6. Shell Programming and Scripting

Substring problem

I have a file which comes with a few tags and some contents inside them. eg: <ABC>Hello Unix.com</ABC> <XYZ>WWSHF</XYZ> Now I want to pick out the contents in between the tags <ABC> and </ABC>. The contents within <ABC> and </ABC> spans more than one line. So greping and cutting is not an... (4 Replies)
Discussion started by: Rajat
4 Replies

7. Shell Programming and Scripting

Substring HELP!

Hi, I am trying to do something which I thought was very simple but still being a beginner, has proved not to be. Input: val1 val2 val3 val4 val5 val6 . . . etc Desired Output: Every row in which value of val6 is a number starting with 0.0 or contains a capital E. The input... (2 Replies)
Discussion started by: awknerd
2 Replies

8. Shell Programming and Scripting

substring

Dear All, i have a file that contains, FROM_DATE: 06-08-2007 00:00:00 TO_DATE: 06-08-2007 23:59:59 Total number of lines: 6874154 in another file,the contain is, FROM_DATE: 06-08-2007 00:00:00 Total number of lines: 874154 alltime i want to find the particular string... (4 Replies)
Discussion started by: panknil
4 Replies

9. UNIX for Dummies Questions & Answers

problem extracting substring in korn shell

hi all, I have read similiar topics in this board, but i didn' t find the posting which is the same with the problem i face.. I try to extract string from the end. i try to do this: num=abcdefghij num2=${num:-5} echo $num2 #this should print the last 5 characters (fghij) but it doesn;t... (3 Replies)
Discussion started by: nashrul
3 Replies

10. Shell Programming and Scripting

How do I Substring ??

Hello everyone. I'm writing a script in UNIX. The purpose is to get the second character from a variable that stores the system year. This is the code: unix_year_yy=`date "+%g"` This will return "07" in variable unix_year_yy. How can I get the second character (7)?? (6 Replies)
Discussion started by: Rigger
6 Replies
Login or Register to Ask a Question