- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: need a script to assign
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
Discussions
Discussions
Discussions
Forums
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
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
тАО02-01-2007 03:44 AM
тАО02-01-2007 03:44 AM
ABC
DCF
GRF
File2 has:
4
7
8
Want output should look like:
ABC 4
DCF 7
GRF 8
thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 03:53 AM
- Tags:
- paste
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 04:24 AM
тАО02-01-2007 04:24 AM
Re: need a script to assign
note, that the default delimiter of 'paste' is TAB character - use
paste -d' ' file1 file2 ...
to get 'real' blank.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 04:25 AM
тАО02-01-2007 04:25 AM
Re: need a script to assign
like:
command -H
so that it will take 1st variable from 1st file and 1st variable in 2nd file to execute command.
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 05:17 AM
тАО02-01-2007 05:17 AM
Re: need a script to assign
#!/usr/bin/sh
paste file1 file2 | while read A B
do
command -H ${A} -I ${B}
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 05:25 AM
тАО02-01-2007 05:25 AM
Re: need a script to assign
>like:
>command -H
>so that it will take 1st variable from 1st file and 1st variable in 2nd file to >execute command.
Try the shell script below which uses the coprocess functionality of sh-posix(1):
==============================================================
#!/usr/bin/sh
cat file1 |&
exec 3>&p; exec 4<&p
cat file2 |&
exec 5>&p; exec 6<&p
while true
do
read -u4 file1var1 && read -u6 file2var1
if [[ -z $file1var1 || -z $file2var1 ]]; then
break
else
echo "command -H $file1var1 -I $file2var1"
fi
done
- Tags:
- coprocess
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 05:30 AM
тАО02-01-2007 05:30 AM
Re: need a script to assign
a general approach for this type of problem is to handle with arrays:
In ksh syntax:
set -A A 'aa bb cc'
set -A B '11 22 33'
typeset -i i=-1
while [ i+=1 -lt ${#A[*]} ]
do print $i: ${A[i]} ${B[i]}
done
will produce
0: aa 11
1: bb 22
2: cc 33
mfG Peter
- Tags:
- ARRAY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2007 01:47 PM
тАО02-01-2007 01:47 PM
Re: need a script to assign
Here is a one-liner with awk.
It uses the main loop to read file 1 into $0.
Then for every loop it reads a line from file2 into a variable to be passde with $0 to the command (here: echo)
#awk '{getline f2 < "file2"; system ("echo " $0 " " f2)}' file1
abc 4
def 5
ghi 6
Here is an array style solution with perl.
It sucks up each file into an array, chops of teh newlines, calls teh comamdn for each entry in the first array, passing both:
perl -e 'open F,shift;@f1=
abc 4
def 5
ghi 6
That's of course less ugly with a help file:
perl x.pl file1 file2
abc 4
def 5
ghi 6
------------- x.pl ----------
open F,shift;
@f1=
open F,shift;
@f2=
chomp @f1;
chomp @f2;
for $i (0..@f1){
system("echo $f1[$i] $f2[$i]")
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2007 12:58 AM
тАО02-02-2007 12:58 AM
Re: need a script to assign
Sandman,
Would you please explain it. I liked this way.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2007 05:36 PM
тАО02-02-2007 05:36 PM
Re: need a script to assign
Hmm, I've always thought that |& was useless junk.
If you look at ksh(1), you see |& under Commands.
<&p and >&p under Input/Output. And later it talks about redirecting them because you want another coprocess.
Then under read, you can read -u#.
So this does:
cat file1 |&
exec 3>&p; exec 4<&p
Puts cat in the background and the exec changes the output to cat to 3 and input from cat to 4. (3 seems pretty useless since cat is only reading from file1.)
exec with no executable just does file redirection/closing.
cat file2 |&
exec 5>&p; exec 6<&p
Again the same for 5 & 6. Note 4 and 6 are the output FROM the two cats.
read -u4 file1var1 && read -u6 file2var1
This reads one line from file 4 (from cat file1) and one line from file 6 (from cat file2).
if [[ -z $file1var1 || -z $file2var1 ]]; then
Checks for mismatched number of records and probably could print an error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2007 07:05 PM
тАО02-02-2007 07:05 PM
Re: need a script to assign
You can do simply
# paste 1 2 > result_file
And the result file will be result_file ,
Cheers,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 04:42 AM
тАО02-07-2007 04:42 AM