Sponsored Content
Top Forums Shell Programming and Scripting Scripting Issue with Variables from awk Post 302818885 by sjrupp on Sunday 9th of June 2013 06:40:06 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
gnomevfs-cat(1) 						   User Commands						   gnomevfs-cat(1)

NAME
gnomevfs-cat - print a file to standard output using the VFS system SYNOPSIS
gnomevfs-cat URI DESCRIPTION
gnomevfs-cat prints a file to standard output using the virtual file system to access the file via a URI. OPERANDS
The following operands are supported: URI Specifies the file location in standard URI format. EXAMPLES
Example 1: Cat a File From a Web Server example% gnomevfs-cat http://www.sun.com Example 2: Cat a Local File example% gnomevfs-cat file:///home/user/README.txt ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables: NLSPATH. EXIT STATUS
The exit value 0 is returned regardless of success or failure. FILES
The following files are used by this application: /usr/bin/gnomevfs-cat Executable to print a file to standard output using the VFS system ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWgnome-vfs | +-----------------------------+-----------------------------+ |Interface stability |External | +-----------------------------+-----------------------------+ SEE ALSO
gnomevfs-copy(1), gnomevfs-info(1), gnomevfs-ls(1), gnomevfs-mkdir(1) NOTES
Written by Stephen Browne, Sun Microsystems Inc., 2004. SunOS 5.10 6 Sep 2004 gnomevfs-cat(1)
All times are GMT -4. The time now is 09:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy