substr


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers substr
# 1  
Old 03-16-2009
substr

Quote:
awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwxs]/)*2^(8-i));if(k)printf(" %0o ",k);print}'
can anybody explain this code?

thanks in advance..Smilie
# 2  
Old 03-17-2009
can you provide the some data of the file on which this awk command has been run..
that will help us to explain it in much better way
# 3  
Old 03-17-2009
in ls -l to list files in numerals like 766 755 etc

ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwxs]/)*2^(8-i));if(k)printf(" %0o ",k);print}'
# 4  
Old 03-17-2009
this will convert the file permission from rwxs to octal values..
this simple perl code will also do that
Code:
perl -e 'for (@ARGV) { printf "%04o %s",  (stat)[2] & 07777, $_ }'

# 5  
Old 03-17-2009
I know that it will convert that.. but i cant understand how does it do that..
It will be better if u explain the code.
# 6  
Old 03-17-2009
Quote:
Originally Posted by janani_kalyan
I know that it will convert that.. but i cant understand how does it do that..
It will be better if u explain the code.
dear janani_kalyan,
I think this much explanation is enough.
I have explained a lot of code like this here so first time i said what it will do but i think you are very new to unix..
regards,
vidya
Code:
 
 awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwxs]/)*2^(8-i));if(k)printf(" %0o ",k);print}'
ls -l give a o/p like below
-rwxr-xr-x    1 fnsonlu1 fnsur             0 Mar 17 12:03 vidya
where $1=-rwxr-xr-x
first iteration of for loop when i=1
substr($1,2,1)=r  ##it will get the second char of $1 that is r
then it matches with either of r,w,x,s
if it matches with one of four then it adds 2 to the power 7
printf(" %0o ",k) then prints the k value in octal that is 7
the same continue till i reaches 8 that is last char of $1

# 7  
Old 03-17-2009
thanks vidhya. yes i'm new to unix so required lot of explanation thanks for your explaining me in detail.
 
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. 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

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

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

5. UNIX for Dummies Questions & Answers

substr of a file

Hi, i'm a newbie and i don't know unix... I'm a dba oracle. I need to cat the content of a file like this: > ps -eaf|grep pmon oracle 221422 1 0 Sep 17 - 7:20 ora_pmon_ORCL oracle 405626 1 0 Sep 17 - 8:39 ora_pmon_ORCL1 oracle 491534 1 0 ... (3 Replies)
Discussion started by: davyp74
3 Replies

6. UNIX for Dummies Questions & Answers

substr of a file

.wysiwyg { PADDING-RIGHT: 0px; PADDING-LEFT: 0px; BACKGROUND: #f5f5ff; PADDING-BOTTOM: 0px; MARGIN: 5px 10px 10px; FONT: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; COLOR: #000000; PADDING-TOP: 0px } .wysiwyg A:link { COLOR: #22229c } .wysiwyg_alink { COLOR:... (1 Reply)
Discussion started by: davyp74
1 Replies

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

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

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

10. 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
Login or Register to Ask a Question