Repeating a column but changing one parameter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Repeating a column but changing one parameter
# 1  
Old 06-13-2010
Repeating a column but changing one parameter

Does anyone know how I can create a file like this:
Code:
blue  0
red    0
yellow  0
green  0
orange  0
blue  2
 red  2 
 yellow  2
 green  2
 orange  2
blue  4
 red  4
 yellow  4
 green  4
 orange  4
blue  6
 red  6
 yellow  6
 green  6
 orange  6

That is repeating the first column but increasing the number in column 2 each time?
# 2  
Old 06-13-2010
Code:
awk '{a[NR]=$1}END{for(i=0;i<=6;i+=2){for(j=1;j<=NR;j++){print a[j],i}}}' infile

where infile contains:
Code:
blue  0
red    0
yellow  0
green  0
orange  0

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-14-2010
Code:
# for i in 0 2 4 6; do sed "s/$/$i/" infile; done
blue  0
red 0
yellow 0
green 0
orange0
blue  2
red 2
yellow 2
green 2
orange2
blue  4
red 4
yellow 4
green 4
orange4
blue  6
red 6
yellow 6
green 6
orange6

Code:
# cat infile
blue
red
yellow
green
orange

# 4  
Old 06-14-2010
Try this, n is the number of sets:
Code:
awk -v n=10 'BEGIN {
  split("blue red yellow green orange", a)
  c=0
  for(i=1;i<=n;i++) {
    for(j=1;j<=5;j++){
      print a[j], c
    }
    c+=2
  }
}'

# 5  
Old 06-14-2010
Quote:
Originally Posted by bartus11
Code:
awk '{a[NR]=$1}END{for(i=0;i<=6;i+=2){for(j=1;j<=NR;j++){print a[j],i}}}' infile

where infile contains:
Code:
blue  0
red    0
yellow  0
green  0
orange  0



What if I have more than one column in the infile and I want all columns in the output, i.e:
Code:
blue .5  8.9 0
red   9.3  4.2 0
yellow  2.0 9.1  0
green  7.3  1.8 0
orange  4.3  2.1  0

# 6  
Old 06-14-2010
Code:
awk '{a[NR]=$1" "$2" "$3}END{for(i=0;i<=6;i+=2){for(j=1;j<=NR;j++){print a[j],i}}}' infile

This User Gave Thanks to bartus11 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing the nth parameter in a file

Hello, I use a program using an input stream of parameters: ./my_prog.out <parameters.txt The parameters.txt file has the following structure: name date number1 number2 system now imagine i would like to execute several times (>200) this program, but with different sets of number1... (2 Replies)
Discussion started by: shamsfield
2 Replies

2. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!102.56.1.101!30!50!/ other host server 2 des check_ssh_disk!192.6.1.10!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

3. Shell Programming and Scripting

Indexing each repeating pattern of rows in a column using awk/sed

Hello All, I have data like this in a column. 0 1 2 3 0 3 4 5 6 0 1 2 3 etc. where 0 identifies the start of a pattern in my data. So I need the output like below using either awk/sed. 0 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

4. Shell Programming and Scripting

Parameter file with changing date

Hi, I have a trigger file which looks like this abcdefgh_YYYYMMDD.trg The YYYYMMDD is the year, month and the date. So for example today the trigger file would be abcdefgh_20130703.trg similarly tomorrow it would be abcdefgh_20130704.trg I need to write a script to check if the trigger file... (2 Replies)
Discussion started by: halfafringe
2 Replies

5. Shell Programming and Scripting

Retreiving multiple files by changing a parameter with one program

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

6. IP Networking

Changing network parameter in Existing OS image

Hi , Can some one tell me , is there a way to change the existing network parameter in existing OS image , or do i have to create a new image to have new network parameters . (0 Replies)
Discussion started by: thana
0 Replies

7. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

get the first column of each line as a parameter

hi I have a file with this format server1:port1 server2:port2 server3:port3 I already built a script that starts 1 server. I want to add 'ALL' option to start all servers how to do that (for loop? awk? sed....) thanks for your help (2 Replies)
Discussion started by: melanie_pfefer
2 Replies

9. Shell Programming and Scripting

How to pass one parameter from one column to another column in AWK

I am having scenario where input file is containing text like below ----------------------------------- Name,x.y.z.i.j.k,p.q.r.s.t.k,y.d.f.u.g.h,e.r.t.yu.t.u,...... Place,Bangalore,hyderabad,Chennai,Delhi,............ I need to read and put it in file in a column structure ... (1 Reply)
Discussion started by: prasanta jena
1 Replies

10. Shell Programming and Scripting

Capturing the values of column in one parameter

Hi, I am trying to capture the values of a column in a parameter..here is what I wanted to do... 1,2,3,4 2,3,4,1 3,4,1,2 4,1,2,3 is there any way that I could get the values of column values into one parameter?? Here is what I want... COL1=1,2,3,4 COL2=2,3,4,1 ... (5 Replies)
Discussion started by: mgirinath
5 Replies
Login or Register to Ask a Question