Scripting Issue with Variables from awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting Issue with Variables from awk
# 1  
Old 06-09-2013
Scripting Issue with Variables from awk

Greetings all,

Disclaimer: I'm a novice and always welcome best practices as I'm learning.

File example:
Code:
100,1.1.1.1,1.1.1.2,10.10.10.1,172.16.1.10,172.16.1.20
101,1.1.2.1,1.1.2.2,10.10.20.1,172.16.2.10,172.16.2.20
102,1.1.3.1,1.1.3.2,10.10.30.1,172.16.3.10,172.16.3.20
...and so on

I'm trying to write a loop that will use awk to generate variables from the lines above by pattern matching.

After Googling examples and searching forums, I thought I could use the following:

Code:
#!/bin/bash
i=100

while [ $i -le 102 ] #limited run for testing, ultimately about 600
do

v1=`cat sites.txt | awk -v va=$i ' /^va/ ' | cut -d, -f2`
v2=`cat sites.txt | awk -v va=$i ' /^va/ ' | cut -d, -f3`
v3=`cat sites.txt | awk -v va=$i ' /^va/ ' | cut -d, -f4`
v4=`cat sites.txt | awk -v va=$i ' /^va/ ' | cut -d, -f5`
v5=`cat sites.txt | awk -v va=$i ' /^va/ ' | cut -d, -f6`

echo $i > $i.php
echo $v1 >> $1.php
echo $v2 >> $1.php
echo $v3 >> $1.php
echo $v4 >> $1.php
echo $v5 >> $1.php

(( i++ ))

done

I know stepping out of a script isn't a the best practice, but this is a quick and dirty script just to quickly kick out some flat files.

The variables I'm trying to create do not generate output. I know the $i variable is there.

Output (summary):
Code:
++ cat sites.txt
++ awk -v va=101 ' /^va/ '
++ cut -d, -f2
+ v1=

Would anyone mind giving me a hint to what is going awry? I don't expect anyone to rewrite all the code and make it beautiful, just trying to figure out how I can work through problems like this by myself in the future.

Thanks,
Stephen

Last edited by Scrutinizer; 06-10-2013 at 01:05 AM.. Reason: more code tags
# 2  
Old 06-09-2013
Try for loop instead...see if this works for you


Code:
#!/bin/bash
for i in 100 101 102
do
  v1=`cat sort | awk -v va=$i ' $0~"^"va ' | cut -d, -f2`
  v2=`cat sort | awk -v va=$i ' $0~"^"va ' | cut -d, -f3`
  v3=`cat sort | awk -v va=$i ' $0~"^"va ' | cut -d, -f4`
  v4=`cat sort | awk -v va=$i ' $0~"^"va ' | cut -d, -f5`
  v5=`cat sort | awk -v va=$i ' $0~"^"va ' | cut -d, -f6`

echo $i > $i.php
echo $v1 >> $1.php
echo $v2 >> $1.php
echo $v3 >> $1.php
echo $v4 >> $1.php
echo $v5 >> $1.php

done

# 3  
Old 06-09-2013
Please avoid useless use of cat and also useless use of awk.

Always use shell builtins where ever possible because it will help significantly improve the performance of your script.

Assuming you have one record for each key and it is sorted:
Code:
#!/bin/bash

i=100
while IFS="," read v1 v2 v3 v4 v5 v6 v7
do
        if [ $i -eq $v1 ]
        then
                # Action here
        fi

        (( i++ ))
done < sites.txt

# 4  
Old 06-10-2013
Thanks Yoda for the code.

Yes, I know my code was poorly written and I admitted it. It was my second attempt at a loop and something I cobbled together by experimenting.

Thanks Juzz for reading my post and your thoughts. I just dumped my experiment and used what Yoda provided. Seems I have a long way to go, thanks for not being too harsh.

Last edited by sjrupp; 06-10-2013 at 12:13 AM.. Reason: Thanking posters
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Create, validate and using dynamic variables in Bash scripting

Hi All, I am really struggling to solve this problem, this might be small but I am not able to, can somebody help me? I have few directories and these directories receives text files in large amount with in fraction of seconds. So I just want to send all the files in current directory to... (2 Replies)
Discussion started by: VasuKukkapalli
2 Replies

3. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

4. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code: WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT) when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved. i tested from... (1 Reply)
Discussion started by: SkySmart
1 Replies

5. Shell Programming and Scripting

beginner scripting questions User variables

If there's anywhere to look this up, it would be just as helpful. I googled and really couldn't find anything relative to this. ok... General Variables 1) When creating a script I made a file "prog1.sh" does it matter if the end is .sh or is this what has to be done like prog.bash or... (4 Replies)
Discussion started by: austing5
4 Replies

6. Shell Programming and Scripting

awk scripting issue

hi guys, I am quite a newbie to Linux.I have an issue.Please help me; I have to create a script that runs "ls -la" inside a directory Bong and then I need to run the command through awk and have the output as follows: Filename: abc.txt Owner: test1 Group: test2 Permissions:... (1 Reply)
Discussion started by: mahesh_raghu
1 Replies

7. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

8. Shell Programming and Scripting

exporting variables (shell scripting)

Hi, i've got an executable shell script, called mysc.sh with this line only: export DATASIZE=0 i run it from my console (./mysc.sh), and after that in the console i run: echo $DATASIZE and nothing prints what could be the problem??? thanks!! (2 Replies)
Discussion started by: kfad
2 Replies

9. UNIX for Dummies Questions & Answers

server variables in bash scripting

are they any server variables in bash/SSH scripting as is in PHP or Perl, for example in PHP: $_SERVER or $_SERVER I'm trying to retrieve the absolute path of an .SH script I'm running thank you (6 Replies)
Discussion started by: basher400
6 Replies

10. Shell Programming and Scripting

Awk scripting issue

Ok, after pulling my hair out on this one, i need to ask for some help I have a file called "final" that looks like the following (this is a section of a bigger file) 18-5-252906,180105,00041.00,42070461139120,AUTH CODE:630911,000,101 18-5-255398,170105,01594.00,42110524389101,AUTH... (3 Replies)
Discussion started by: hcclnoodles
3 Replies
Login or Register to Ask a Question