Creating delimiter file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating delimiter file
# 1  
Old 07-25-2011
Creating delimiter file

HTML Code:
#/bin/sh
sysdate=`date +"%m/%d/%Y"`
systime=`date +%r`
ps_per=`lsps -s | nawk '{print $2+0}'|tail -1`   
ps_tot=`lsps -s | nawk '{print $1+0}'|tail -1`      
lcpu=`vmstat | nawk -F= '/lcpu/ {print $2+0}'`   
mem_tot=`vmstat | nawk -F= '/mem=/ {print $3+0}'`    
avm=`vmstat|awk '{print $3/256}'|tail -1`          

# starting sar collection for cpu usage
sar -P ALL -o $tmpFile  1 3
#CPU usage information
cpusage=`sar -f $tmpFile | grep -p Average | awk '{print $2,$3,$4,$5}'`
cpu_usr=`echo $cpusage | awk '{print $1}'`
cpu_sys=`echo $cpusage | awk '{print $2}'`
cpu_wio=`echo $cpusage | awk '{print $3}'`
cpu_idle=`echo $cpusage | awk '{print $4}'`
I will be running this script very often with cron and need to get a delimiter file for smooth data import to Excel.

A new line needs to be added under the column names (sysdate, systime, ps_per, etc) in horizontal.
HTML Code:
sysdate       systime          ps_per    etc
07/25/2011  03:08:25 PM       2         ....
07/25/2011  03:12:25 PM       1         ....
07/25/2011  03:16:25 PM       3         ....
Please advise.

Thank you so much
# 2  
Old 07-25-2011
What is wrong with:
Code:
echo $sysdate'|'$systime'|'$ps_per'|'...

# 3  
Old 07-26-2011
PHP Code:
#/bin/sh

tmpFile=/tmp/sartemp.out
outputFile
=/tmp/delimeter.out

sysdate
=`date +"%m/%d/%Y"`
systime=`date +%r`
ps_per=`lsps -s | nawk '{print $2+0}'|tail -1`   
ps_tot=`lsps -s | nawk '{print $1+0}'|tail -1`      
lcpu=`vmstat | nawk -F= '/lcpu/ {print $2+0}'`   
mem_tot=`vmstat | nawk -F= '/mem=/ {print $3+0}'`    
avm=`vmstat|awk '{print $3/256}'|tail -1`          

# starting sar collection for cpu usage
sar -P ALL -o $tmpFile  1 3
#CPU usage information
cpusage=`sar -f $tmpFile | grep -p Average | awk '{print $2,$3,$4,$5}'`
cpu_usr=`echo $cpusage | awk '{print $1}'`
cpu_sys=`echo $cpusage | awk '{print $2}'`
cpu_wio=`echo $cpusage | awk '{print $3}'`
cpu_idle=`echo $cpusage | awk '{print $4}'`

echo `
$sysdate`|`$systime`|`$ps_per`|`$ps_tot`|`$lcpu`|`$mem_tot`|`$avm`|`$cpusage`|`$cpu_usr`|`$cpu_sys`|`$cpu_wio`|`$cpu_idle` >> $outputFile 
On the execution, I am getting errors.
PHP Code:
./sysmon.sh[23]: 10:50:18:  not found.
./
sysmon.sh[23]: 1:  not found.
./
sysmon.sh[23]: 07/26/2011:  not found.
./
sysmon.shThere is no process to read data written to a pipe.
./
sysmon.sh[23]: 128000:  not found.
./
sysmon.sh[23]: 16:  not found.
./
sysmon.sh[23]: 63871:  not found.
./
sysmon.sh[23]: 47989:  not found.
./
sysmon.sh[23]: 13:  not found.
./
sysmon.sh[23]: 13:  not found.
./
sysmon.sh[23]: 2:  not found.
./
sysmon.sh[23]: 1:  not found.
./
sysmon.sh[23]: 84:  not found
Please advise.
# 4  
Old 07-26-2011
Why did you put backtick (`) in the "echo" command instead of the single quote (') that I recommended?
# 5  
Old 07-26-2011
If I put single quote (') instead of backtick (`), I am getting these errors.

Please advise.

PHP Code:
./sysmon.sh[23]: $ps_per:  not found.
./
sysmon.sh[23]: $ps_tot:  not found.
./
sysmon.sh[23]: $lcpu:  not found.
./
sysmon.sh[23]: $mem_tot:  not found.
./
sysmon.sh[23]: $avm:  not found.
./
sysmon.sh[23]: $cpusage:  not found.
./
sysmon.sh[23]: $cpu_usr:  not found.
./
sysmon.sh[23]: $cpu_sys:  not found.
./
sysmon.sh[23]: $cpu_wio:  not found.
./
sysmon.sh[23]: $cpu_idle:  not found.
./
sysmon.sh[23]: $systime:  not found.
./
sysmon.shThere is no process to read data written to a pipe
# 6  
Old 07-26-2011
Please, post what your current code.

Pay close attention to what I suggested.
This User Gave Thanks to Shell_Life For This Post:
# 7  
Old 07-26-2011
Daniel, backquotes have a special meaning in shell scripting as does the pipe character '|' outside of a quotes that you're using. The backquotes execute commands on the same line as another command and then substitute their output in that spot for the original command. The pipe character tells a command to use its output for the input of the next command (i.e., redirect standard output to standard input in UNIX parlance).

Code:
ms:/u/campbelm>X='whoami'
ms:/u/campbelm>echo "I am $X"
I am whoami
ms:/u/campbelm>echo "I am `$X`"
I am campbelm
ms:/u/campbelm>

So in your code, you're trying to execute whatever is held in those variables -- meaning it's like you're running the literal line '$ps_per' on the command line like so:
Code:
ms:/u/campbelm>\$ps_per
-ksh93: $ps_per: not found.

So to fix the output, use double quotes around the entire line (don't use single quotes because they don't interpret the variables you assigned as varialbes, i.e., they're 'strong' quotes).

Code:
ms:/u/campbelm>X=1
ms:/u/campbelm>Y=2
ms:/u/campbelm>echo "$X|$Y"
1|2
ms:/u/campbelm>echo '$X|$Y'
$X|$Y
ms:/u/campbelm>


Last edited by Nitrodist; 07-26-2011 at 04:47 PM..
This User Gave Thanks to Nitrodist For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. UNIX for Dummies Questions & Answers

Getting the folder name and file name after delimiter

Hi, I have a input /dev/cm/test1.txt /qa/tm/hmkr/cc/test2.txt and I need an out like below foldername, filename /dev/cm/,test1.txt /qa/tm/hmkr/cc/,test2.txt I tried with awk $NF, but I'm getting the filenames and not folder names. Please let me know how to achive the above... (5 Replies)
Discussion started by: somu_june
5 Replies

3. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

4. UNIX for Advanced & Expert Users

File Delimiter

Hi All, I woul like to know with out opening a file in unix ,how we can find out what is the delemeter in that file... Thanks.. edit by bakunin: changed thread title to "delimiter" so it can be found. (4 Replies)
Discussion started by: raju4u
4 Replies

5. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/4313'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

6. Shell Programming and Scripting

Delimiter in output file

Hello, I am trying to find the record count in a specific folder, Here is the part of the code =========================== STARTDATE=`date +"%y%m%d%H%M"` for i in `ls *.DAT` do wc -l $i >> /XYZ/SrcFiles/"Record_counts"$STARTDATE.csv ... (2 Replies)
Discussion started by: Shanks
2 Replies

7. Shell Programming and Scripting

creating delimiter file & append with cron

I have the following script working fine, and need to generate a file delimiter (with tab or special character) for Excel data import. The script will run every hour in crontab to append the new rows to the delimiter, so that I can collect the data for i.e. a week, which will give me a lot of... (0 Replies)
Discussion started by: Daniel Gate
0 Replies

8. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

9. Shell Programming and Scripting

creating a delimiter string

hi i have a function printValues() { var=$# count=0 qName="" while do if then echo QManager Name $1 fi if then echo Cluster Name$2 fi if (( $count != 0 && $count != 1 )) then ... (0 Replies)
Discussion started by: satish@123
0 Replies

10. Shell Programming and Scripting

splitting file with more than one delimiter

Hi, I just wandering how to split a record which has more than one delimiter, i have a file which contains pattern as group separtor and ~ as field separtor, Ultimately I need consider even the groups as a field, So i need to make this multi-delimited file into ~ delimited file. My record... (4 Replies)
Discussion started by: braindrain
4 Replies
Login or Register to Ask a Question