- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script to concatenate columns from diffe...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2009 05:41 PM
03-01-2009 05:41 PM
I have five files and each file has one column of data defined in it and all files have the same number of rows. I need to create a new file and add all five columns in such a way that they appear side by side in five columns. Can someone help me create a POSIX shell script to perform this.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2009 06:33 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2009 07:11 PM
03-01-2009 07:11 PM
Re: Shell script to concatenate columns from different files into one
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2009 07:52 PM
03-01-2009 07:52 PM
Re: Shell script to concatenate columns from different files into one
#!/usr/bin/ksh
#populate an array for file 1
count=1
cat file1 | while read LINE
do
FILE1[$count]=$LINE
increment count
done
#repeat for each file
then loop through each position in the arrays and cat in to a new file:
count=1
echo FILE1[$count] FILE2[$count] FILE3[$count] .... >> NEWFILE
increment count
etc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2009 08:25 PM
03-01-2009 08:25 PM
Re: Shell script to concatenate columns from different files into one
Why not use the 'paste' command?
Hein.
NAME
paste - merge same lines of several files or subsequent lines of one file
SYNOPSIS
paste file1 file2 ...
paste -d list file1 file2 ...
paste -s [-d list ] file1 file2 ...
DESCRIPTION
In the first two forms, paste concatenates corresponding lines of the given input files file1, file2, etc. It treats each file as a column or columns in a table and pastes them together horizontally (parallel merging).
- Tags:
- paste
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2009 09:00 PM
03-01-2009 09:00 PM
Re: Shell script to concatenate columns from different files into one
I would use a PERL script for that.
For example:
------------------- my_paste.pl --------
use strict;
use warnings;
my ($done,@fh);
while (my $file = shift) { # Open all files
open $fh[@fh],"<$file" or die "Could not open file $file\n$!";
}
while (1) { # forever ?
my $line; # clean line every iteration
for (@fh) { # each file handle
$line .= <$_> or $done=1; # append chunck with newlines
}
last if $done; # done ?
$line =~ s/\n\b/\t/g; # replace newline followed by word with tab
print $line;
}
-------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2009 01:13 AM
03-03-2009 01:13 AM
Re: Shell script to concatenate columns from different files into one
paste file1 file2 file3 file4 file5 >newfile
This should work for you.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2009 04:26 AM
03-03-2009 04:26 AM
Re: Shell script to concatenate columns from different files into one
If I am right in understanding your requirement, I think "paste" should work with the help of "awk".
If your column seperator is "space", and you want 2nd column of file1 and 1st column of file2. The following will do,
#paste -d " " file1 file2 |awk '{print $2, $3}'
Try "paste -d " " file1 file2, then you understand which filed you need to filter.
Good luck
Shahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2009 06:43 AM
03-03-2009 06:43 AM
Re: Shell script to concatenate columns from different files into one
exec 3
while read -u 3 f1
do
read -u 4 f2
read -u 5 f3
read -u 6 f4
read -u 7 f5
printf "%-15s %-15s %-15s %-15s %-15s\n" $f1 $f2 $f3 $f4 $f4
done
read -u 4 f2 && read -u 5 f3 && read -u 6 f4 && read -u 7 f5 && echo we still have some lines in the other files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2009 08:38 AM
03-03-2009 08:38 AM
Re: Shell script to concatenate columns from different files into one
Hein had the simplest answer, use "paste", as thats precisely what its made for....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2009 07:54 AM
03-26-2009 07:54 AM