Comparing and Formatting the text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing and Formatting the text file
# 1  
Old 10-07-2010
Comparing and Formatting the text file

hi,

I need a script which can format the below text file which contains comments
Code:
 
 
 
file1.txt
--------
 
//START
//Name: some value
//Date:
//Changes:.............
//.....................
//END
//START
//Date:
//Name: some value
//Changes:.............
//.....................
//END

Output should be:
Code:
Name |Date|Changes

Script should compare the column name and paste the output in above said manner.

I am trying this, can anybody please help me on this.
# 2  
Old 10-07-2010
Code:
awk '
BEGIN{print "Name |Date|Changes"}
/Name:/    {$1="";n=$0}
/Date:/    {$1="";d=$0} 
/Changes:/ {$1="";c=$0}
/END/      {print n " | " d " | " c;n=d=c=""}
' file1.txt

# 3  
Old 10-07-2010
Here is my trial text with some more data added
Code:
file1.txt
---------
 
//START
//Name: some value
//Date:
//Changes:Change A
//more of change A
//END
//START
//Date:
//Name: some value
//Changes:This is change b
//And some more info
//END

And the awk program
Code:
fixfile.awk
-----------
BEGIN {
    FS=":" ; OFS="|"
        num=split("Name,Date,Changes",cols,",")
        print cols[1],cols[2],cols[3]
}
{
   $1=substr($1, 3);
   s=$2
   sub(/^ */, "", s);
   if ($1 == "END") print res[1], res[2], res[3]
   else {
       if (res[3] != "" && NR=1) res[3]=res[3]" "$1
       for(i=1; i<=num; i++) {
           if (cols[i] == $1) res[i]=s;
       }
   }

And the result
Code:
$ awk -f fixfile.awk file1.txt
Name|Date|Changes
some value|23rd June 2009|Change A more of change A
some value|16th May 2010|This is change b And some more info


Last edited by Chubler_XL; 10-07-2010 at 10:10 PM.. Reason: typo
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 10-08-2010
Thank you very much for your replies.

hi rdcwayx,

The script given by you is not working as required. If the changes made exceeds more then one line, then it is not working.

hi Chubler_XL,

your script is working perfectly.
Now i need to extend this to more columns.
Can you please explain the logic written especially the below part:

Code:
   $1=substr($1, 3);
   s=$2
   sub(/^ */, "", s);



---------- Post updated at 06:15 AM ---------- Previous update was at 02:58 AM ----------

hi Chubler_XL,

I have done some analysis and found that the below code is for the following purpose.

$1=substr($1, 3); #Would remove the two backslashes at the starting of the line

sub(/^ */, "", s); # Would remove the leading whitespaces in the result.

I have tried modifying the above code for four feilds. But it didnt work.

Sample file:
-----------

Code:
START
Name: some value
Date:
Function Name: kakjkldj
asdkjfkjlfj
Changes:Change A
more of change A
END


Ouput should be:
Code:
Name |Date|Function Name|Changes


Since am new to awk concepts, please help me out in understanding the logic written.....

Last edited by flamingo_l; 10-11-2010 at 07:19 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting text file

Hi All, how to format text Thanks (19 Replies)
Discussion started by: ROCK_PLSQL
19 Replies

2. Shell Programming and Scripting

Formatting a text file

Hi All :), I have a formatting question and I am unsure on how I should proceed with my bash shell script. I am unsure weather to use perl or simply edit it in bash. I prefer bash but I am only aware of the awk utility to extract parts of a file, not edit output. Scenario: I have a file... (5 Replies)
Discussion started by: bstrizzy
5 Replies

3. Shell Programming and Scripting

[Solved] Formatting the text file

Hi All, I ahve requirement where I want to put the text file in into proper format. I am wondering how can i achieve that:- Host/Alias Name IP Address Resolved sinuiy01.infra.go2uti.com 10.240.8.158 N sinuid20.devtst.go2uti.com 10.240.8.230 N sinuid21.devtst.go2uti.com... (6 Replies)
Discussion started by: sharsour
6 Replies

4. UNIX Desktop Questions & Answers

COMPARING COLUMNS IN A TEXT FILE

Hi, Good day. I currently have this data called database.txt and I would like to check if there are no similar values (all unique) on an entire row considering the whole column data is unique. the data is as follows cL1 cL2 cL3 cL4 a12 c13 b13 c15 b11 a15 c19 b11 c15 c17 b13 f14 with... (1 Reply)
Discussion started by: whitecross
1 Replies

5. UNIX for Dummies Questions & Answers

Comparing a number in a text file with a specific value

My project is to get a temperature reading from a refridgerator every 2 minutes and check to see if the door has been left open. I don't yet have the mastery of Linux, being a complete noob, but I reckon I need a text file with the latest temperature reading in it. This I've managed to do by... (2 Replies)
Discussion started by: Fitch
2 Replies

6. Shell Programming and Scripting

Formatting the text file using shell script

How to add the filename to end of each line with | as seperator, except first and last line of the file(s) in directories(with diff tree structure) using shell script?. And also how to replace a list of strings with another set of strings, which is present in a file?. Kindly help out on... (1 Reply)
Discussion started by: av_vinay
1 Replies

7. Shell Programming and Scripting

Formatting text file in unix

Hi, I am using the following format command for formatting my text file in unix. awk -F":" '{ printf "%-50s%-1s%-50s\n", $1,":", $2}' filename > targetfile The target file is then sent as an attachment via email. When I view the target file in notepad multiple lines get spanned as a... (2 Replies)
Discussion started by: AAA
2 Replies

8. UNIX for Dummies Questions & Answers

formatting of the text file

Hi Guys, I have a file with contents in the below format DO_VJ_IDOC;03.23.2009;22:31:09; ZJDO_VJ_IDOC;03.23.2009;22:46:14; ZJDO_RESEND_FAILURES;03.24.2009;01:46:18; Now i need to replace the semicolons with tabs for which i am usig the sed command which gives the O/p as below ... (1 Reply)
Discussion started by: rohit.shetty84
1 Replies

9. UNIX for Dummies Questions & Answers

Text file formatting

Hi all! I'm new in unix, and faced with some difficulties. So I have text file f.e. "textfile" which contains rows like: aaa bbb ccc ddd How could I format it, so the file looks like: aaabbb cccddd Thanks in andvance (5 Replies)
Discussion started by: consta.v
5 Replies

10. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies
Login or Register to Ask a Question