LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-06-2008, 06:39 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
modify all fields in awk


Hi ,

Is there a way to modify all fields same way in awk eg.

Code:
awk -F'|' '{BEGIN OFS=","} {print FRNTEND_$1_BCKEND}' FileName
I want to aply similar kind of update to all fields not just one or two.
 
Old 08-07-2008, 03:56 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

You'll have to loop through the fields, with something like
Code:
for (i=1; i<=NF; i++) {print FRNTEND_$i_BCKEND}
Dave
 
Old 08-07-2008, 10:34 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You should use printf to avoid printing each field on a new line. However, I would use a slightly different approach altering the value of each field using sub, then printing the whole altered line:
Code:
awk '{for (i=1; i<=NF; i++) sub($i,"FRNTEND_"$i"_BCKEND",$i)}1' file
 
Old 08-07-2008, 11:09 AM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Quote:
Originally Posted by colucix View Post
You should use printf to avoid printing each field on a new line. However, I would use a slightly different approach altering the value of each field using sub, then printing the whole altered line:
Code:
awk '{for (i=1; i<=NF; i++) sub($i,"FRNTEND_"$i"_BCKEND",$i)}1' file
Watch yerself. If $1 contains a string that looks like a regex, it'll be treated as such - maybe not what you want...
 
Old 08-07-2008, 12:11 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You're totally right, ilikejam! Thank you! I suggest the following, now:
Code:
awk '{for (i=1; i<=NF; i++) sub(/.*/,"FRNTEND_"$i"_BCKEND",$i)}1' file
 
Old 08-07-2008, 04:42 PM   #6
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
You're totally right, ilikejam! Thank you! I suggest the following, now:
Code:
awk '{for (i=1; i<=NF; i++) sub(/.*/,"FRNTEND_"$i"_BCKEND",$i)}1' file
I tried to run this command , but it gives error. Why doesn't awk display error messages properly
Quote:
awk: syntax error near line 1
awk: illegal statement near line 1
Can we use the same awk statment for variables (arrays)

Last edited by tostay2003; 08-07-2008 at 05:34 PM.
 
Old 08-07-2008, 06:06 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Which version of awk are you running and on which system? The only issue that comes in my mind is the Solaris Sparc version of awk (and other Unix systems) which does not have the sub function. If this is the case, is nawk available on your system? Or even better... gawk?

Regarding the array issue, you can have a similar behaviour with a slightly modified approach: you have to use a for loop to scan all the elements of the array and alter them, instead of the input fields. But at this point is crucial to know which awk version and on which system you're running on.

Last edited by colucix; 08-07-2008 at 06:07 PM.
 
Old 08-07-2008, 06:13 PM   #8
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Which version of awk are you running and on which system? The only issue that comes in my mind is the Solaris Sparc version of awk (and other Unix systems) which does not have the sub function. If this is the case, is nawk available on your system? Or even better... gawk?

Regarding the array issue, you can have a similar behaviour with a slightly modified approach: you have to use a for loop to scan all the elements of the array and alter them, instead of the input fields. But at this point is crucial to know which awk version and on which system you're running on.
I don't know how to check the awk version. However made a grep on man awk. And found only substr() but not sub()

How can we go on with the array without sub() function

Last edited by tostay2003; 08-07-2008 at 06:16 PM.
 
Old 08-07-2008, 06:45 PM   #9
ne pas
Member
 
Registered: Jul 2008
Posts: 55

Rep: Reputation: 23
Code:
awk -F'|' 'BEGIN { ORS="" }' \
'{ for (i=1; i<=NF; i++){print "FRNTEND_" $i "_BCKEND"} print "\n" }' example_file
 
Old 08-08-2008, 03:54 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by tostay2003 View Post
I don't know how to check the awk version. However made a grep on man awk. And found only substr() but not sub()
Ok... I definitively think you're on a Unix system. Try
Code:
cat /etc/release
to confirm. Also check if you have the enhanced version of awk installed or the GNU awk:
Code:
which nawk
which gawk
both of these support the sub function. Otherwise, you can try to elaborate the other suggested solutions to make the output match exactly your requirements.
 
Old 08-08-2008, 04:22 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
An alternative to give exactly what do you want, without extra spaces or unwanted newlines, is
Code:
awk '{for (i=1; i<=NF-1; i++) printf "%s ","FRNTEND_" $i "_BCKEND"; print "FRNTEND_" $NF "_BCKEND"}' file
this uses printf to print the altered fields except the last one. They are printed with a space at the back end. Then print the last field using print, which puts a newline at the end, as usual.

Regarding the array part of your question, can you explain more precisely your requirements?
 
Old 08-08-2008, 10:57 AM   #12
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Ok... I definitively think you're on a Unix system. Try
Code:
cat /etc/release
to confirm. Also check if you have the enhanced version of awk installed or the GNU awk:
Code:
which nawk
which gawk
both of these support the sub function. Otherwise, you can try to elaborate the other suggested solutions to make the output match exactly your requirements.
The version is

a) Solaris 10
Copyright 2006 Sun Microsystems, Inc.

b) I have nawk but not gawk

Quote:
Originally Posted by colucix View Post
Regarding the array part of your question, can you explain more precisely your requirements?
Instead of data being in the file, I will be having it a variable OR array variable. And want to do the same manipulation i.e. FRNTEND_$i_BCKEND for each array element.

Last edited by tostay2003; 08-08-2008 at 01:05 PM.
 
Old 08-08-2008, 02:30 PM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by tostay2003 View Post
b) I have nawk but not gawk
Ok. In nawk the version with the sub function works. Otherwise you can try the one I suggested in post #11.
Quote:
Originally Posted by tostay2003 View Post
Instead of data being in the file, I will be having it a variable OR array variable. And want to do the same manipulation i.e. FRNTEND_$i_BCKEND for each array element.
To scan all the elements of an array you will use a for loop like
Code:
for (var in array) {
     array[var] = "FRONTEND_" array[var] "_BACKEND"
     <other statements here>
}

Last edited by colucix; 08-08-2008 at 03:05 PM. Reason: Changed loop variable (var instead of index): you can't use a function name as variable name in awk
 
Old 08-08-2008, 03:52 PM   #14
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
To scan all the elements of an array you will use a for loop like
Code:
for (var in array) {
     array[var] = "FRONTEND_" array[var] "_BACKEND"
     <other statements here>
}
This is long prevailing question in my mind.

How do we do the same with a variable (not array, just some field seperated variable - string) Without using help of another variable.

eg.

Code:
var1=Field1,Field2,Field3
Now this should turn out to

Code:
var2=FRNT_Field1_END, FRNT_Field2_END, FRNT_Field3_END
Without making use of another variable... just a awk/sed or anything acting on variable just like it acts on files and modifies the file. Ho w could me modify the variable.
 
Old 08-08-2008, 04:19 PM   #15
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Sorry, but I don't follow you anymore. What are you trying to achieve? Where this variable comes from? Is it created in a shell script or something? Can you provide a real example?

Does this match what you're trying to do?
Code:
#!/bin/bash
var1=Field1,Field2,Field3
var2=$(echo $var1 | awk -F, '{for (i=1; i<=NF-1; i++) printf "%s, ","FRNT_" $i "_END"; printf "%s","FRNT_" $NF "_END"}')
echo $var2
 
  


Reply


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 07:23 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
AWK - why fields go to seperate lines? korhan Linux - Newbie 2 03-01-2007 04:21 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 12:06 PM
search for specific text in fields using awk Helene Programming 2 04-23-2004 01:13 AM


All times are GMT -5. The time now is 06:45 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration