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 12:40 PM
Merging lines - Tuning the script senthil_is Shell Programming and Scripting 3 03-05-2008 03:24 AM
Merging files with AWK filtering and counting lines Meert Shell Programming and Scripting 2 01-28-2008 11:58 AM
merging two file in an ordered way raku05 Shell Programming and Scripting 2 09-22-2005 08:57 AM
Merging lines into one Foxgard UNIX for Dummies Questions & Answers 8 06-19-2005 07:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-11-2007
thaduka thaduka is offline
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
  #2 (permalink)  
Old 07-11-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Here is one way.

Code:
while read line
do
  OUT=${line}
  read line
  OUT=${OUT};${line}
  echo ${OUT} >> output.txt
done <input.txt

  #3 (permalink)  
Old 07-11-2007
hemangjani hemangjani is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 73
Smile

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

Join Date: Jun 2007
Location: Pennsylvania
Posts: 46
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?
  #5 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

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

  #6 (permalink)  
Old 07-11-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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}
  #7 (permalink)  
Old 07-11-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

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}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0