Merging lines in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging lines in a text file
# 8  
Old 10-14-2010
Data

hi All,

Sorry for the late reply. Thank you all the replies.

code given by birei is working ...

@Birei,

Am new to sed so am trying to understand the code given by you.

sed -n '1h;2,$ {/^Name:\|^Date:\|^Function Name:\|^Changes:/ {s/ *$//;x;s/\n/ /g;p}; /^Name:\|^Date:\|^Function Name:\|^Changes:/! {s/ *$//;H}}; $ {x;s/\n/ /g;p}' <file>

as i came to know that , 1h is used for holding multiple lines of a text file in a hold file.

am not able to understand 2, $........ .

Can you please help me on this.

---------- Post updated at 03:55 AM ---------- Previous update was at 03:53 AM ----------

[/COLOR]
Quote:
Originally Posted by Scrutinizer
Code:
sed '/:/N;s/\n\([^:]*\)$/\1/;s/[<>]//g' infile


Hi Scrutinizer,


The code given by you is working.


Code:
sed '/:/N;s/\n\([^:]*\)$/\1/;s/[<>]//g' infile
As an new to sed , can you explain the logic in above code.. I think it is taking the : as the criteria...

But i need a script which considers the sub heading Name, Date, function Name, Changes and then joins them.

---------- Post updated at 03:56 AM ---------- Previous update was at 03:55 AM ----------

Quote:
Originally Posted by danmero
And awk one Smilie
Code:
awk '{printf (toupper(substr($0,1,1))!=substr($0,1,1) ||NR==1)?$0:RS $0}' file


Hi,

This is not working for the below sample file:
Code:
 
 
START
Name: some value
Date:
Function Name:aaa
llllll
Changes:Change A
more of change A
END
START
Date:
Name: some value
Function Name:aaa
llllll
Changes:This is change b 
And some more info
END
START
Date:
Name: some value
Function Name:aaa
llllll
Changes:This is change b
And some more info
END

Am unable to figure out what is the problem here
# 9  
Old 10-14-2010
Please state you exact input and output criteria, since your latest sample file suddenly contains the words END and START and no longer contains angular brackets.
# 10  
Old 10-14-2010
Try this,

Code:
awk '{printf (/START/ || /END/ || /:/)?RS $0: $0}' inputfile

# 11  
Old 10-14-2010
Quote:
Originally Posted by Scrutinizer
Please state you exact input and output criteria, since your latest sample file suddenly contains the words END and START and no longer contains angular brackets.
My requirement is:

If my Function name extends 2 lines i need to merge to a single line.
Similarly changes or any other....

so i need to merge lines into 1 based on the heading --> Name , Date, Function Name, Changes.
# 12  
Old 10-14-2010
Is the order constant? Your example has the Date: and Name: changing places.
# 13  
Old 10-14-2010
Hi,

English isn't my native language, but I will try:

From second line to end of file I check if line begins with 'Name', 'Date', 'Function Name' or 'Changes':
1.- If that condition isn't met it means that this line will be appended to the end of the previous one so I remove trailing spaces and append it to 'hold space' without print anything.
2.- If that condition is met it means that this line will be the beginning of a line in the output, so I will print all previous line accumulated until here. I exchange 'hold space' and 'pattern space', keep current line in 'hold space' for a latter printing, retrieve lines accumulated in previous step, join removing newline characters and print them.

The first line is an exception, because I don't know if next line is the continuation of this one, so I save it in 'hold space' until check the next one.
The last line is an exception too. It is processed in range '2,$' so it will have been printed or appended to 'hold space'. I check if it is in 'hold space' and carry on same process that second step.
Code:
sed -n '
    ## First line.
    1h    
    ## From second to last line of file.
    2,$ {
        ## Point 2 in explanation.
        /^Name:\|^Date:\|^Function Name:\|^Changes:/ {
            s/ *$//
            x
            s/\n/ /g
            p
        } 
        ## Point 1 in explanation.
        /^Name:\|^Date:\|^Function Name:\|^Changes:/! {
            s/ *$//
            H
        }
    } 

    ## Last line.
    $ {
        x
        s/\n/ /g
        p
    }
' infile

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 14  
Old 10-15-2010
Bug

Quote:
Originally Posted by DGPickett
Is the order constant? Your example has the Date: and Name: changing places.

The order is may or may not be constant.

First, am trying to get a script with order as constant.

---------- Post updated at 02:23 AM ---------- Previous update was at 02:06 AM ----------

Quote:
Originally Posted by birei
Hi,

English isn't my native language, but I will try:

From second line to end of file I check if line begins with 'Name', 'Date', 'Function Name' or 'Changes':
1.- If that condition isn't met it means that this line will be appended to the end of the previous one so I remove trailing spaces and append it to 'hold space' without print anything.
2.- If that condition is met it means that this line will be the beginning of a line in the output, so I will print all previous line accumulated until here. I exchange 'hold space' and 'pattern space', keep current line in 'hold space' for a latter printing, retrieve lines accumulated in previous step, join removing newline characters and print them.

The first line is an exception, because I don't know if next line is the continuation of this one, so I save it in 'hold space' until check the next one.
The last line is an exception too. It is processed in range '2,$' so it will have been printed or appended to 'hold space'. I check if it is in 'hold space' and carry on same process that second step.
Code:
sed -n '
    ## First line.
    1h    
    ## From second to last line of file.
    2,$ {
        ## Point 2 in explanation.
        /^Name:\|^Date:\|^Function Name:\|^Changes:/ {
            s/ *$//
            x
            s/\n/ /g
            p
        } 
        ## Point 1 in explanation.
        /^Name:\|^Date:\|^Function Name:\|^Changes:/! {
            s/ *$//
            H
        }
    } 
 
    ## Last line.
    $ {
        x
        s/\n/ /g
        p
    }
' infile

Regards,
Birei

Thanks a lot Birei.....

Thought English is not your native language you explained it very neatly... Smilie

You have mentioned that:

Code:
2.- If that condition is met it means that this line will be the beginning of a line in the output, so I will print all previous line accumulated until here. I exchange 'hold space' and 'pattern space', keep current line in 'hold space' for a latter printing, retrieve lines accumulated in previous step, join removing newline characters and print them.

The one which is highlighted in bold is done by the following code

Code:
 
            s/ *$//
            x
            s/\n/ /g
            p

So, does x means exchanging ??
how are you keeping the current line in hold space??

As per my understanding ,

Code:
s/ *$//

This would remove the first occurence of leading space.

Code:
   s/\n/ /g

This would remove all the new lines.

Please clarify.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Merging the lines of a file

Hello, I have a file with few lines starting with a digit (1-5 only ) followed by a dot (.). Remaining all the lines to be merged with its previous numbered lines. Merging must be done with a space. E.g., Source file: 3. abc def xyz 5. pqr mno def 4. jkl uvw 7. ghi 1. abc xyz 6. mno... (4 Replies)
Discussion started by: magnus29
4 Replies

3. Shell Programming and Scripting

Merging multiple files using lines from one file

I have been working of this script for a very long time and I have searched the internet for direction but I am stuck here. I have about 3000 files with two columns each. The length of each file is 50000. Each of these files is named this way b.4, b.5, b.6, b.7, b.8, b.9, b.10, b.11, b.12... (10 Replies)
Discussion started by: iconig
10 Replies

4. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

5. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

6. Shell Programming and Scripting

Merging lines in a file

Hi, I want to merge the lines starting with a comma symbol with the previous line of the file. Input : cat file.txt name1,name2 ,name3,name4 emp1,emp2,emp3 ,emp4 ,emp5 user1,user2 ,user3 Output name1,name2,name3,name4 emp1,emp2,emp3,emp4,emp5 (9 Replies)
Discussion started by: mohan_tuty
9 Replies

7. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies

8. Shell Programming and Scripting

Urgent Need Help! Merging lines in .txt file

I need to write a script that reads through an input .txt file and replaces the end value with the end value of the next line for lines that have distance <=4000. The first label line is not actually in the input. In the below example, 3217 is the distance from the end of the first line to the... (12 Replies)
Discussion started by: awknerd
12 Replies

9. UNIX for Dummies Questions & Answers

merging two lines in a file

Hi All, I want to merge two lines in a file till the end of the file. So what could be the command to get so. say file name : sample.txt contents: country=1 send apps =1 rece=2 country=2 send apps =3 rece=3 .. ... output: country=1;send apps =1 rece=2 country=2;send apps =3... (6 Replies)
Discussion started by: thaduka
6 Replies

10. Shell Programming and Scripting

merging few columns of two text files to a new file

hi i need to select a few columns of two txt files and write it to a new file. there is one common field for both of these files. plz help me in this thanks in advance (4 Replies)
Discussion started by: kolvi
4 Replies
Login or Register to Ask a Question