How to combine two string into one?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to combine two string into one?
# 1  
Old 02-19-2010
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 using C shell?
Can anyone guide me please? Thanks

Regards,
SL
# 2  
Old 02-19-2010
If Perl is acceptable:

Code:
perl -F'\s*=\s*' '-lane
  push @{$_{$F[0]}}, split ":", $F[1];
  END {
    print $_, " = ", join ":", grep !$_{$_}++, @{$_{$_}} for keys %_;
  }' file[12]

Example:
Code:
% head file[12]
==> file1 <==
a = b:c:d

==> file2 <==
a = c:d:e
% perl -F'\s*=\s*' '-lane
  push @{$_{$F[0]}}, split ":", $F[1];
  END {
    print $_, " = ", join ":", grep !$_{$_}++, @{$_{$_}} for keys %_;
  }' file[12]
a = b:c:d:e

With awk (if the order of the values is not important):

Code:
awk -F' *= *' 'END {
  printf "%s = ", f
  for (k in s) printf "%s", k \
    (j++ < n ? ":" : RS)
  }
{
  n = split($2, t, ":"); f = $1
  for (i = 0; ++i <= n;) s[t[i]]
  n > m && m = n
  }' file[12]



---------- Post updated at 11:19 AM ---------- Previous update was at 10:55 AM ----------

Or:

Code:
perl -F'\s*=\s*' '-lane
  $f ||= $F[0]; @f{split ":", $F[1]} = (1) x length %f;
  END { print $f, " = ", join ":", sort keys %f }
  ' file[12]


Last edited by radoulov; 02-19-2010 at 06:03 AM..
# 3  
Old 02-19-2010
Another approach:

Code:
awk -F"[ =:]" 'NR==FNR{a[$1]=$NF;next}a[$1]{print $0 ":"a[$1]}' file2 file1


Top Ten Reasons not to use the C shell:

http://www.grymoire.com/Unix/CshTop10.txt
# 4  
Old 02-19-2010
file[12] are your filenames: file1 and file2, most shells will expand the names automatically, assuming the pattern matches the real filenames.
If you really need to run the code from csh, you should create a script file with the following content:

merge.awk

Code:
BEGIN { FS = " *= *" }
END {
  printf "%s = ", f
  for (k in s) printf "%s", k \
    (j++ < n ? ":" : RS)
  }
{
  n = split($2, t, ":"); f = $1
  for (i = 0; ++i <= n;) s[t[i]]
  n > m && m = n
  }

The script should be executed like this:

Code:
awk -f merge.awk file1 file2

# 5  
Old 02-19-2010
Quote:
Originally Posted by radoulov
file[12] are your filenames: file1 and file2, most shells will expand the names automatically, assuming the pattern matches the real filenames.
If you really need to run the code from csh, you should create a script file with the following content:

merge.awk

Code:
BEGIN { FS = " *= *" }
END {
  printf "%s = ", f
  for (k in s) printf "%s", k \
    (j++ < n ? ":" : RS)
  }
{
  n = split($2, t, ":"); f = $1
  for (i = 0; ++i <= n;) s[t[i]]
  n > m && m = n
  }

The script should be executed like this:

Code:
awk -f merge.awk file1 file2

Radoulov,

With your script I get as output:
Code:
a = d:e:b:c

# 6  
Old 02-19-2010
Quote:
Originally Posted by Franklin52
Radoulov,

With your script I get as output:
Code:
a = d:e:b:c

Yes,
as I said, use this script if the order of the values is not important.
I thought the OP wanted the unique values of the : separated strings after the equal sign.
I'm not sure I've got it right Smilie
# 7  
Old 02-19-2010
Quote:
Originally Posted by radoulov
Yes,
as I said, use this script if the order of the values is not important.
I thought the OP wanted the unique values of the : separated strings after the equal sign.
I'm not sure I've got it right Smilie
Ahh, I see, I think the OP should clarify that...Smilie

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine files

I have n of files with ending with _ZERO.txt need to combine all file ending with _ZERO.txt into 1 file ex: A_ZERO.txt 1 2 B_ZERO.txt 3 4 Output: FINAL.txt 1 2 (3 Replies)
Discussion started by: satish1222
3 Replies

2. 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

3. Shell Programming and Scripting

Combine 2 lines

All, i am new to linux script... source Filter: vlan281-BUM-5M BUM-5M 0 0 Filter: vlan282-BUM-5M BUM-5M 0 0 Filter: vlan2828-BUM-5M Filter:... (2 Replies)
Discussion started by: samoptimus
2 Replies

4. 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

5. UNIX for Dummies Questions & Answers

combine -A and -v in grep

I have a data file that looks like this: infile: A 13 Z 23 F 22 Z 413 R 16how do I combine the -A option with the -v option to get the lines that don't match 'Z' and their next lines? the outfile should be: A 13 F 22 R 16I tried: (1 Reply)
Discussion started by: jdhahbi
1 Replies

6. Shell Programming and Scripting

combine

Dear all i am having text file like xxx|yyy|1|2| zzz|rrr|3|4| www|xxx|>< 5|6|>< jjj|kkk|>< 8|9>< i want to join two lines which are having ' >< ' by taking only two lines at a stretch ...using awk command the result output should be xxx|yyy|1|2| zzz|rrr|3|4| www|xxx|5|6|... (2 Replies)
Discussion started by: suryanarayana
2 Replies

7. Shell Programming and Scripting

perl combine if

Hi Evereyone, %q = (); $q{"a"} = 0; $q{"b"} = 0; $q{"c"} = 0; if ($q{"a"} !=0 || $q{"b"} !=0 || $q{"c"} !=0) { print "non-zero" } if any simple way to do that? assume you have not only a, b, c inside %q, but a, b, c, d, e, ... ... Thanks (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. Shell Programming and Scripting

combine

Hi I am having text file like this 001|ramu|hno221|>< sheshadripuram|delhi|560061>< 002|krishna|hno225|>< newdelhimain|delhi|560061>< i want to combine every two lines as single...line... i.e 001|ramu|hno221|sheshadripuram|delhi|560061 can u pls help me (3 Replies)
Discussion started by: suryanarayana
3 Replies

9. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: whchee
4 Replies

10. Shell Programming and Scripting

Combine files with same name

I need a script that combines files with the same name. These files are on a windows directory but the PC has Cygwin so i have a limited unix command set. What I've got; WebData_9_2007-09-20.txt WebData_9_2007-09-20.txt WebData_9_2007-09-21.txt WebData_9_2007-09-20.txt... (4 Replies)
Discussion started by: jmwhitford
4 Replies
Login or Register to Ask a Question