Script formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script formatting
# 1  
Old 07-12-2011
Question Script formatting

Hi,



Code:
ls -ltre

Code:
  
  total 4
  drwxr-xr-x   9 wlsuser wlsgrp      1024 Feb 14 12:23:14 2011 _WL_internal
  drwxr-xr-x   3 wlsuser wlsgrp        96 Mar  8 18:11:33 2011 _WL_user
  -rw-r--r--   1 wlsuser wlsgrp        48 May 23 10:35:30 2011 WebServiceUtils.ser
  -rw-r--r--   1 wlsuser wlsgrp         0 Jun  5 15:49:24 2011 MyServer1.lok



Need a unix script to achieve the below ouput:

Choose any of the below files/folder:
1. _WL_internal
2. _WL_user
3. WebServiceUtils.ser
4. MyServer1.lok

Enter the number: 6
Output: Invalid Entry

Enter the number: 2
_WL_user was deployed on 8 Mar 2011 at 18:11:33 ET


This is all i can do:
Code:
 for i in $(ls); do echo $n : $i; n=`expr $n + 1`;  done



     read x





Can you please help?


Last edited by vbe; 07-12-2011 at 01:50 PM.. Reason: code tags!!! You have been warned
# 2  
Old 07-12-2011
try awk:

Code:
awk 'BEGIN{while("ls|grep -v total" |getline line){l=split(line,arr);a[++p]=arr[l]" was deployed on "arr[7]" "arr[6]" at"arr[8]" ET"}}
    while (getline line <"/dev/tty"){
        if(a[line]) {print a[line]}
        else{
            if(line=="q"||line=="Q"){exit}
            else {print"Output: Invalid Entry"
            }
        }
    }
}'

1
_WL_internal was deployed on 14 Feb at12:23:14 ET
2
_WL_user was deployed on 8 Mar at18:11:33 ET
3
WebServiceUtils.ser was deployed on 23 May at10:35:30 ET
4
MyServer1.lok was deployed on 5 Jun at15:49:24 ET
5
Output: Invalid Entry
6
Output: Invalid Entry
Q

# 3  
Old 07-15-2011
Question

Getting error: Can you help please?

./timestamp.sh <pid>
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 2
awk: bailing out near line 2


more timestamp.sh
Code:
cd $(dirname $(pfiles $1 |grep tmp | grep .lok))/_WL_user
awk 'BEGIN{while("ls|grep -v total" |getline line){l=split(line,arr);a[++p]=arr[l]" was deployed on "arr[7]" "arr[6]" at"arr[8]" ET"}}
    while (getline line <"/dev/tty"){
        if(a[line]) {print a[line]}
        else{
            if(line=="q"||line=="Q"){exit}
            else {print"Output: Invalid Entry"
            }
        }
    }
}'

Shortened it:

awk 'BEGIN{while("ls|grep -v total" |getline line)}'
awk: syntax error near line 1
awk: illegal statement near line 1


uname -a
SunOS mymachine Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220

Last edited by mohtashims; 07-15-2011 at 03:10 PM..
# 4  
Old 07-15-2011
try using nawk or /usr/xpg4/bin/awk
# 5  
Old 07-15-2011
Now I am getting the below with nawk

./timestamp.sh 21100
nawk: syntax error at source line 2
context is
>>> while <<< (getline line <"/dev/tty"){
extra }
nawk: bailing out at source line 2


Also,

nawk 'BEGIN{while("ls|grep -v total" |getline line)}'

is not resulting any output or error
# 6  
Old 07-15-2011
Code:
awk 'BEGIN{while("ls|grep -v total" |getline line){l=split(line,arr);a[++p]=arr[l]" was deployed on "arr[7]" "arr[6]" at"arr[8]" ET"}}


Hi
mohtashims,
please remove the red "}", and try again, hope it can work.
if not, I would have no idea, since I'm not fimiliar with your system.

Best,

Y



---------- Post updated at 02:35 PM ---------- Previous update was at 02:32 PM ----------

Quote:
nawk 'BEGIN{while("ls|grep -v total" |getline line)}'

is not resulting any output or error
for this issue, try :
Code:
ls|grep -v total|awk '{l=split($0,arr);a[++p]=arr[l]" was deployed on "arr[7]" "arr[6]" at"arr[8]" ET"}
 END{while (getline line <"/dev/tty"){
         if(a[line]) {print a[line]}
         else{
             if(line=="q"||line=="Q"){exit}
             else {print"Output: Invalid Entry"
             }
         }
}
}'

This User Gave Thanks to yinyuemi For This Post:
# 7  
Old 07-19-2011
Question

Removing the '}' did help.

However, wanted to understand how does a script with and the one without both work fine ?

Also, I need to know how to print the script variable inside awk.

For eg:

timezone="$(date | awk '{print $5}')"

nawk 'BEGIN{while("ls -lte|grep -v total" |getline line){l=split(line,arr);a[++p]=arr[l]" was deployed on "arr[7]" "arr[6]" "arr[9]" at "arr[8]" $timezone"}'

Output:
$timezone

What i need is the value of timezone variable.

Many thanks,
Mohtashim
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need some help in formatting answer of shell script

Dear all, I am new to shell scripting and have tried to make a small program for fun sake that tells age from the last number of cell phone and birth year. At the end we get a final 3 digit number and the first digit represents the last digit of cell phone and rest 2 numbers shows age. eg. 423... (4 Replies)
Discussion started by: stg44
4 Replies

2. Shell Programming and Scripting

Formatting output of script

I'm looking for a way to neaten up the out put of an ldapquery. I might get one, none, or several returns for each query; and each query consists of multiple lines. And while each return will contain the same types of info, it might be in a different order, like: uniq: 1 ip: 192.168.2.3... (3 Replies)
Discussion started by: jnojr
3 Replies

3. Shell Programming and Scripting

formatting a shell script

there is one shell which calls for different sql scripts. output of different are formatted in frame like structure.i want columnar wise formatting to be done in such a manner that output of one sql may be comon input of rest sqls . ERC DML Closure Date ER DML SAP Transfer Date ... (0 Replies)
Discussion started by: arc
0 Replies

4. Shell Programming and Scripting

Help needed in formatting script files

Hi, Can anyone tell me how i can convert all tab spaces inside a script to 4 spaces through another script. Also i need to find if all the quotes are matching and ended properly. Any idea whould be of great help. Many thanks! (3 Replies)
Discussion started by: justchill
3 Replies

5. Shell Programming and Scripting

Formatting the output of a script

Dear readers, I have a script that counts the number of files in particular directories in my home location and displays the output. Now I have 13 directories in my home location. I am getting the output as : Avishek_dir 13 Kunal_dir 17 Shantanu_dir 18 Arup_dir 12 Pranabesh_dir 19 . .... (7 Replies)
Discussion started by: avishek007
7 Replies

6. Shell Programming and Scripting

formatting script

Disclaimer: I did not set out to write the next big thing... I'm simply collecting all of the group information for all of the users across disparate hosts. I wrote a simple script to collect all of the group information for each user on a system and pushed it out to each host: #!/bin/sh... (7 Replies)
Discussion started by: avronius
7 Replies

7. Shell Programming and Scripting

Script for data formatting

Hi I have to convert the data in a file ******* 01-20-09 11:14AM 60928 ABC Valuation-2009.xls 01-20-09 11:16AM 55808 DEF GHI Equation-2009.xls 01-20-09 11:02AM 52736 ABC DF Valuation-2009.xls 01-20-09 11:06AM 89600 THE... (6 Replies)
Discussion started by: shekhar_v4
6 Replies

8. Shell Programming and Scripting

awk script formatting

Hello, I have got the following kine in my script awk '{printf("%s,", $0);next}{printf("%s", $0)}' ORS="," a.txt > b.out The contents of b looks somewaht like this: QUEUE(QUEUE1.Q),CURDEPTH(0),QUEUE(QUEUE2.Q),CURDEPTH(0),QUEUE(QUEUE3.Q),CURDEPTH(0) But my desired output is :... (10 Replies)
Discussion started by: King Nothing
10 Replies

9. Shell Programming and Scripting

Formatting Script Output

Hello all, Can somebody please offer some advice. I'm working on a small script to list all packages on the local and remote systems and need it in the following format. Machine Name, Package, Version Here is what i have so far but i can't seem to get the output of the hostname to loop to... (3 Replies)
Discussion started by: liketheshell
3 Replies

10. UNIX for Advanced & Expert Users

Date Formatting in Shell Script

Hi, Can anybody throw somelight on how to handle date in a shell script? I need to pass the date in this format 'yymmdd' and then pad it to a file. The way in which i've handled is given below, but the date is not getting passed rather the file is getting created with a null date. Any... (2 Replies)
Discussion started by: Isiva
2 Replies
Login or Register to Ask a Question