[Solved] Assigning a value to a variable name then running a loop on these values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Assigning a value to a variable name then running a loop on these values
# 1  
Old 06-06-2012
[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer.

I have input files containing something like

Code:
File 1

Apples
Apples
Apples 
Apples

File 2 

Bananas
Bananas 
Bananas
Bananas

etc

I want to add a second column where the same number is equal to each fruit e.g. and these need to be sequential (ie, file 1, column 2 value = 1, file 2, column 2 value = 2) etc. The output files would look like this:

Code:
File 1

Apples 1
Apples 1 
Apples 1 
Apples 1

File 2 

Bananas 2
Bananas 2
Bananas 2
Bananas 2

File 3 

Pears 3
Pears 3
Pears 3
Pears 3

I can write the loop for the different fruits easily enough, the problem is adding 1 to the column in each subsequent file. I tried using awk to print values into the second column directly:

Code:
awk '{(($2=1))}1' apples.txt > apples_with_1_in_second_column.txt

This works fine for running individual fruits. Unfortunately, this is a small example and I have hundreds of different files like this and it would take ages to do each one individually.

I was thinking something like:
Code:
fruit=(apples bananas pears) 
for i in {0..2}
do
awk '{$2=(Value + 1 for each subsequent file}1' ${fruit[$i]}.txt > ${fruit[$i]}_with_number_in_second_column.txt
done

Can anyone suggest a generic script that would allow me to do this for hundreds of files like the one above? I'm not married to a solution using awk and any assistance would be very much appreciated.
# 2  
Old 06-06-2012
Like this..?

Code:
awk 'FNR==1 {i++} {print $0 FS i > FILENAME"_with_"i}' apples bananas pears

# 3  
Old 06-06-2012
Hi, thanks for your rapid response. I changed the code a little because it kept throwing up syntax errors.

Code:
fruit=(apples bananas)
for i in {0..1} 
do
awk 'FNR==1 {i++} {print $0 FS i}' ${fruit[$i]}.txt > ${fruit[$i]}_$i.txt
done

It is creating the output files, but 1 is in the second column for both apples and bananas, rather than 1 for apples and 2 for bananas. Am I missing something obvious?

Thanks!
# 4  
Old 06-06-2012
The "i" variable in my post is the awk variable and has nothing to do the shell (for loop). If you have files-names in an array, you can use like this

Code:
awk 'FNR==1 {i++} {print $0 FS i > FILENAME"_with_"i}' ${fruit[*]}

Just try this command (without using loop).

---------- Post updated at 23:24 ---------- Previous update was at 23:11 ----------

with loop,

Code:
for i in {0..1} 
do
awk -v i=$(( i+1 )) '{print $0 FS i}' ${fruit[$i]}.txt > ${fruit[$i]}_$i.txt
done

Again, both the i's are different. just like this

Code:
for i in {0..1} 
do
awk -v x=$(( i+1 )) '{print $0 FS x}' ${fruit[$i]}.txt > ${fruit[$i]}_$i.txt
done

But, In this case you must know the number of elements of the array (right?)
You better loop over the files rather than index.
This User Gave Thanks to clx For This Post:
# 5  
Old 06-07-2012
Sorry for the slow reply back. Brilliant that has solved it. Many thanks for your help CLX Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Assigning Shell variable

Hello, Need a small help to execute below script. #!/bin/bash . new.txt for no in 3 4 do echo $((uname_$no)) done new.txt contains uname_1="XXXXXX" uname_2="YYYYY" uname_3="ZZZZZ" ......... (6 Replies)
Discussion started by: prasanna2166
6 Replies

2. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies

3. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

4. Shell Programming and Scripting

help with assigning multiple values to a variable

I have a situation where my variable needs to pick up any of the 4 values from the environment it is in for e.g i am on server named a server=a (script running on this server) ftp servers= b c d e ----- the parameter passed should be any of these values in these 4 values, if not throw an... (4 Replies)
Discussion started by: dsravan
4 Replies

5. Shell Programming and Scripting

Assigning a set of values to a variable

I wnat to assign a set of values to a variable and use it in if condition. for example: i=$1 d=1 2 3 4 5 6 if then echo "Fine" else echo "Check" fi i will either of the value in d, i.e. i can be 1 or 2 or any value in d, How this can be done? Thanks in advance (2 Replies)
Discussion started by: sol_nov
2 Replies

6. Shell Programming and Scripting

Assigning multiple values to a variable

Hi Guru`s, I have to write a prog. which will traverse through diff. directories and then extract some data from files. I have written it and its working fine. But I have tested it in 1 folder. There are many folders and I need to loop through each of them. I am not sure abt the... (4 Replies)
Discussion started by: unx100
4 Replies

7. Shell Programming and Scripting

retreiving and assigning values and manipulating string in a for loop

Hi I am new to shell scripting and i am preparing a script. for now i am work on a sub part of it..but i am unable to make it work. --- the test code that i am working on -------------------------- IFS="" Sample_eve=`psg proc_s | grep tY` n=0 for line in $Sample_eve... (41 Replies)
Discussion started by: Anteus
41 Replies

8. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

9. Shell Programming and Scripting

problem assigning values to variable

Date of Request: 20080514 10:37 Submitted By: JPCHIANG i want to get the value "JPCHIANG" only in read a file, however, when i do this: name=`"$line"|cut -d " " -f8` it display all the line and append 'not found' at the end of the statement the $line is actually a variable in a... (2 Replies)
Discussion started by: finalight
2 Replies

10. UNIX for Dummies Questions & Answers

assigning values to a variable

i try to get the year and month values using the below shell script when i enter the script like this #!/usr/bin/ksh dd=`DATE +%Y%M` echo $dd it is showing the error as shown below abc.ksh: DATE: not found any suggestions please (3 Replies)
Discussion started by: trichyselva
3 Replies
Login or Register to Ask a Question