Help!!! Shell script to parse data file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help!!! Shell script to parse data file.
# 1  
Old 08-14-2008
Help!!! Shell script to parse data file.

I am faced with a Smilie tricky problem to parse a data file ( May not be a tricky problem to the scripting guru's ).

Here is what I am faced with. I have a file with multiple rows of data and the rows are not of fixed length. "|" is used as a delimiters for individual columns and each row of data has 5 columns.

I have tried `cat data.out` and then looping through to read the lines, but since there are spaces, I am not able to read entire line of data and I am failing to read individual records.

How can I parse this data file and extract individual records? I have attached the data file to this post and also copied few records from the data file below.(at the end of this posting).

Any help/suggestions is highly appreciated.

Thank you,
Ajay.


|test.txt

|2008-7-2.19.19. 0.
162000000|/default/main/administration/STAGING/Configuration/TeamSite/local/config

|/opt/iw-home/TeamSite/local/config

|34 |
|test.txt


|2008-7-2.19.13. 29. 529000000|/default/main/administration/STAGING/Configuration/TeamSite/local/config

|/opt/iw-home/TeamSite/local/config

|23
|
|test.txt

|2008-7-2.19.23. 5.
692000000|/default/main/administration/STAGING/Configuration/TeamSite/local/config

|/opt/iw-home/TeamSite/local/config

|11 |
|test.txt


|2008-7-3.10.30. 42. 912000000|/default/main/administration/STAGING/Configuration/TeamSite/local/config

|/opt/iw-home/TeamSite/local/config

|16
|
|New File.txt

|2008-7-2.19.19. 0.
162000000|/default/main/administration/STAGING/Configuration/TeamSite/local/config

|/opt/iw-home/TeamSite/local/config

|22 |
|New File.txt


|2008-7-2.19.13. 29.
529000000|/default/main/administration/STAGING/Configuration/TeamSite/local/config

|/opt/iw-home/TeamSite/local/config

|24 |
# 2  
Old 08-14-2008
I don't know whether ur data in the file is really of multiple rows (i mean 5 columns u r looking for), coz when i opened the file, it contains lot of spaces in it.
What i did is below and got the file to parse and convert it into comma delimited. You can do what ever processing you want.

awk -F'|' 'gsub(/ /,""){print $2 ","$3 "," $4 ","$5 "," $6}' testout1.txt
# 3  
Old 09-24-2008
One of many ways to remove spaces:

Code:
 tr -d " " <testout>testout1

Above command will remove all spaces from testout and generate new file testout1.
Go ahead with any kind of processing.
# 4  
Old 09-24-2008
Quote:
Originally Posted by Dhruva
One of many ways to remove spaces:
To remove every blank might not be desirable because some values might contain blanks. In a first step i would remove the blanks trailing and leading the delimiter characters:

Code:
sed 's/ *|/|/g;s/| */|/g' file > file.new

I hope this helps.

bakunin
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 Parse the XML data along with the URL in Shell Script?

Hi, Can anybody help to solve this. I want to parse some xmldata along with the URL in the Shell. I'm calling the URL via the curl command Given below is my shell script file export... (7 Replies)
Discussion started by: Megala
7 Replies

2. Shell Programming and Scripting

AWK script to parse a data in a file

Hi Unix gurus.. I have a file which has below data, It has several MQ Queue statistics; QueueName= 'TEST1' CreateDate= '2009-10-30' CreateTime= '13.45.40' QueueType= Predefined QueueDefinitionType= Local QMinDepth= 0 QMaxDepth= 0 QueueName= 'TEST2' CreateDate= '2009-10-30'... (6 Replies)
Discussion started by: dd_psg
6 Replies

3. UNIX for Dummies Questions & Answers

Help to parse csv file with shell script

Hello ! I am very aware that this is not the first time this question is asked here, because I have already read a lot of previous answers, but none of them worked, so... As said in the title, I want to read a csv file with a bash script. Here is a sample of the file: ... (4 Replies)
Discussion started by: Grhyll
4 Replies

4. Shell Programming and Scripting

Bash Shell Script to parse file

Raw Results: results|192.168.2|192.168.2.1|general/udp|10287|Security Note|For your information, here is the traceroute from 192.168.2.24 to 192.168.2.1 : \n192.168.2.24\n192.168.2.1\n\n results|192.168.2|192.168.2.1|ssh (22/tcp)|22964|Security Note|An SSH server is running on this port.\n... (2 Replies)
Discussion started by: jroberson
2 Replies

5. Shell Programming and Scripting

Parse file from 2nd line in shell script

Hi, I need to parse input file from 2nd line. Input file contents are, ABC123;20100913115432;2000000;NO; 04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;; 04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5; 04;CCC;09;DDD;601020304;CCCC;1;;;;; For each line, 1] I need to check... (17 Replies)
Discussion started by: Poonamol
17 Replies

6. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

7. Shell Programming and Scripting

Parse XML file in shell script

Hi Everybody, I have an XML file containing some data and i want to extract it, but the specific issue in my file is that the data is repeated some times like the following example : <section1> <subsection1> X=... Y=... Z=... <\subsection1> <subsection2> X=... Y=... Z=...... (2 Replies)
Discussion started by: yassine
2 Replies

8. UNIX for Advanced & Expert Users

shell script to parse html file

hi all, i have a html file something similar to this. <tr class="evenrow"> <td class="data">added</td><td class="data">xyz@abc.com</td> <td class="data">filename.sql</td><td class="modifications-data">08/25/2009 07:58:40</td><td class="data">Added TK prof script</td> </tr> <tr... (1 Reply)
Discussion started by: sais
1 Replies

9. Shell Programming and Scripting

parse data between parenthesis using shell script

Hi I am using shell script and i need to parse the data between parenthesis.How do i do it using shell script. Ex: section(name). I want to extract name from the above string using shell script. (4 Replies)
Discussion started by: julie_s
4 Replies

10. Shell Programming and Scripting

Parse a string in XML file using shell script

Hi! I'm just new here and don't know much about shell scripting. I just want to ask for help in creating a shell script that will parse a string or value of the status in the xml file. Please sample xml file below. Can you please help me create a simple script to get the value of status? Also it... (46 Replies)
Discussion started by: ayhanne
46 Replies
Login or Register to Ask a Question