The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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 lines in a file thaduka UNIX for Dummies Questions & Answers 6 07-11-2007 10:27 AM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 02:21 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 03-04-2008
senthil_is senthil_is is offline
Registered User
  
 

Join Date: Sep 2007
Location: singapore
Posts: 35
Merging lines using AWK

Hi,

Anybody help on this.

I want to merge the line with previous line, if the line starts with 7.
Otherwise No change in the line.

Example file aa.txt is like below

122122
222222
333333
734834
702923
389898
790909
712345
999999

My output should be written in another file called bb.txt

122122
222222
333333734834702923
389898790909712345
999999

Your help is very much appriciated.

Last edited by senthil_is; 03-04-2008 at 11:19 PM.. Reason: change
  #2 (permalink)  
Old 03-05-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
Code:
awk 'BEGIN{t=""}
{
if ($1 ~ /^7/)
	t=sprintf("%s%s",t,$1)
else
	t=sprintf("%s\n%s",t,$1)
}
END{
print t
}' filename
  #3 (permalink)  
Old 03-05-2008
senthil_is senthil_is is offline
Registered User
  
 

Join Date: Sep 2007
Location: singapore
Posts: 35
Thanks summer.

The output is coming with one blank line in the header.

# Here blank line is coming.
122122
222222
333333734834702923
389898790909712345
999999

How to avoid and how to redirect to another file.
  #4 (permalink)  
Old 03-05-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,313
One way:

Code:
awk '
NR>1 && substr($0,1,1)!=7{printf("\n")}
{printf("%s",$0)}
END{if(substr($0,1,1)!=7){printf("\n")}}
' file
Regards
  #5 (permalink)  
Old 03-05-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,881
Another:

Code:
awk 'END{print RS}$0=(NR>1&&!/^7/)?RS$0:$0' ORS= file>new
Use nawk or /usr/xpg4/bin/awk on Solaris.
  #6 (permalink)  
Old 03-05-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,763
Did you want an extra blank line at the end of the file? If not
Code:
 awk '$0=(NR>1&&!/^7/)?RS$0:$0' ORS= file
  #7 (permalink)  
Old 03-05-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by senthil_is View Post
Hi,

Anybody help on this.

I want to merge the line with previous line, if the line starts with 7.
Otherwise No change in the line.

Example file aa.txt is like below

122122
222222
333333
734834
702923
389898
790909
712345
999999

My output should be written in another file called bb.txt

122122
222222
333333734834702923
389898790909712345
999999

Your help is very much appriciated.
Code:
[n]awk '{
      if ($0 ~ /^7/)
         l = l" "$0
      else {
         if (l) print l
         l = $0
      }
} END {if (l) print l}' aa.txt > bb.txt
Closed Thread

Bookmarks

Tags
solaris

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 07:50 AM.


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