[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
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:
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:
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:
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.
Hi, thanks for your rapid response. I changed the code a little because it kept throwing up syntax errors.
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?
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)