LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2013, 04:22 AM   #1
jozelo
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Rep: Reputation: 0
Question awk print dos not print text value


I Have a simple shell-awk program called "nu" to print a list of numbers as follows:

#Genero una lista de números
#recibo 1, 2, 3 o 4 parametros Si me dan 1 la genero desde 0 hasta ese numero de 1 en 1
#Si me dan 2 la genero desde $1 hasta $2 de uno en uno
# si me dan tres desde $1 hasta $2 de $3 en $3
# si me dan cuatro desde $1 hasta $2 de $3 en $3 uso como prefijo el parametro $4

echo $# $1 $2 $3 $4
case $#
in
"1") awk "BEGIN {i=0;j=$1;k=1 ; for (x=i;x<=j;x=x+k) {print x}}";;
"2") awk "BEGIN {i=$1;j=$2;k=1 ; for (x=i;x<=j;x=x+k) {print x}}";;
"3") awk "BEGIN {i=$1;j=$2;k=$3 ; for (x=i;x<=j;x=x+k) {print x}}";;
"4") awk "BEGIN {i=$1;j=$2;k=$3 ; for (x=i;x<=j;x=x+k) {print $4 x}}";;
esac

when i run it as:
/jml58z/ShLinux# nu 1320 1339 1 CMUR0
4 1320 1339 1 CMUR0
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339


It dos not print the variable $4 "CMUR0"
I have tried with printf("%s %d \n", $4 x) but not work either
 
Old 10-23-2013, 04:33 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,504

Rep: Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572
Your use of $4 in this line is incorrect:

Code:
awk "BEGIN {i=$1;j=$2;k=$3 ; for (x=i;x<=j;x=x+k) {print $4 x}}";;
In that context it tells awk to print the 4th field, which doesn't exist.

You could pass your value to awk as a parameter:

Code:
awk "BEGIN {i=$1;j=$2;k=$3 ; for (x=i;x<=j;x=x+k) {print ARGV[1] x}}" $4;;
Which will give you:

#./nu 1320 1339 1 CMUR0
4 1320 1339 1 CMUR0
CMUR01320
CMUR01321
CMUR01322
CMUR01323
CMUR01324
CMUR01325
CMUR01326
CMUR01327
CMUR01328
CMUR01329
CMUR01330
CMUR01331
CMUR01332
CMUR01333
CMUR01334
CMUR01335
CMUR01336
CMUR01337
CMUR01338
CMUR01339
 
Old 10-23-2013, 04:37 AM   #3
jozelo
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Original Poster
Rep: Reputation: 0
It worked perfectly thanks a lot
 
  


Reply

Tags
awk, print, printf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] help with awk-print please saeedamer Programming 6 03-16-2012 12:44 PM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 02:40 AM
How do I - not print - in awk? ZimMonkey Programming 10 08-21-2009 07:11 PM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM
HP PS7150: text doesn't print in OOO and Abiword!, but I can print test page & w MzFF gabba Mandriva 6 10-31-2005 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration