Visit Our UNIX and Linux User Community


Read two lines at time from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read two lines at time from a file
# 1  
Old 09-07-2012
Read two lines at time from a file

Hello community,
what I need to do is read 2 rows at time from a file. I have this simple solution:

File to read:
Code:


LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
LINE7
LINE8

Read routine:
Code:
#!/bin/ksh
sed '1,3d' /out.txt | while read line; do
  read line2
  echo $line $line2
done

Result:
Code:
LINE1 LINE2
LINE3 LINE4
LINE5 LINE6
LINE7 LINE8

That's ok for me, but, just to be sure, do you have any other advice? Or any other faster methods to do that?

Thanks
Lucas
# 2  
Old 09-07-2012
Code:
paste -sd' \n' file

Hey, sorry for not paying attention to those blank lines.
# 3  
Old 09-07-2012
Hi


Code:
$ sed '1,3d;N;s/\n/ /' file
LINE1 LINE2
LINE3 LINE4
LINE5 LINE6
LINE7 LINE8

Guru.
# 4  
Old 09-07-2012
Wait, wait, wait! Smilie
I forgot to mention that variables $line and $line2 should be used for create other output file inside the loop.

Something like:
Code:
#!/bin/ksh
sed '1,3d' /out.txt | while read line; do
  read line2
  echo "ABC ; DEF ; $line ; GHI ; $line2 ; ZZZ" >> finaloutput.txt
  echo "XXX ; YYY ; $line ; UUU ; $line2 ; PPP" >> finaloutput2.txt
done

# 5  
Old 09-07-2012
Though this looks a bit intimidating, it might be faster than the loop:
Code:
sed -n '${s/$/\
/;b proc
}
/^[ \t]*$/d;N
:proc
h;
s/\n/ ; GHI ; /
s/^/ABC ; DEF ; /
s/$/ ; ZZZ/
w finaloutput.txt
g
s/\n/ ; UUU ; /
s/^/XXX ; YYY ; /
s/$/ ; PPP/
w finaloutput2.txt' /out.txt


Last edited by elixir_sinari; 09-07-2012 at 07:24 AM..
# 6  
Old 09-07-2012
Code:
awk 'NF>0{x=$0;getline y;print x FS y}' filename


Previous Thread | Next Thread
Test Your Knowledge in Computers #651
Difficulty: Medium
Amanda the Panda is the mascot of MOTIF.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read multiple lines at a time from file

Hello All, I have a file like below.... dn: cn=user1,ou=org,o=org cn=user1 uid=user1 cn=user2,ou=org,o=org cn=user2 uid=user2 cn=user3,ou=org,o=org cn=user3 cn=user33 uid=user3 cn=user4,ou=org,o=org cn=user4 uid=user4 (6 Replies)
Discussion started by: s_linux
6 Replies

2. Shell Programming and Scripting

Read few last lines of a file

Hi, I have a txt file in below format - START 1 2 3 4 END START 5 6 7 8 END START 9 10 END (8 Replies)
Discussion started by: bhupinder08
8 Replies

3. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

4. Shell Programming and Scripting

File read time

hi, how to check the file latest read time.. (2 Replies)
Discussion started by: rsivasan
2 Replies

5. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies

6. Shell Programming and Scripting

How to read/process a .gz file, one line at a time?

Hello I'm stuck trying to solve this KSH issue and I'm hoping someone out there can offer some suggestions. I want to read lots of large .gz files one line at a time in order to compare its Error entries with a list of known errors. I can't simply do "foreach ERROR do gzcat *.gz |grep... (2 Replies)
Discussion started by: dayscripter
2 Replies

7. Shell Programming and Scripting

How to read max of 10 file at a time?

Hi All, Please advise . Welcome more suggestions. For examples, I have 1000 file with prefix x??? In fact, I want to convert them to x???.txt with max 10 files at a time. As such, I will need to call another script to read from those 10 *txt files and sleep 5000 to convert the next 10 again.... (10 Replies)
Discussion started by: cedrichiu
10 Replies

8. Shell Programming and Scripting

read some lines from file!!!

any idea please!!! I want to pick up all lines of "state" and "desc" from x files: ... # state blah blah blah blah ... .. # desc blah blah blah .... Thx Andy (7 Replies)
Discussion started by: andy2000
7 Replies

9. UNIX for Dummies Questions & Answers

Read lines from file

i have a problem on my bourne shell script. I want to read line by line and then stop when the line does not have letters or is an empty string. But i encounter an error at "while ". The error nessage is "test.sh: test: unknown operator line". Can anyone help me on this thanks :) (2 Replies)
Discussion started by: sagolo
2 Replies

10. Shell Programming and Scripting

why shell scripting takes more time to read a file

i have done a coding in shell scripting which reads a file line by line and does something....it takes more than 30 seconds to execute for a single file. when i do the same with perl scripting it takes less than a second. is that shell scripting is not efficient while working with large number of... (1 Reply)
Discussion started by: brkavi_in
1 Replies

Featured Tech Videos