substr issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substr issue
# 1  
Old 09-01-2010
substr issue

Hi

I have a script:
Code:
 
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.

Code:
'expr substr "$run" 1 5'


Could someone help me to fix the issue?

Thanks
# 2  
Old 09-01-2010
Code:
a="abcdefg"
echo ${a:0:5}

output:
abcde

# 3  
Old 09-01-2010
Try this instead of the loop:
Code:
ls -l | awk -v d="$TDATE" '$0 ~ d{print substr($NF,1,5)}'

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 09-01-2010
MySQL

Thanks Franklin & Icon.
# 5  
Old 09-01-2010
You can also pipe the output from the awk to a code block { } around your loop and avoid writing to a file.

Code:
 
ls -l | grep "$TDATE" | awk '{print $NF}' |
{ while read run; do 
     echo ${run:0:5}
done }

read run not only reads the line but also evaluates to FALSE when it can't read the next to last line breaking while the look at the top.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substr

awk '/^>/{id=$0;next}length>=7 { print id, "\n"$0}' Test.txt Can I use substr to achieve the same task? Thanks! (8 Replies)
Discussion started by: Xterra
8 Replies

2. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: Bobby_2000
4 Replies

3. Shell Programming and Scripting

How to use if/else if with substr?

I have a command like this: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES" else if (substr ($0,37,1)==0 && NR == 3) print "NO"}' This syntax doesn't work. But I was able to get this to work: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES"}' ... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. 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

5. UNIX for Dummies Questions & Answers

substr

can anybody explain this code? thanks in advance..:) (6 Replies)
Discussion started by: janani_kalyan
6 Replies

6. Shell Programming and Scripting

substr not working

Hi I am trying to run this command in ksh ...its not working $line="123356572867116w1671716" actual_length = 16 cut_line=`awk 'BEGIN{print substr(ARGV,1,actual_length)}' "$line"` the substr is not giving me an output how can i make it done can anyone hwlp me on this cut_line=`awk... (2 Replies)
Discussion started by: pukars4u
2 Replies

7. Shell Programming and Scripting

get substr?

Hi, I have a long string like, aabab|bcbcbcbbc|defgh|paswd123 dedededede|efef|ghijklmn|paswd234 ghghghghgh|ijijii|klllkkk|paswd345 lmlmlmmm|nononononn|opopopopp|paswd456 This string is devided into one space between substrings. This substrings are, aabab|bcbcbcbbc|defgh|paswd123... (6 Replies)
Discussion started by: syamkp
6 Replies

8. UNIX for Dummies Questions & Answers

Substr

Hi, My input file is 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... .. .. I need to change the second field value from 2 to 1. i.e., 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... .. .. Thanks in advance. (9 Replies)
Discussion started by: deepakwins
9 Replies

9. Shell Programming and Scripting

Using substr

What is the more efficient way to do this (awk only and default FS) ? $ echo "jefe@alm"|awk '{pos = index($0, "@");printf ("USER: %s\n",substr ($0,1,pos-1))}' USER: jefe Thx in advance (2 Replies)
Discussion started by: Klashxx
2 Replies

10. Shell Programming and Scripting

Substr of variable

Dear All, I am new to Unix and need your help. I am trying to write b-shell script which will return substr of some variable like this d="asdfg" return Substr(d,1,5). Please help (5 Replies)
Discussion started by: giviut
5 Replies
Login or Register to Ask a Question