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
# 8  
Old 11-22-2012
Please use code tags for code and data sample.

Try.. this as per what your input i see from here..Smilie

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

# 9  
Old 11-22-2012
Output coming now but not fully

see pamu this is output but all values not coming...some are missing
Code:
akshay@akshay-Vostro-460:~/NCL/Untitled Folder$ ./new.sh
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    
akshay@akshay-Vostro-460:~/NCL/Untitled Folder$


Last edited by Franklin52; 11-22-2012 at 08:39 AM.. Reason: Please use code tags for data and code samples
# 10  
Old 11-22-2012
Have you tried my code(post 8)...?
# 11  
Old 11-22-2012
Yes its displaying output...whether it is possible to write data into new file..and every time its difficult to edit script because I have files (of type F1.txt )about 290
# 12  
Old 11-22-2012
Quote:
Originally Posted by Akshay Hegde
Yes its displaying output...whether it is possible to write data into new file..and every time its difficult to edit script because I have files (of type F1.txt )about 290
Yes we can write data into new file

if you want to create separate file
use

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

Or if you want to write into a one single file
use

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

pamu
# 13  
Old 11-22-2012
Hats off to you...Great Solution ! I wasn't expected that it will be solved..thank you so much....


---------- Post updated at 07:40 AM ---------- Previous update was at 07:34 AM ----------

Just now I had tried with one big file.....its not working
# 14  
Old 11-22-2012
Well, you might want to try this one, based on pamu's suggestion, adapted to your multiline records. May not work on all versions of awk:
Code:
awk  '{gsub (/[ ;{}]/,"")}
     /^DEPTH=/       {m=split($2, D, /,\n*/)}
     /^TEMPERATURE=/ {n=split($2, T, /,\n*/)}
     /^SALINITY=/    {o=split($2, S, /,\n*/)}
     END {for (i=1;i<=n; i++) print D[i],T[i],S[i]}
    ' FS="=" OFS="\t" RS= file
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

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