LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   formatting op of awk command (https://www.linuxquestions.org/questions/linux-newbie-8/formatting-op-of-awk-command-866891/)

manish72 03-07-2011 01:48 AM

formatting op of awk command
 
Hi!I have an command to read inputs from file2 and replace the content in file1. Issue is that the op is coming in a single line as gsub and split functions don't recognize new line characters. Can someone plz suggest how to get the op in format given below:
Code:

awk 'NR==FNR{gsub(/input./,""); split($0,a," = ");b[a[1]]=a[2];next} {gsub(/@/,"");for (i in b) gsub(i,b[i])}1' $t2 $t3
This gives belwo op:
Code:

RANGE:DUMMY:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=DUMMY:RECLASS:DUMMY:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=\DUMMY\ANBDGD\KASLLS\AKLLS:
Required op:
Code:

RANGE:DUMMY:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=\\\\DUMMY\\ANBDGD\\KASLLS\\AKLLS:
RECLASS:tb137:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=DUMMY:

The input files to awk cmd are:
t2 content-
Code:

input.xcom.range.hostname = DUMMY
input.xcom.range.remote_system = DUMMY
input.xcom.range.userid = DUMMY
input.xcom.range.password = DUMMY
input.xcom.range.remote_file_system = DUMMY\\\\ANBDGD\\\\KASLLS\\\\AKLLS
input.xcom.reclass.hostname = tb137
input.xcom.reclass.remote_system = DUMMY
input.xcom.reclass.userid = DUMMY
input.xcom.reclass.password = DUMMY
input.xcom.reclass.remote_file_system = DUMMY

t3 content:
Code:

RANGE:@xcom.range.hostname@:REMOTE_SYSTEM=@xcom.range.remote_system@ FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=@xcom.range.userid@ PASSWORD=@xcom.range.password@ COMPRESS=YES QUEUE=NO:REMOTE_FILE=\\\\@xcom.range.remote_file_system @:
RECLASS:@xcom.reclass.hostname@:REMOTE_SYSTEM=@xcom.reclass.remote_system@ FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=@xcom.reclass.userid@ PASSWORD=@xcom.reclass.password@ COMPRESS=YES QUEUE=NO:REMOTE_FILE=@xcom.reclass.remote_file_system@:


colucix 03-07-2011 03:26 AM

Hi and welcome to LinuxQuestions!

I cannot reproduce the problem. Actually your code should work as expected: the print action triggered by the 1 at the end should print out the current record on its own line. Is this code part of a script? Maybe there is something else that prevents the printing of newlines (typically an echo command).

grail 03-07-2011 08:47 AM

Works here too :)

szboardstretcher 03-07-2011 08:51 AM

Works.

(Fedora 14 - GNU Awk 3.1.8)

manish72 03-07-2011 11:52 PM

formatted the exact content
 
Thank you all for your response :) Yes.It is part of a script.I have edited the exact contents of the t3 file.is it because of the extra parameters in t3 that my op is in a single line?If yes,plz help me to correct my awk command.Thanks again!

grail 03-08-2011 12:42 AM

I think you are not understanding us. Your script and data work for us as is, no changes required. Although, noticing now that you are in windows, was either file
written in windows?? If so you will find there are pesky little control characters at the end of the line that can throw you a curve.
Try running dos2unix over it and see if the same script now works.

colucix 03-08-2011 01:19 AM

It would be useful if you posted the updated version of the t3 file and the relevant part of the script? Is the output of awk command stored into a shell variable, then printed out? Also you can give a try to grail's suggestion, since it is one of the first causes for weird problems in shell scripting.

manish72 07-12-2011 08:23 AM

Hi All,

I have a problem with slashes now.If I have 4 slashes (\\\\) in my input file, they get converted to single slash in op (\). I tried using /\/\ in my input file but it didnt help. Can someone suggest how do I get the exact \\\\ in my op? I have edited teh ip and required op fields.

colucix 07-12-2011 09:04 AM

Again it works for me. Please, tell us which version of awk are you running and which is your OS. You're posting from a Windows system and if you run the awk command under cygwin or using the unix tools for windows can be relevant. GNU awk on a Linux system work as expected:
Code:

$ cat > t2
input.xcom.range.hostname = DUMMY
input.xcom.range.remote_system = DUMMY
input.xcom.range.userid = DUMMY
input.xcom.range.password = DUMMY
input.xcom.range.remote_file_system = DUMMY\\\\ANBDGD\\\\KASLLS\\\\AKLLS
input.xcom.reclass.hostname = tb137
input.xcom.reclass.remote_system = DUMMY
input.xcom.reclass.userid = DUMMY
input.xcom.reclass.password = DUMMY
input.xcom.reclass.remote_file_system = DUMMY

$ cat > t3
RANGE:@xcom.range.hostname@:REMOTE_SYSTEM=@xcom.range.remote_system@ FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=@xcom.range.userid@ PASSWORD=@xcom.range.password@ COMPRESS=YES QUEUE=NO:REMOTE_FILE=\\\\@xcom.range.remote_file_system@:
RECLASS:@xcom.reclass.hostname@:REMOTE_SYSTEM=@xcom.reclass.remote_system@ FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=@xcom.reclass.userid@ PASSWORD=@xcom.reclass.password@ COMPRESS=YES QUEUE=NO:REMOTE_FILE=@xcom.reclass.remote_file_system@:

$ awk 'NR==FNR{gsub(/input./,""); split($0,a," = ");b[a[1]]=a[2];next} {gsub(/@/,"");for (i in b) gsub(i,b[i])}1' t2 t3
RANGE:DUMMY:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=\\\\DUMMY\\\\ANBDGD\\\\KASLLS\\\\AKLLS:
RECLASS:tb137:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=DUMMY:

$ awk --version
GNU Awk 3.1.6
$ lsb_release -d
Description:    openSUSE 11.3 (i586)


grail 07-12-2011 09:49 AM

Ditto on output:
Code:

$ awk --version
GNU Awk 3.1.7
$ lsb_release -d
Description:        Ubuntu 10.10

Interestingly, when run with latest gawk I get different results:
Code:

$ awk 'NR==FNR{gsub(/input./,""); split($0,a," = ");b[a[1]]=a[2];next} {gsub(/@/,"");for (i in b) gsub(i,b[i])}1' t2 t3
RANGE:DUMMY:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=\\\\DUMMY\\ANBDGD\\KASLLS\\AKLLS:
RECLASS:tb137:REMOTE_SYSTEM=DUMMY FILE_OPTION=REPLACE CODE_FLAG=ASCII USERID=DUMMY PASSWORD=DUMMY COMPRESS=YES QUEUE=NO:REMOTE_FILE=DUMMY:
$ awk --version
GNU Awk 4.0.0

This version is running on a CLFS derivative (shouldn't matter). I have highlighted the differences in red

Edit: Found this in the changelog for 4.0.0, but not sure if it is the cause:
Quote:

2. The POSIX 2008 behavior for `sub' and `gsub' are now the default.
THIS CHANGES BEHAVIOR!!!!

manish72 07-12-2011 10:09 AM

I am using the awk statement in a shell script on an AIX server. I tried awk --version but its showing me error:
awk --version
awk: Not a recognized flag: -
Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]

Is it because awk doesnt behave the same way on AIX machines?

colucix 07-12-2011 10:55 AM

Quote:

Originally Posted by manish72 (Post 4412760)
Is it because awk doesnt behave the same way on AIX machines?

That's true. Try nawk or gawk (if available).

manish72 07-12-2011 11:14 AM

I cannot use nawk or gawk as it is not availbale.Is there any work around for this?

colucix 07-12-2011 11:17 AM

Quote:

Originally Posted by grail (Post 4412741)
Edit: Found this in the changelog for 4.0.0, but not sure if it is the cause:

The GNU awk manual is not clear about this point. Anyway I get the same behavior (that one you've reported) with gawk 4.0.0. I will deepen into this issue.

grail 07-12-2011 11:35 AM

Hard to say for a solution as your awk is showing a different result, ie all four being converted to one is an anomaly that I cannot replicate.


All times are GMT -5. The time now is 08:54 AM.