File convert


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File convert
# 1  
Old 06-12-2012
File convert

Hi ,

I Have an output from the a log file which I need to convert to a pipe delimiter, but the problem is , I am not able to find a proper delimiter b/w the words .

My log file

Code:
1. confirm the status     07:03:05 (U=AP2521)       ASBCS_39833.log           1268D7980: 891.118
2. Query status (ExecuteQuery)     08:39:28 (U=AP2521)      CNMSHK_39833.log          1268D7980: 891.118
3. confirm the status     07:22:03 (U=AP2521)       ASBCS_39833.log           1268D7980: 891.118

Expected output

Code:
1.|confirm the status|07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118
2.|Query status (ExecuteQuery)|08:39:28|(U=AP2521)|CNMSHK_39833.log|1268D7980:|891.118
3.|confirm the status|07:22:03|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

Since there is no proper delimiter , I tried to suppress the spaces and then change to pipe , but tr -s didn't work

Code:
tr -s '[:space:]' '|' < log.txt

Please need some help here

Thanks
Rashmi

Last edited by methyl; 06-12-2012 at 01:16 PM.. Reason: please use code tags
# 2  
Old 06-12-2012
Your tr needed to be two trcommands. I'm assuming that the white space is all space characters. If they are in fact tabs, that might be useful to know.
Assuming that the message text field always contains three space-separated "words", then we can build on your tr idea and then correct the message text field.

Code:
cat log.txt | tr -s ' ' | tr ' ' '|' | while read line
do
        part1=`echo "${line}"|cut -d\| -f1`
        part2=`echo "${line}"|cut -d\| -f2-4|tr '|' ' '`
        part3=`echo "${line}"|cut -d\| -f5-`
        #
        echo "${part1}|${part2}|${part3}"
done

./scriptname
1.|confirm the status|07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118
2.|Query status (ExecuteQuery)|08:39:28|(U=AP2521)|CNMSHK_39833.log|1268D7980:|891.118
3.|confirm the status|07:22:03|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

This User Gave Thanks to methyl For This Post:
# 3  
Old 06-12-2012
A sed suggestion:
Code:
sed 's/  */|/; h; s/.*  *\(..:..:...*\)/\1/; s/  */|/g; x; s/  *..:..:...*//; G; s/\n/|/' file

I am feeling lazy at the moment, so I assumed that there would only be one timestamp-ish sequence per line, xx:xx:xx where x is any character. If that's not a valid assumption (even if it is at the moment, it's best to be stricter to protect against the future), it's a very good idea to tighten ..:..:.. by explicitly matching digits.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 06-13-2012
Thanks a lot Methyl and Alister :-) .

Alister , I am not that well versed with Sed , can you please explain me the code
# 5  
Old 06-13-2012
... or using awk:
Code:
awk '$1=$1' OFS=\| log.txt

# 6  
Old 06-13-2012
Quote:
Originally Posted by Chubler_XL
... or using awk:
Code:
awk '$1=$1' OFS=\| log.txt

Note that there are spaces in the desired output's second field which your code will convert to pipes.

Regards,
Alister

---------- Post updated at 10:48 AM ---------- Previous update was at 09:32 AM ----------

Quote:
Originally Posted by rashmisb
Thanks a lot Methyl and Alister :-) .

Alister , I am not that well versed with Sed , can you please explain me the code
If you don't understand sed at all, you should first read your manual page, because, while the following explanation doesn't assume much, it probably won't make a whole lot of sense if you don't understand basic sed commands such as s, G, and x.

I hope that you'll find the following explanation helpful.

sed has two "spaces" with which you can work, the pattern space and the hold space. Think of them as two memory locations, or variables, which are available to you. While there are commands to copy and append the contents of one space into the other, all text manipulation (substitutions and deletions) operate on the pattern space.

s/ */|/; h; s/.* *\(..:..:...*\)/\1/; s/ */|/g; x; s/ *..:..:...*//; G; s/\n/|/
That is a sed script consisting of 8 semicolon-delimited commands. Let's break it down, looking at each one in turn, to see how they manipulate the contents of the pattern and hold spaces to achieve our goal.

We begin our analysis at a point in time just after sed has read the first line of data from your original post. Bold red highlighting indicates something that changed.

===============================================

0.

Initial State

pattern space:
1. confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

hold space:
<EMPTY>

===============================================

1.

Substitute the first series of spaces with a pipe.
s/ */|/

pattern space:
1.|confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

hold space:
<EMPTY>

===============================================

2.

Copy the line as it now exists into the hold space (think of it as a location where we can put some text for later use, like a shell variable)
h

pattern space:
1.|confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

hold space:
1.|confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

===============================================

3.

Delete everything before the timestamp
s/.* *\(..:..:...*\)/\1/

pattern space:
<DELETED TEXT>07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

hold space:
1.|confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

===============================================

4.

Substitute all series of spaces with a pipe
s/ */|/g

pattern space:
07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

hold space:
1.|confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

===============================================

5.

exchange the pattern and hold space.
x

pattern space:
1.|confirm the status 07:03:05 (U=AP2521) ASBCS_39833.log 1268D7980: 891.118

hold space:
07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

===============================================

6.

Delete everything after the second field
s/ *..:..:...*//

pattern space:
1.|confirm the status<DELETED TEXT>

hold space:
07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

===============================================

7.

Append to the pattern space a newline followed by the contents of the hold space.
G

pattern space:
1.|confirm the status
07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118


hold space:
07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

===============================================

8.

Substitute the first (in this case, the only) newline with a pipe.
s/\n/|/

pattern space:
1.|confirm the status|07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

hold space:
07:03:05|(U=AP2521)|ASBCS_39833.log|1268D7980:|891.118

===============================================

The pattern space now contains the desired output. Since the end of the script has been reached, sed will print the current contents of the pattern space before copying the next line of input into the pattern space.

Regards,
Alister

Last edited by alister; 06-13-2012 at 02:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. UNIX for Beginners Questions & Answers

Convert Excel File (xls) to tab delimited text file on AIX

Hi i have a problem in my job i try to convert an excel file (xls extention) to text file (tab delimited), but no result with this comand cat xxx.xls > xxx.txt Do you have eny idea? PS: sorry for my english Thanks!! (4 Replies)
Discussion started by: frisso
4 Replies

3. Shell Programming and Scripting

Need help to write a shell script to convert text file to excel file.

Hi Everyone, I want your help to write a script which will take text file as input and on the basis of delimiter ":"script will create excel sheet. Example input: IpAdress:InstanceName:Port:ServerName 10.255.255.1:abc:2232:xyz_abc Output should be an excel sheet like below: Column... (8 Replies)
Discussion started by: akabhinav18
8 Replies

4. UNIX for Advanced & Expert Users

Convert CSV file to nested XML file using UNIX/PERL?

we have a CSV which i need to convert to XML using Perl or Unix shell scripting. I was able to build this XML in oracle database. However, SQL/XML query is running for long time. Hence, I'm considering to write a Perl or shell script to generate this XML file. Basically need to build this XML... (3 Replies)
Discussion started by: laknar
3 Replies

5. Shell Programming and Scripting

How to convert excel file to csv file or text file?

Hi all, I need to find a way to convert excel file into csv or a text file in linux command. The reason is I have hundreds of files to convert. Another complication is the I need to delete the first 5 lines of the excel file before conversion. so for instance input.xls description of... (6 Replies)
Discussion started by: johnkim0806
6 Replies

6. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

7. Shell Programming and Scripting

Convert CSV file (with double quoted strings) to pipe delimited file

Hi, could some help me convert CSV file (with double quoted strings) to pipe delimited file: here you go with the same data: 1,Friends,"$3.99 per 1,000 listings",8158here " 1,000 listings " should be a single field. Thanks, Ram (8 Replies)
Discussion started by: Ram.Math
8 Replies

8. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies

9. Shell Programming and Scripting

Plz Help To convert xml file to text file using bourn shell scripts

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> <country_code>AK></country_code>... (0 Replies)
Discussion started by: ram2s2001
0 Replies

10. Shell Programming and Scripting

convert XML file into Text file(fixed length)

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> ... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question