awk With multiple print variables


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk With multiple print variables
# 8  
Old 04-13-2015
Quote:
Originally Posted by Mohammed Rafi
Hi RudiC,

Both the approach are working fine, But the OFS is not outputting in the file, not sure why its omitting the OFS.

Thanks
it should be in mine.
# 9  
Old 04-13-2015
I believe, for making it more generic, I need to use the solution by vgersh99 with the for loop, which is with the below command
Code:
nawk -v para="$param" 'BEGIN {FS="\037";OFS="\037"; n=split (para, P, "$")} {for(i=1; i>=n;i++) printf("%s%s", $P[i], (i==n)?ORS:OFS)}' 1.dat,

But its doesn't O/P anything. Is it because of the solaris OS?

Last edited by rbatte1; 04-13-2015 at 01:41 PM.. Reason: Added CODE tags
# 10  
Old 04-13-2015
Quote:
Originally Posted by Mohammed Rafi
I believe, for making it more generic, I need to use the solution by vgersh99 with the for loop, which is with the below command
Code:
nawk -v para="$param" 'BEGIN {FS="\037";OFS="\037"; n=split (para, P, "$")} {for(i=1; i>=n;i++) printf("%s%s", $P[i], (i==n)?ORS:OFS)}' 1.dat,

But its doesn't O/P anything. Is it because of the solaris OS?
Most likely it's because this is not the suggested solution? Also we don't know what the '$param' looks like...
This User Gave Thanks to vgersh99 For This Post:
# 11  
Old 04-13-2015
Many thanks, This is working perfectly fine.

Code:
nawk -v para="$param" 'BEGIN {FS="\037";OFS="\037"; n=split (para, P, "$")} {for(i=2; i<=n;i++) printf("%s%s", $P[i], (i==n)?ORS:OFS)}' 1.dat

Regards,

---------- Post updated at 12:03 PM ---------- Previous update was at 12:02 PM ----------

Just not sure why the first number i should be equal to 2?, But it works

Last edited by Don Cragun; 04-13-2015 at 04:00 PM.. Reason: Add CODE tags, again.
# 12  
Old 04-13-2015
Quote:
Originally Posted by Mohammed Rafi
Many thanks, This is working perfectly fine.

nawk -v para="$param" 'BEGIN {FS="\037";OFS="\037"; n=split (para, P, "$")} {for(i=2; i<=n;i++) printf("%s%s", $P[i], (i==n)?ORS:OFS)}' 1.dat

Regards,

---------- Post updated at 12:03 PM ---------- Previous update was at 12:02 PM ----------

Just not sure why the first number i should be equal to 2?, But it works
If your param='$1$2' and you n=split (para, P, "$"), then the P[1] is empty as the field preceding the FIRST $, is empty.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple variables using awk and for loop for web form submission

Hi My goal is to fill an HTML form and submit. What I have managed to do: 1. curl command to fill up the form and submit 2. a file which has the input curl command: curl -v -b cookie.txt -d __CSRFToken__=dc23d5da47953b3b390ec68d972af10380908b14 -d do=create -d a=open -d... (10 Replies)
Discussion started by: zorrox
10 Replies

2. Shell Programming and Scripting

Awk: is it possible to print into multiple columns?

Hi guys, I have hundreds file like this, here I only show two of them: file 1 feco4_s_BB95.log ZE_1=-1717.5206260 feco4_t_BB95.log ZE_1=-1717.5169250 feco5_s_BB95.log ZE_1=-1830.9322060... (11 Replies)
Discussion started by: liuzhencc
11 Replies

3. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

4. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

5. Shell Programming and Scripting

awk output to multiple variables

Hi I need to assign the ouput of a awk statement to two variables; below is a example of the txt file i have which I use awk against sample file testval,USA,loc2,testing02 testval1,GB,loc4,testing01 awk statement awk -F , '{print $2,$3}' USA loc2 GB loc4 I need a method where... (6 Replies)
Discussion started by: duckeggs01
6 Replies

6. Shell Programming and Scripting

Variables into SED or AWK and multiple commands

Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used... (4 Replies)
Discussion started by: lostincashe
4 Replies

7. Shell Programming and Scripting

To print variables using awk

Can anyone help me with how to print the variable using a awk statement. for i in ` cat serverlist.txt ` ; do my command | awk '{print $1 $2 $i}' done It should print like below but it is not XXXXX YYYYY Servername XXXXX YYYYY Servername XXXXX YYYYY Servername XXXXX YYYYY... (6 Replies)
Discussion started by: rrb2009
6 Replies

8. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

9. Shell Programming and Scripting

awk (or other) script that assigns fields from a line to multiple variables

Hey all, Unfortunately I have only basic knowledge of awk and/or scripting. If I have a file with lines that can look similar to this: Name=line1 Arg1=valueA Arg2=valueB Arg3=valueC Name=line2 Arg1=valueD Name=line3 Arg1=valueE Arg3=valueF Name=line4 Arg2=valueG ... (4 Replies)
Discussion started by: Rike255
4 Replies

10. UNIX for Dummies Questions & Answers

print multiple lines with awk

Hi everyone! I'm not new to Unix, but I've never used awk before. I tried to look up this information on several sites and forums, I also looked in the documentation but I haven't found a solution yet. I would like to print the previous 3 lines before and the following 4 lines after the... (6 Replies)
Discussion started by: djcsabus
6 Replies
Login or Register to Ask a Question