awk substr issue


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk substr issue
# 1  
Old 05-19-2014
awk substr issue

Hi,

I am trying to print last few characters of a string but unable to get the correct output through awk substr.

The length of the string and the number of characters to be printed varies day to day.

For example,To print 1234 from abcdef1234, i will be given 2 strings,string1(for example abcdef1234) and another string2 (for example nnnn) whose length is the number of characters i need to print from reverse of string1.


My code is:

Code:
# test.ksh 
#!/bin/ksh
file2=$2
file=$1
echo file=$file
len=`expr ${#file2}`
final_string=`echo $file | awk '{print substr($0,length($file)-length($file2),length($file2))}'`
echo $final_string



Execution: ksh -x test.ksh 1234567890987 nnnn
Output:
Code:
+ file2=nnnn
 + file=1234567890987
 + echo file=1234567890987
 file=1234567890987
 + + expr 4
 len=4
 + + awk {print substr($0,length($file)-length($file2),length($file2))}
 + echo 1234567890987
 final_string=1234567890987
 + echo 1234567890987
 1234567890987



Any help on this scenaio?
# 2  
Old 05-19-2014
change the awk line to below
Code:
awk -v file="${file}" -v file2="${file2}" '{print substr($0,length(file)-length(file2),length(file2))}'

# 3  
Old 05-19-2014
Try
Code:
echo "$file" | awk -vf2="$file2" '{ print substr ($0, length ($0) - length(f2) + 1) }'

# 4  
Old 05-19-2014
Hi,

I rewrote the code as below:
Code:
#!/bin/ksh

file2=$2
file=$1
echo file=$file
echo file2=$file2
final_string=`echo $file | awk -v file="${file}" -v file2="${file2}" '{print substr($0,length(file)-length(file2),length(file2))}'`
echo $final_string

But i am getting an error:

Code:
$ksh -x test.ksh 1234567890987 nnnn
+ file2=nnnn
+ file=1234567890987
+ echo file=1234567890987
file=1234567890987
+ echo file2=nnnn
file2=nnnn
+ + awk -v file=1234567890987 -v file2=nnnn {print substr($0,length(file)-length(file2),length(file2))}
+ echo 1234567890987
awk: syntax error near line 1
awk: bailing out near line 1
final_string=
+ echo

# 5  
Old 05-20-2014
Quoting Don Cragun:
Quote:
As always, if you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and substr

Hello All; I have an input file 'abc.txt' with below text: 512345977,213458,100021 512345978,213454,100031 512345979,213452,100051 512345980,213455,100061 512345981,213456,100071 512345982,213456,100091 512345983,213457,100041 512345984,213451,100011 I need to paste the first field... (10 Replies)
Discussion started by: mystition
10 Replies

2. Shell Programming and Scripting

HELP : awk substr

Hi, - In a file test.wmi Col1 | firstName | lastName 4003 | toto_titi_CT- | otot_itit - I want to have only ( colones $7,$13 and $15) with code 4003 and 4002. for colone $13 I want to have the whole name untill _CT- or _GC- 1- I used the command egrep with awk #egrep -i... (2 Replies)
Discussion started by: georg2014
2 Replies

3. Shell Programming and Scripting

awk substr

Hello life savers!! Is there any way to use substr in awk command for returning one part of a string from declared start and stop point? I mean I know we have this: substr(string, start, length) Do we have anything like possible to use in awk ? : substr(string, start, stop) ... (9 Replies)
Discussion started by: @man
9 Replies

4. Shell Programming and Scripting

Substr with awk

Hi to all, I'm here again, cause I need your help to solve another issue for me. I have some files that have this name format: date_filename.csv In my shell I must rename each file removing the date so that the file name is filename.csv To do this I use this command: fnames=`ls ${fname}|... (2 Replies)
Discussion started by: leobdj
2 Replies

5. Shell Programming and Scripting

awk substr

HI I am using awk and substr function to list out the directory names in the present working directory . I am using below code ls -l | awk '{ if ((substr($1,1,1)) -eq d) {print $9 }}' But the problem is i am getting all the files and directories listed where as the requirement i wrote... (7 Replies)
Discussion started by: prabhu_kumar
7 Replies

6. Shell Programming and Scripting

substr issue

Hi I have a script: ls -l | grep "$TDATE" | awk '{print $NF}' > todays_files.txt for run in $(cat todays_files.txt) do SSTR='expr substr "$run" 1 5' echo "$SSTR" done I want 1 to 5 chars of each files. It returns instead for all files. 'expr substr "$run" 1 5' (4 Replies)
Discussion started by: dipeshvshah
4 Replies

7. Shell Programming and Scripting

Help with awk and substr

I have the following to find lines matching "COMPLETE" and extract parts of it using substr. sed -n "/COMPLETE/p" 1.txt | awk 'BEGIN { FS = "\" } {printf"%s %s:%s \n", substr($3,17,3),substr($6,4,1), substr($7,4,1)}' | sort | uniq > temp.txt Worked fine until the numbers in 2nd & 3rd substr... (5 Replies)
Discussion started by: zpn
5 Replies

8. UNIX for Dummies Questions & Answers

awk or substr

i have a variable 200612 the last two digits of this variable should be between 1 and 12, it should not be greater than 12 or less than 1 (for ex: 00 or 13,14,15 is not accepted) how do i check for this conditions in a unix shell script. thanks Ram (3 Replies)
Discussion started by: ramky79
3 Replies

9. Shell Programming and Scripting

How to use awk substr ?

Hi all, I have a flatfile I would like to get ext = 7950 , how do I do that ? if ($1 == "CTI-ProgramStart") { ext = substr($9,index($9,"Extension")+11,4); But why it is not working ???? Please help . Thanks (1 Reply)
Discussion started by: sabercats
1 Replies

10. Shell Programming and Scripting

awk substr?

Sorry if this has been posted before, I searched but not sure what I really want to do. I have a file with records that show who has logged into my application: 2003-03-14:I:root: Log_mesg: registered servername:userid. (more after this) I want to pull out the userid, date and time into... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question