Visit Our UNIX and Linux User Community


Shell Script:split the values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script:split the values
# 1  
Old 05-10-2013
Linux Shell Script:split the values

Hi,

I have file like below as file.txt

Code:
Linux sptpp1a 2.6.18 348.1.1.el5 xxxxx    05/02/2013

              %usr     %sys     %wio    %idle
14:18:30         0        1        0       99
14:18:40         1        1        0       98
14:18:50         1        1        0       98
14:19:00         0        1        0       99
14:19:10         1        2        0       97
14:19:20         0        0        0       99
14:19:30         0        0        0       99
14:19:40         1        1        0       98
14:19:50         2        1        0       97
14:20:00         8        1        0       91
14:20:10        20        3        0       77
14:20:20        26        2        0       72

I would like to get start and end time from file... I mentioned o/p below .

Output:
Code:
start=05/02/2013 14:18:30
end=05/02/2013 14:20:20

Thanks,
Mani

Last edited by Scrutinizer; 05-10-2013 at 04:05 AM..
# 2  
Old 05-10-2013
try this

Code:
admin@IEEE:~/Desktop$ cat test.txt
Linux sptpp1a 2.6.18 348.1.1.el5 xxxxx    05/02/2013

              %usr     %sys     %wio    %idle
14:18:30         0        1        0       99
14:18:40         1        1        0       98
14:18:50         1        1        0       98
14:19:00         0        1        0       99
14:19:10         1        2        0       97
14:19:20         0        0        0       99
14:19:30         0        0        0       99
14:19:40         1        1        0       98
14:19:50         2        1        0       97
14:20:00         8        1        0       91
14:20:10        20        3        0       77
14:20:20        26        2        0       72

Code:
admin@IEEE:~/Desktop$ cat test.sh

awk '/Linux/{start=$6}
       /usr/{getline;stm=$1}    
END{
    printf "start=" OFS start OFS stm
    printf "\n"
    printf "end=" OFS start OFS $1 
    printf "\n"
   }' test.txt

Code:
admin@IEEE:~/Desktop$ sh test.sh 
start= 05/02/2013 14:18:30
end= 05/02/2013 14:20:20

# 3  
Old 05-10-2013
Thanks Akshay.... one more small change...

O/p should be:

Code:
start=2013-05-02 14:18:30
end=2013-05-02 16:25:12

please help on this... sorry for inconvenience

Thanks
Mani

Last edited by Scott; 05-10-2013 at 04:34 AM.. Reason: Code tags (final time)
# 4  
Old 05-10-2013
Quote:
Thanks Akshay.... one more small change...

O/p should be:

start=2013-05-02 14:18:30
end=2013-05-02 16:25:12

please help on this... sorry for inconvenience

Thanks
Mani
I didn't find
Code:
16:25:12

in your file

if you want date to be formatted then

use this

Code:
awk '/Linux/{st=$6;split(st,A,"/");start=sprintf("%s-%s-%s",A[3],A[1],A[2])}

       /usr/{getline;stm=$1}    
END{
    printf "start=" OFS start OFS stm
    printf "\n"
    printf "end=" OFS start OFS $1 
    printf "\n"
   }' test.txt

O/P
Code:
start= 2013-05-02 14:18:30
end= 2013-05-02 14:20:20


Last edited by Akshay Hegde; 05-10-2013 at 04:44 AM..
# 5  
Old 05-10-2013
Thanks one again Akshay..... But i feel that will be little bit difficult to understand for me....

Any way i have noted that, if we can do the same in any different way( I meant any other comman)

Thanks,
Mani
# 6  
Old 05-10-2013
Code:
awk 'NR==1{S=$NF}
    !P && /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/{P=$1}
    END{printf "start="S,P"\nend="S,$1"\n"}' file


Previous Thread | Next Thread
Test Your Knowledge in Computers #152
Difficulty: Easy
In 1995, Novell transferred the Unix trademarks to the ACME Computing Company.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to Split matrix file with delimiter into multiple files

I have a large semicolon delimited file with thousands of columns and many thousands of line. It looks like: ID1;ID2;ID3;ID4;A_1;B_1;C_1;A_2;B_2;C_2;A_3;B_3;C_3 AA;ax;ay;az;01;02;03;04;05;06;07;08;09 BB;bx;by;bz;03;05;33;44;15;26;27;08;09 I want to split this table in to multiple files: ... (1 Reply)
Discussion started by: trymega
1 Replies

2. UNIX for Beginners Questions & Answers

How to pass values to a script called from within another script in shell?

Need ideas on how to achieve the below. We have a script say "profile.sh" which internally calls another existing script called "name.sh" which prompts for the name and age of a person upon execution. When i run profile.sh how can i populate a pre-defined value from another file and pass that... (1 Reply)
Discussion started by: sankasu
1 Replies

3. Shell Programming and Scripting

How to split files using shell script?

solid top facet normal 0 1 0 outer loop vertex 0 1 0 vertex 1 1 1 vertex 1 1 0 endloop endfacet facet normal 0 1 0 outer loop vertex 0 1 0 vertex 0 1 1 vertex 1 1 1 endloop endfacet endsolid top solid bottom facet normal 0 -1 ... (3 Replies)
Discussion started by: linuxUser_
3 Replies

4. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

5. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

6. Shell Programming and Scripting

SPLIT STRING in bash shell script

i need one help.... if i have a string like aaaaa,bbbbb,ccccc,aaaaa How to to split the string and check howmany times aaaaa will be in that string? Thanks (7 Replies)
Discussion started by: karthinvk
7 Replies

7. Shell Programming and Scripting

Help with Split shell script

Hi all, Following is the shell script which I am using for splitting the contents from a flat file to the multiple XMLs. The problem is that this script is working file when the input file is having 10000 lines. When the number of lines increases; the performance degrades drastically. Please... (1 Reply)
Discussion started by: amtrip
1 Replies

8. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

9. Shell Programming and Scripting

Shell Script: want to insert values in database when update script runs

Hi , I am new to linux and also also to shell scripting. I have one shell script which unpacks .tgz file and install software on machine. When this script runs I want to insert id,filename,description(which will be in readme file),log(which will be in log file) and name of unpacked folder... (1 Reply)
Discussion started by: ring
1 Replies

10. Shell Programming and Scripting

Shell script to split XML file

Hi, I'm experiencing difficulty in loading an XML file to an Oracle destination table.I keep running into a memory problem due to the large size of the file. I want to split the XML file into several smaller files based on the keyword(s)/tags : '' and '' and would like to use a Unix shell... (2 Replies)
Discussion started by: bayflash27
2 Replies

Featured Tech Videos