Needed shell script to read txt file and do some modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Needed shell script to read txt file and do some modification
# 1  
Old 11-21-2012
Needed shell script to read txt file and do some modification

Hi ...programmers...

I need a shell script to perform some specific task..

my txt file looks like this
Code:
netcdf new {
dimensions:
    XAX1_11 = 11 ;
variables:
    double XAX1_11(XAX1_11) ;
        XAX1_11:point_spacing = "even" ;
        XAX1_11:axis = "X" ;
    float DEPTH(XAX1_11) ;
        DEPTH:missing_value = -1.e+34f ;
        DEPTH:_FillValue = -1.e+34f ;
        DEPTH:long_name = "DEPTH" ;
        DEPTH:history = "From new.txt" ;
    float TEMPERATURE(XAX1_11) ;
        TEMPERATURE:missing_value = -1.e+34f ;
        TEMPERATURE:_FillValue = -1.e+34f ;
        TEMPERATURE:long_name = "TEMPERATURE" ;
        TEMPERATURE:history = "From new.txt" ;
    float SALINITY(XAX1_11) ;
        SALINITY:missing_value = -1.e+34f ;
        SALINITY:_FillValue = -1.e+34f ;
        SALINITY:long_name = "SALINITY" ;
        SALINITY:history = "From new.txt" ;

// global attributes:
        :history = "FERRET V6.82   21-Nov-12" ;
        :Conventions = "CF-1.0" ;
data:

 XAX1_11 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ;

 DEPTH = 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 ;

 TEMPERATURE = 28.25, 28.26, 28.23, 28.22, 28.2, 28.19, 28.18, 28.18, 28.18, 
    28.19, 28.19 ;

 SALINITY = 36.42, 36.42, 36.43, 36.43, 36.42, 36.43, 36.43, 36.43, 36.44, 
    36.45, 36.44 ;
}

I have of need of output in following way
Code:
DEPTH    TEMPR    SALINITY
1    28.25    36.42
5    28.26    36.42
10    28.23    36.43
15    28.22    36.43
20    28.2    36.42
25    28.19    36.43
30    28.18    36.43
35    28.18    36.43
40    28.18    36.44
45    28.19    36.45
50    28.19    36.44


kindly programmers give me the script to get this output, I have lot of txt files of same format..


Thanks in advance

- Akshay

Last edited by Franklin52; 11-21-2012 at 05:51 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-21-2012
Please use code tags for code and data sample

try

Code:
awk -F "[=;]" '/DEPTH/{n=split($2,DP,",")}
/TEMPERATURE/{m=split($2,TM,",")}
/SALINITY/{o=split($2,SL,",")}END{print "DEPTH","TEMPERATURE","SALINITY";
for(i=1;i<=m;i++){print DP[i],TM[i],SL[i]}}' file

DEPTH TEMPERATURE SALINITY
 1  28.25  36.42
 5  28.26  36.42
 10  28.23  36.43
 15  28.22  36.43
 20  28.2  36.42
 25  28.19  36.43
 30  28.18  36.43
 35  28.18  36.43
 40  28.18  36.44
 45 28.19 36.45
 50   28.19   36.44

# 3  
Old 11-22-2012
Solution not working...

commands not working, and unable to read .txt file,
is there any other solution ?
# 4  
Old 11-22-2012
Quote:
Originally Posted by Akshay Hegde
commands not working, and unable to read .txt file,
is there any other solution ?
What you have tried? Please show us.
# 5  
Old 11-22-2012
Dear Pamu your solution not working...I had pasted as you given in my script...

---------- Post updated at 06:00 AM ---------- Previous update was at 05:59 AM ----------

Code:
for file in *.txt; do
echo $file
awk -F "[=;]" '/DEPTH/{n=split($2,DP,",")}
/TEMPERATURE/{m=split($2,TM,",")}
/SALINITY/{o=split($2,SL,",")}END{print "DEPTH","TEMPERATURE","SALINITY";
for(i=1;i<=m;i++){print DP[i],TM[i],SL[i]}}' file

done


Last edited by Franklin52; 11-22-2012 at 07:59 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 11-22-2012
Quote:
Originally Posted by Akshay Hegde
Dear Pamu your solution not working...I had pasted as you given in my script...

---------- Post updated at 06:00 AM ---------- Previous update was at 05:59 AM ----------

for file in *.txt; do
echo $file
awk -F "[=;]" '/DEPTH/{n=split($2,DP,",")}
/TEMPERATURE/{m=split($2,TM,",")}
/SALINITY/{o=split($2,SL,",")}END{print "DEPTH","TEMPERATURE","SALINITY";
for(i=1;i<=m;i++){print DP[i],TM[i],SL[i]}}' file

done
It should be $file.

Code:
$ cat F1.txt
netcdf new {
dimensions:
    XAX1_11 = 11 ;
variables:
    double XAX1_11(XAX1_11) ;
        XAX1_11:point_spacing = "even" ;
        XAX1_11:axis = "X" ;
    float DEPTH(XAX1_11) ;
        DEPTH:missing_value = -1.e+34f ;
        DEPTH:_FillValue = -1.e+34f ;
        DEPTH:long_name = "DEPTH" ;
        DEPTH:history = "From new.txt" ;
    float TEMPERATURE(XAX1_11) ;
        TEMPERATURE:missing_value = -1.e+34f ;
        TEMPERATURE:_FillValue = -1.e+34f ;
        TEMPERATURE:long_name = "TEMPERATURE" ;
        TEMPERATURE:history = "From new.txt" ;
    float SALINITY(XAX1_11) ;
        SALINITY:missing_value = -1.e+34f ;
        SALINITY:_FillValue = -1.e+34f ;
        SALINITY:long_name = "SALINITY" ;
        SALINITY:history = "From new.txt" ;

// global attributes:
        :history = "FERRET V6.82   21-Nov-12" ;
        :Conventions = "CF-1.0" ;
data:

 XAX1_11 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ;

 DEPTH = 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 ;

 TEMPERATURE = 28.25, 28.26, 28.23, 28.22, 28.2, 28.19, 28.18, 28.18, 28.18,28.19, 28.19 ;

 SALINITY = 36.42, 36.42, 36.43, 36.43, 36.42, 36.43, 36.43, 36.43, 36.44,36.45, 36.44 ;
}

Code:
$ awk -F "[=;]" '/DEPTH/{n=split($2,DP,",")}
/TEMPERATURE/{m=split($2,TM,",")}
/SALINITY/{o=split($2,SL,",")}END{print "DEPTH","TEMPERATURE","SALINITY";
for(i=1;i<=m;i++){print DP[i],TM[i],SL[i]}}' F1.txt
DEPTH TEMPERATURE SALINITY
 1  28.25  36.42
 5  28.26  36.42
 10  28.23  36.43
 15  28.22  36.43
 20  28.2  36.42
 25  28.19  36.43
 30  28.18  36.43
 35  28.18  36.43
 40  28.18  36.44
 45 28.19 36.45
 50   28.19   36.44

# 7  
Old 11-22-2012
I had pasted as you given not even modified single word..pls help

and I need output in column wise depth in one column, temperature in one column,,,,so on....

Last edited by Akshay Hegde; 11-22-2012 at 07:43 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

2. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

3. Shell Programming and Scripting

Help with shell script - filter txt file full of ips

Hello again gentlemen. I would like to make a shell script to 'optimize' a plain text full of IPs. Let's suppose to have this text file: 1.192.63.253-1.192.63.253 1.0.234.46/32 1.1.128.0/17 1.116.0.0/14 1.177.1.157-1.177.1.157 1.23.22.19 1.192.61.0-1.192.61.99 8.6.6.6 I want to... (2 Replies)
Discussion started by: accolito
2 Replies

4. UNIX for Dummies Questions & Answers

C-Shell script help reading from txt file

I need to write a C-Shell script with these properties: It should accept two arguments on the command line. The first argument is the name of a file which contains a list of names, and the second argument is the name of a directory. For each file in the directory, the script should print the... (1 Reply)
Discussion started by: cerce
1 Replies

5. UNIX for Dummies Questions & Answers

txt file modification which is beyond me

Dear all, I 'd like to create a new txt file using the old file. For example, in old file, if count=2 then in new file, repeat that row twice, with the only difference is: on the first row, 'start' column contains the 1st apart of the 'start' in the old file; while in the 2nd row, the 'start'... (7 Replies)
Discussion started by: forevertl
7 Replies

6. Shell Programming and Scripting

Shell script to send an email from the txt file

Hi Friends, Could you guys help me out of this problem... I need to send an email to all the users and the email has to be picked from the text file. text file contains the no. of records like: giridhar 224285 847333 giridhar276@gmail.com ramana 84849 33884 venkata.ramana@gmail.com... (6 Replies)
Discussion started by: giridhar276
6 Replies

7. Shell Programming and Scripting

Shell Script Needed to Read a text from a list files

Hi, Below is my issue which I desperately need and I want a shell script which can do this job. I need this script as I m planning to put this for a system health check. Please assist me. 1. There are 10 log files in a particular location. 2. open each log file. Goto to the end of the... (4 Replies)
Discussion started by: kashriram
4 Replies

8. UNIX for Dummies Questions & Answers

File comparision and modification using shell script

Hello everyone, I would like to know how to compare two files and modify any differences with some other data using shell script. I think it would be better understood with an example. I got two files named 'filex' and filey'. 'filex' is constant file without any changes in data. 'filey' is... (2 Replies)
Discussion started by: maddy81
2 Replies

9. UNIX for Dummies Questions & Answers

Shell script: last modification date for a file

Hi i have a ques in Shell scripting: ques: accept a filename as a command line argument. Validate the input and display the last modification date for that file. Help pls. (4 Replies)
Discussion started by: onlyc
4 Replies

10. AIX

How to edit txt file by shell script?

What I want to do is just delete some lines from a text file, I know it's easy using copy and redirect function, but what I have to do is edit this file (delete the lines) directly, as new lines may be added to the text file during this period. Can AIX do this ? # cat text 1:line1 2:line2... (3 Replies)
Discussion started by: dupeng
3 Replies
Login or Register to Ask a Question