How to seperate two lines that are joined?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to seperate two lines that are joined?
# 8  
Old 07-28-2012
A better solution with awk would be :
Code:
awk '{gsub(/([^*]*[*]){2} */,"&"RS);sub(/\n$/,"")}1' inputfile

# 9  
Old 07-28-2012
Why would that be better? The OP said that the basis for splitting is the recurrence of the first field, he did not mention asterisks..
# 10  
Old 07-28-2012
My bad..
I didn't read the thread fully...Smilie
Presumptions could be dangerous. Smilie
# 11  
Old 07-28-2012
Actually this would probably be a better solution:
Code:
awk '{for(i=2;i<=NF;i++)if($i==$1)$i=RS $i}1' infile

Otherwise partial matches with field 1 will also invoke a newline...
# 12  
Old 07-28-2012
Thank you very much but can u just explain me how that works? if i have to join two lines of same file based on common 1st field how do i do that? the exact opposite of previous one
# 13  
Old 07-28-2012
Quote:
Originally Posted by Scrutinizer
Actually this would probably be a better solution:
Code:
awk '{for(i=2;i<=NF;i++)if($i==$1)$i=RS $i}1' infile

Otherwise partial matches with field 1 will also invoke a newline...
And this approach isn't vulnerable to regular expression metacharacters in the data.

Regards,
Alister
# 14  
Old 07-28-2012
Quote:
Originally Posted by anurupa777
Thank you very much but can u just explain me how that works? if i have to join two lines of same file based on common 1st field how do i do that? the exact opposite of previous one
It means, for every line compare fields 2, 3 ... until the last field with the first field. If there is a match, prepend such a field with a newline character (so the record may contain one or more newline characters afterwards).
Then print the record ( The number 1) .

--
I would suggest you open a new thread for the join version..



--
Quote:
Originally Posted by alister
And this approach isn't vulnerable to regular expression metacharacters in the data. [..]
Good point.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Seperate columns according to delimiters

Hi all I need your help to separate colomns based on "-" delimiter for a very big file 30 millions rows I have a colmun looking like this : clomun 1 1-100000989-A_ATC 1-10000179-AAAAA 1-100002154-TGTTA 1-100002155-GTTAG 1-100002443 1-100002490 1-100002619 I need to separte in three... (5 Replies)
Discussion started by: biopsy
5 Replies

2. High Performance Computing

Huge Files to be Joined on Ux instead of ORACLE

we have one file (11 Million) line that is being matched with (10 Billion) line. the proof of concept we are trying , is to join them on Unix : All files are delimited and they have composite keys.. could unix be faster than Oracle in This regards.. Please advice (1 Reply)
Discussion started by: magedfawzy
1 Replies

3. Programming

USing two seperate programmes on same FIFO

hi this is a problem statement i have tried real hard but could not come up with a satisfactory program can someone help you are to create a pair of programs that do the following in this order: 1 one of the program creates a FIFO which another program should be able to write into 2 the... (2 Replies)
Discussion started by: puneetparnani
2 Replies

4. Shell Programming and Scripting

split a sentence and seperate into two lines

Hi, I have a string as str="route net,-hopcount,1,255.255.255.0,10.230.20.111,10.230.20.234 Route True route net,-hopcount,0,-netmask,255.255.248.0,0,10.230.23.254 Route True" I need to split this string into two lines as route net,-hopcount,1,255.255.255.0,10.230.20.111,10.230.20.234... (4 Replies)
Discussion started by: chaitanyapn
4 Replies

5. Shell Programming and Scripting

Set lines of in a file to seperate vars

In a bash script, I'm looking for a way to set each matching line of a file into its own variable, or variable array. As an example, i have a crontab file with several entries: 00 23 * * * /usr/local/bin/msqlupdate -all 00 11 * * * /usr/local/bin/msqlupdate -inc 00 03 * * *... (2 Replies)
Discussion started by: lochraven
2 Replies

6. Shell Programming and Scripting

how to awk a data from seperate lines

Hi guys, i have a problem which im hoping you will be able to help me with. I have follwing output :- ------------------------------------------------------------------------------- NSTEP = 407000 TIME(PS) = 43059.000 TEMP(K) = 288.46 PRESS = 0.0 Etot = -2077.4322 ... (2 Replies)
Discussion started by: Mish_99
2 Replies

7. Shell Programming and Scripting

how to seperate space in a string

Dear Members, I have string like this string1="file2.txt file4.txt kittu.txt file1.txt" in this i need to cut spaces and take each one has one file output should be --------- fileslist:4 file2.txt file4.txt kittu.txt file1.txt is it possible, please tell me how it possible ... (5 Replies)
Discussion started by: kittusri9
5 Replies

8. Shell Programming and Scripting

Script needs to be modified - Each 5 Rows to be joined in single line with comma (,)

Hi All, I'm using the following script to produce a result: #!/bin/sh awk ' $0 ~ /\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+Interface\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+/ { match_str="YES"; line_cnt=0; next; } { if((line_cnt < 5) && ( match_str=="YES")) { print $0; line_cnt += 1; } else... (0 Replies)
Discussion started by: ntgobinath
0 Replies

9. Shell Programming and Scripting

Need help to seperate data

Hello ALL, I really need help to grep data and store in particular format. I am struggling to write that script .Please help me to solve this problem otherwise i will loose my job... I have to compare 2 files and generate the staticts of the data. First file product.dat contains 2 column . ... (4 Replies)
Discussion started by: getdpg
4 Replies

10. Shell Programming and Scripting

row seperate

i have this sample data: test01 --- abc-01 name1 abc-02 name2 abc-03 name3 test02 --- abc-20 name4 abc-21 name5 test03 --- abc-22 name6 abc-23 name7 i want to generate a file... (13 Replies)
Discussion started by: inquirer
13 Replies
Login or Register to Ask a Question