Combine multiple string into 1 string group by certain criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine multiple string into 1 string group by certain criteria
# 1  
Old 05-08-2009
Data 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  
Old 05-08-2009
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  
Old 05-09-2009
Hammer & Screwdriver

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  
Old 05-11-2009
MySQL

Hi Vgersh,

Thanks. It works well Smilie
# 5  
Old 05-11-2009
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
%%

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove multiple lines from a particular string to particular string

Hi, I have a file containing the DDLs of tables in a schema. From that I need to remove all the lines from a starting string till a specific string. Here is an example. File1.txt ------------- CREATE TABLE "SCHEMA1"."LKP11_TBL_USERS" ( "ID" NUMBER(8,0) NOT NULL ENABLE, "USER_ID"... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

2. Shell Programming and Scripting

Group by and string concatenation

Hi, I was trying to work on a file which had the following data format 1 hi 1 this 1 is 1 john 2 hello 3 test 3 case the expected output file is the below 1 hi, this, is, john 2 hello 3 test, case I tried using awk or while read, but I couldnt... (13 Replies)
Discussion started by: karthikbhuvana
13 Replies

3. Shell Programming and Scripting

Find and replace string matching criteria

Dear Friends, I am looking for a way to replace a string (multiple lines) starting with something and ending with something (these two values do not change) with blank. Basically I want to delete this code injection accross many sites and folders. Search Code (across files and folders) that... (2 Replies)
Discussion started by: libras
2 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

7. UNIX for Dummies Questions & Answers

Combine multiple files with common string into one new file.

I need to compile a large amount of data with a common string from individual text files throughout many directories. An example data file is below. I want to search for the following string, "cc_sectors_1" and combine all the data from each file which contains this string, into one new... (2 Replies)
Discussion started by: GradStudent2010
2 Replies

8. Shell Programming and Scripting

How to combine two string into one?

Hi, I would like to ask whether is it possible to do the following: let say i have 2 files and the content of the file are: first file: a = b:c:d second file: a = c:d:e can i read this 2 files and create a third file that will have this result? third file: a=b:c:d:e is this possible by... (8 Replies)
Discussion started by: llobak
8 Replies

9. Shell Programming and Scripting

Need to find a string, check the next line, and if it matches certain criteria, replace it with a s

Hey Fellas. I am new to scripting. I have searched through the forums and found a lot of good info, but I can't seem to get any of it to work together. I am trying to find a particular sting in a file, and if the next string matches certain criteria, replace it with a string from a csv... (6 Replies)
Discussion started by: midniteslice
6 Replies

10. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies
Login or Register to Ask a Question