LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-07-2011, 01:48 AM   #1
manish72
LQ Newbie
 
Registered: Mar 2011
Posts: 11

Rep: Reputation: 0
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@:

Last edited by manish72; 07-12-2011 at 11:27 AM. Reason: corrected the required op
 
Old 03-07-2011, 03:26 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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).
 
Old 03-07-2011, 08:47 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Works here too
 
Old 03-07-2011, 08:51 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Works.

(Fedora 14 - GNU Awk 3.1.8)
 
Old 03-07-2011, 11:52 PM   #5
manish72
LQ Newbie
 
Registered: Mar 2011
Posts: 11

Original Poster
Rep: Reputation: 0
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!
 
Old 03-08-2011, 12:42 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 03-08-2011, 01:19 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 07-12-2011, 08:23 AM   #8
manish72
LQ Newbie
 
Registered: Mar 2011
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 07-12-2011, 09:04 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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)
 
Old 07-12-2011, 09:49 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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!!!!

Last edited by grail; 07-12-2011 at 10:13 AM.
 
1 members found this post helpful.
Old 07-12-2011, 10:09 AM   #11
manish72
LQ Newbie
 
Registered: Mar 2011
Posts: 11

Original Poster
Rep: Reputation: 0
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?
 
Old 07-12-2011, 10:55 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by manish72 View Post
Is it because awk doesnt behave the same way on AIX machines?
That's true. Try nawk or gawk (if available).
 
Old 07-12-2011, 11:14 AM   #13
manish72
LQ Newbie
 
Registered: Mar 2011
Posts: 11

Original Poster
Rep: Reputation: 0
I cannot use nawk or gawk as it is not availbale.Is there any work around for this?
 
Old 07-12-2011, 11:17 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by grail View Post
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.
 
Old 07-12-2011, 11:35 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
  


Reply



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
output formatting with awk. pinga123 Programming 4 11-26-2010 07:56 AM
date formatting from stat command tensigh Programming 10 08-30-2010 04:13 AM
Using awk/sed to convert linefeed to csv, with some formatting jaykup Programming 1 04-03-2009 05:18 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
sed or other formatting command help SeT Linux - General 1 10-13-2004 06:57 PM

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

All times are GMT -5. The time now is 07:55 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