Hi,
ls -ltre
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
[FONT="]
Need a unix script to achieve the below ouput:
Choose any of the below files/folder:
_WL_internal
_WL_user
WebServiceUtils.ser
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:
for i in $(ls); do echo $n : $i; n=`expr $n + 1`; done
read x
Can you please help?
try awk:
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
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
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
try using nawk or /usr/xpg4/bin/awk
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
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 ----------
for this issue, try :
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"
}
}
}
}'
1 Like
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
panyam
July 19, 2011, 11:48am
8
pass it using -v, some thing like this:
timezone="$(date | awk '{print $5}')"
nawk -v tz="$timezone" '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]" tz}'
1 Like