Reading two lines in a while loop and combining the lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading two lines in a while loop and combining the lines
# 1  
Old 04-19-2012
Reading two lines in a while loop and combining the lines

Dear all,
I have a file like this:
imput
Code:
scaffold_0 1
scaffold_0 10000
scaffold_0 20000
scaffold_0 25000
scaffold_1 1
scaffold_1 10000
scaffold_1 20000
scaffold_1 23283

and I want the output like this:
Code:
scaffold_0 1         scaffold_0 10000
scaffold_0 10000 scaffold_0 20000
scaffold_0 20000 scaffold_0 25000
scaffold_0 25000 scaffold_1 1
scaffold_1 1         scaffold_1 10000
scaffold_1 10000 scaffold_1 20000
scaffold_1 20000 scaffold_1 23283

NOTE: I want the columns separated by tab

In other words, I want to combine
line 1 with line 2
line 2 with line3
line 3 with line4
....

I have this script:
Code:
while read line1
do
    read line2
    echo $line1 $line2
done < imput

But the results is:
Code:
scaffold_0 1         scaffold_0 10000
scaffold_0 20000 scaffold_0 25000
scaffold_1 1         scaffold_1 10000
scaffold_1 20000 scaffold_1 23283

so the script read line 1 and line 2, combine them, but do not follow reading the line 2 and combining with line 3. The script like jump the line 2 and follow the combination of line 3 and line 4 and again jump the next line (line 4).
How can I fix this script?

Best regards.
Moderator's Comments:
Mod Comment How to use code tags

Last edited by Scrutinizer; 04-19-2012 at 01:16 PM..
# 2  
Old 04-19-2012
Could I suggest something like:-
Code:
prev=""
while read line
do
   if [ "$prev" != "" ]
   then
      echo "$prev\t$line"
   fi
   prev="$line"
done < imput

The plan here being that for each line, you echo the previous line and the current read line, except for the first line read (where there is no previous)


I hope that this helps



Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 04-19-2012
Try:
Code:
{ read line1   
  while read line2
  do
    printf "%s\t%s\n" "$line1" "$line2"
    line1=$line2
  done
} < imput

# 4  
Old 04-19-2012
Yes, that's neater. Sorry for being a fool and adding an if that will cost CPU especially for a large file. I wasn't thinking. Smilie




Robin
# 5  
Old 04-19-2012
Hi,

another way;
Code:
cat YUORFILE.txt | awk -F" " '{ if (  FNR == 1  ) {a=$0;getline} ;  {print a"\t"$0;a=$0} }'

This User Gave Thanks to pokerino For This Post:
# 6  
Old 04-19-2012
Thanks Robin, It is nice to have smart guys like you in computer.
I'm a newbie in programming, in fact I'm a biologist that is entering in bioinformatics.
So I have two question,
when I insert
prev=""
while read line do
is it meaning that the prev="" will be the current line when the loop start?
The condition:
if [ "$prev" != "" ]meaning what?

Sorry to disturb your work.

Best regards.
# 7  
Old 04-19-2012
@Robin, your suggestion look fine, I think it is more a matter of preference. Not every echo interprets a \t like that btw...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combining lines in one line

Hi below is the input file snippet. here i want that all the line which is coming after 1 shoud be in one line. so for exanple if after 1 there is two lines which is starting with 2 should be combine in one line. input file content 1,8091012,BATCH_1430903_01,21,T,2,808738,,,,21121:87:01,... (19 Replies)
Discussion started by: scriptor
19 Replies

2. Shell Programming and Scripting

Combining two lines into one, UNIX

Hi All, I have a file which has the following sample lines -- <Member name="Canada" Currency="CAD" -- <Member name="UK" Currency="GBP" -- <Member name="Switzerland" Currency="CHF" -- <Member name="Germany" Currency="EUR" -- (11 Replies)
Discussion started by: dev.devil.1983
11 Replies

3. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

4. Shell Programming and Scripting

Combining lines in to one line

Hi Friends, I have a file1.txt 1001 jkilo yrhfm 200056 jhdf rjhwjkrh 3+u8jk5h3 uru ehjk 1002 jkfhk hfjkd 2748395 fdjksfh hefjkh 3hdfk ejkh kjhjke In the above if you see the firt charcter of each line mentioned in red has a pattern . I need to create another file where , the... (6 Replies)
Discussion started by: i150371485
6 Replies

5. Shell Programming and Scripting

Combining lines between two specific lines

Hi, I have a requirement like following: I have input file like: Question: 1 ----Multiple choice--- What is your favourite colour? Options: a) red b) blue c) none of these Question: 2 ---Multiple choice----- In which month did you join your first job? Options: a) Jan b) Feb c)... (11 Replies)
Discussion started by: ppatra
11 Replies

6. Shell Programming and Scripting

Combining two text lines into one

Here is the sample file: 646 STARTED Tue Dec 30 06:38:53 2008 Job DdCustXAddr_brad has been reset. 647 STARTED Tue Dec 30 06:38:54 2008 Starting Job DdCustXAddr_brad. (...) 704 STARTED Tue Dec 30 06:49:02 2008 Job DdCustXAddr_brad aborted. I want to combine every non-numbered line... (4 Replies)
Discussion started by: mfavero
4 Replies

7. Shell Programming and Scripting

combining lines in files

hey, I want to combine every three lines in a file onto one i.e old file: 82 67 32 62 58 39 29 47 58 27 34 50 27 35 69 38 58 70 new file: 82 67 32 62 58 39 29 47 58 27 34 50 27 25 69 38 58 70 At the moment I am using the following code: gawk 'BEGIN {x=0} { if(x<3)... (14 Replies)
Discussion started by: sme
14 Replies

8. Shell Programming and Scripting

help combining lines in awk

I seem to have gotten myself in over my head on this one. I need help combining lines together. I have a text file containing 24,000 lines (exactly why I need awk) due to bad formatting it has separated the lines (ideally it should be 12,000 lines total). Example of file: ... (2 Replies)
Discussion started by: blueheed
2 Replies

9. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question