Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Maybe a cleaner way to generate a file?

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-13-2017   -   Original Discussion by crimso
crimso crimso is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 25 July 2017, 11:29 AM EDT
Location: michigan
Posts: 66
Thanks: 25
Thanked 4 Times in 4 Posts
Maybe a cleaner way to generate a file?

greetings,
to be clear, i have a solution but i'm wondering if anyone has a cleaner way to accomplish the following:

the variable:

Code:
LSB_MCPU_HOSTS='t70c7n120 16 t70c7n121 16 t70c7n122 16 t70c7n123 16 t70c7n124 16 t70c7n125 16 t70c7n126 16 t70c7n127 16 t70c7n128 16 t70c7n129 16 t70c7n130 16 t70c7n131 16 t70c7n132 16 t70c7n117 16 t70c7n118 16 t70c7n119 16 '

the command i used to generate the required output:

Code:
printf "%s %s\n" `echo $LSB_MCPU_HOSTS | awk '$2="15"'` | awk '{print $2, $1}' | sed "1 acp `echo ${LSB_MCPU_HOSTS} | awk '{print $1}'`"

the required output:

Code:
15 t70c7n120
cp t70c7n120
16 t70c7n121
16 t70c7n122
16 t70c7n123
16 t70c7n124
16 t70c7n125
16 t70c7n126
16 t70c7n127
16 t70c7n128
16 t70c7n129
16 t70c7n130
16 t70c7n131
16 t70c7n132
16 t70c7n117
16 t70c7n118
16 t70c7n119

thanks in advance.
Sponsored Links
    #2  
Old Unix and Linux 07-13-2017   -   Original Discussion by crimso
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Here's my attempt.
I would avoid all those pipelines and try to do all transformations in awk or sed or Perl etc.


Code:
$
$ echo $LSB_MCPU_HOSTS
t70c7n120 16 t70c7n121 16 t70c7n122 16 t70c7n123 16 t70c7n124 16 t70c7n125 16 t70c7n126 16 t70c7n127 16 t70c7n128 16 t70c7n129 16 t70c7n130 16 t70c7n131 16 t70c7n132 16 t70c7n117 16 t70c7n118 16 t70c7n119 16
$
$ echo $LSB_MCPU_HOSTS | awk '{ $2 = "15";
                                for(i=1; i<=NF; i+=2) {
                                    n = i+1;
                                    print $n, $i;
                                    if (i == 1){print "cp", $1}
                                }
                              }'
15 t70c7n120
cp t70c7n120
16 t70c7n121
16 t70c7n122
16 t70c7n123
16 t70c7n124
16 t70c7n125
16 t70c7n126
16 t70c7n127
16 t70c7n128
16 t70c7n129
16 t70c7n130
16 t70c7n131
16 t70c7n132
16 t70c7n117
16 t70c7n118
16 t70c7n119
$
$


Last edited by durden_tyler; 07-13-2017 at 12:13 PM..
The Following User Says Thank You to durden_tyler For This Useful Post:
crimso (07-14-2017)
Sponsored Links
    #3  
Old Unix and Linux 07-13-2017   -   Original Discussion by crimso
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 November 2017, 12:12 PM EST
Location: Aachen, Germany
Posts: 11,622
Thanks: 320
Thanked 3,615 Times in 3,320 Posts
Try also

Code:
export LSB_MCPU_HOSTS 
awk 'BEGIN {n = split (ENVIRON["LSB_MCPU_HOSTS"], TMP); TMP[0] = "cp"; print "15", TMP[1]; for (i=0; i<n; i+=2) print TMP[i], TMP[i+1] }'
15 t70c7n120
cp t70c7n120
16 t70c7n121
16 t70c7n122
16 t70c7n123
16 t70c7n124
16 t70c7n125
16 t70c7n126
16 t70c7n127
16 t70c7n128
16 t70c7n129
16 t70c7n130
16 t70c7n131
16 t70c7n132
16 t70c7n117
16 t70c7n118
16 t70c7n119

EDIT: Or.
Code:
TMP=( cp $LSB_MCPU_HOSTS ); unset TMP[-1]
printf "15 %s\n" ${TMP[1]} ; printf "%s %s\n" ${TMP[@]}
15 t70c7n120
cp t70c7n120
.
.
.


Last edited by RudiC; 07-13-2017 at 07:06 PM..
The Following User Says Thank You to RudiC For This Useful Post:
crimso (07-14-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
A cleaner way to rearrange column LMHmedchem Shell Programming and Scripting 8 11-30-2016 06:36 AM
Cleaner way to use shell variable in awk /X/,/Y/ syntax? hanson44 Shell Programming and Scripting 2 04-16-2013 08:01 PM
Cleaner method for this if-then statement? lupin..the..3rd Shell Programming and Scripting 2 05-25-2012 03:04 PM
Grabbing the newest file, cleaner method? Karunamon Shell Programming and Scripting 4 12-13-2011 07:30 PM



All times are GMT -4. The time now is 03:32 PM.