The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Multiple search string in multiple files using awk pinnacle Shell Programming and Scripting 0 05-05-2009 01:08 PM
PERL: add string to multiple lines bonny Shell Programming and Scripting 4 05-19-2008 12:11 PM
last occurrence of a string across multiple files porphyrin UNIX for Dummies Questions & Answers 2 12-24-2007 09:39 AM
String replacement in multiple files WABonnett Shell Programming and Scripting 2 02-17-2004 03:49 PM
Editing one string in multiple files Skoshi UNIX for Dummies Questions & Answers 1 11-10-2001 02:13 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 05-08-2009
whchee whchee is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Unhappy Combine multiple string into 1 string group by certain criteria

Hi all,

I am newbie in unix. Just have some doubts on how to join multiple lines into single line.

I have 1 file with following contents.

R96087641
HostName-kul480My
This is no use any more
%%
E78343970
LocalPath-/app/usr/SG
(Blank in this line)
%%
E73615740
LocalPath-/app/usr/US
Urgent Needed
%%
.....
.....
.....
.....

Each grouping is separated by %%. There are basically 2 kinds of grouping which is HostName and LocalPath. I need to format the content into 2 separate files differentiate by HostName and LocalPath. In the files, the keyword HostName and LocalPath must be removed. Below is the sample of formatted data.

(HostName.txt)
R96087641|kul480My|This is no use any more
.....
.....

(LocalPath.txt)
E78343970|/app/usr/SG|
E73615740|/app/usr/US|Urgent Needed
.....
.....

I have really no idea on how to sort it out. My knowledge on awk also not strong enough to make it.

Hopes someone could help to solve my problem.
Thanks.

Last edited by whchee; 05-08-2009 at 12:28 PM..
  #2 (permalink)  
Old 05-08-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
nawk -f wh.awk myFile

wh.awk:
Code:
BEGIN {
  FS="-"
  OFS="|"
  FILEhost="HostName.txt"
  FILElocal="LocalPath.txt"
}
/^%%/ { for(i=1;i<=cnt;i++) printf("%s%c", a[i], (i==cnt)?ORS:OFS) > file; cnt=0;next}
{ cnt++ }
/^HostName/ { file=FILEhost; a[cnt]=$2;next}
/^LocalPath/ { file=FILElocal; a[cnt]=$2;next}
{a[cnt]=$0}
  #3 (permalink)  
Old 05-09-2009
whchee whchee is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Wink

Hi Vgersh,

The logic seems working by reading it, neat and understandable. However, i can only test it on Monday. Will let you know the result then.

Btw, if i keen on using awk only, will there be lot diff with nawk?

I am not so sure is nawk working in HP-UX environment not...
Kindly advice.
  #4 (permalink)  
Old 05-11-2009
whchee whchee is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Thumbs up

Hi Vgersh,

Thanks. It works well
  #5 (permalink)  
Old 05-11-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
Code:
$/="%%\n";
open FH1,"+>HostName.txt";
open FH2,"+>LocalPath.txt";
while(<DATA>){
	my @tmp=split("\n",$_);
	my @t1=split("-",$tmp[1]);
	$tmp[1]=~s/.*-//;
	my $str=join "|",@tmp[0..2];
	if($t1[0] eq "HostName"){
		print FH1 $str."\n";
	}
	else{
		print FH2 $str."\n";
	}
}

__END__
R96087641
HostName-kul480My
This is no use any more
%%
E78343970
LocalPath-/app/usr/SG
(Blank in this line)
%%
E73615740
LocalPath-/app/usr/US
Urgent Needed
%%
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 02:31 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