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
Expected output
Since there is no proper delimiter , I tried to suppress the spaces and then change to pipe , but tr -s didn't work
Please need some help here
Thanks
Rashmi
Last edited by methyl; 06-12-2012 at 01:16 PM..
Reason: please use code tags
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.
A sed suggestion:
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.
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: 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.
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)
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)
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)
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)
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)
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)
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)
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)
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)