Hi ,
In a directory i've the files in the following format
pay:year:mon:11789604
pay:year:mon:17675644
---
and i need to get 4th part of the above file name
so i used awk command in the below code
#!/bin/ksh
for test_data in pay*
do
txt_awk = awk -F':' '{print $4;}' $test_data
echo "$txt_awk"
done
Error:
awk: can't open
Hello,
Could you please try the following code and let me know.
txt_awk = `echo $test_data | awk -F':' '{print $4;}'`
Please do this change and try to run the script. It should work properly.
Thanks,
R. Singh
Hey,
If you want to take the fourth part of file name, you could do something like below,
ls pay*|awk -F':' '{print $4;}'
If you want to store the output of awk to a variable, you should use like below,
variable_name=`your command goes here`
or
variable_name=$(your command goes here)
Cheers!!!
-R
Thanks Ravinder, Rangarasan
I tried both of your option.
And the error is awk not found
#!/bin/ksh
for test_data in pay*
do
txt_awk = `echo $test_data | awk -F':' '{print $4;}'`
echo "$txt_awk"
done
And tried like below also
#!/bin/ksh
for test_data in pay*
do
txt_awk = `$test_data | awk -F':' '{print $4;}'`
echo "$txt_awk"
done
In both cases awk command not found error
Thanks
Yoda
June 26, 2013, 10:14am
5
You don't need an awk code inside for loop to do this. You can use parameter substitution:
#!/bin/ksh
for file in pay*
do
file="${file##*:}"
print "$file"
done
If you are writing a shell script, always use shell built-ins where ever possible.
1 Like
Thank You Yoda,
It worked .
Could you give some idea on command
${file##*:}
Yoda
June 26, 2013, 10:37am
7
${file##*:}
deletes the largest matching pattern (matches from beginning) until colon :
Since I put asterisks *
it deletes every character before that last colon :
For further reference: Parameter Substitution
1 Like
smile689:
You cannot have whitespace around the equals sign in a variable assignment. You made that mistake even when testing rangarasan's suggestion.
Also, I suspect that the correct error message involved "txt_awk" not being found, and not simply "awk".
Regards,
Alister