The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merging lines using AWK senthil_is Shell Programming and Scripting 6 03-05-2008 08:40 AM
Merging lines - Tuning the script senthil_is Shell Programming and Scripting 3 03-04-2008 11:24 PM
Merging files with AWK filtering and counting lines Meert Shell Programming and Scripting 2 01-28-2008 07:58 AM
merging two file in an ordered way raku05 Shell Programming and Scripting 2 09-22-2005 04:57 AM
Merging lines into one Foxgard UNIX for Dummies Questions & Answers 8 06-19-2005 03:36 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2007
Registered User
 

Join Date: Aug 2005
Posts: 28
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 rece=3

thanks
Kumar
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-11-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
Here is one way.
Code:
while read line
do
  OUT=${line}
  read line
  OUT=${OUT};${line}
  echo ${OUT} >> output.txt
done <input.txt
Reply With Quote
  #3 (permalink)  
Old 07-11-2007
Registered User
 

Join Date: Feb 2006
Posts: 65
Smile

Code:
awk 'ORS=NR%2?";":"\n"' sample.txt
Reply With Quote
  #4 (permalink)  
Old 07-11-2007
Registered User
 

Join Date: Jun 2007
Location: Pennsylvania
Posts: 39
Quote:
Originally Posted by hemangjani View Post
Code:
awk 'ORS=NR%2?";":"\n"' sample.txt
Could explain the awk line.

I'm so confused on awk and sed. Is there a good tutorial that explains what all that gibberish means?
Reply With Quote
  #5 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Vino,
The line:
Code:
OUT=${OUT};${line}
Should be replaced by:
Code:
OUT=${OUT}${line}
Hemangjani,
If the input file has an odd number of records, your output does not produce
a line break for the last record.

Here is another way:
Code:
typeset -i mLineCnt=0
mOutLine=''
while read mLine
do
  mLineCnt=${mLineCnt}+1
  mOutLine=${mOutLine}${mLine}
  if [ ${mLineCnt} -eq 2 ]; then
    echo ${mOutLine}
    mOutLine=''
    mLineCnt=0
  fi
done < input_file
if [ ${mLineCnt} -ne 0 ]; then
  echo ${mOutLine}
fi
Reply With Quote
  #6 (permalink)  
Old 07-11-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
Quote:
Originally Posted by Shell_Life View Post
Vino,
The line:
Code:
OUT=${OUT};${line}
Should be replaced by:
Code:
OUT=${OUT}${line}
And where do I introduce the ; as shown in the expected output

country=1;send apps =1 rece=2
country=2;send apps =3 rece=3

I should have double quoted the RHS of OUT=${OUT};${line}
Reply With Quote
  #7 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
You are correct, Vino, it should be single or double quoted:
Code:
OUT=${OUT}';'${line}
Since the requirement asked for a semi-colon, mine should have it:
Code:
mOutLine=${mOutLine}';'${mLine}
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0