LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris and OpenSolaris.
General Sun, SunOS and Sparc related questions also go here.

Notices

Reply
 
Search this Thread
Old 04-27-2012, 02:10 PM   #1
jfsiegel
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Rep: Reputation: Disabled
problems in awk script


I am new to trying to construct scripts but here we go:
I am trying to parse through a rather long file with 10-11 columns per field. What I am trying to do is create a script that will strip out the 10th and 11th column and then compact what is left into one line so it can be read more like a traditional paragraph rather than a really thin column of text. I was able to do it with two nawk commands but scripting it into one file is being problematical. The part I have down and working is thus:

set -x
nawk
{
print $10;print $11
}

Thanks...I hope this forum proves useful.
 
Old 04-27-2012, 02:17 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Welcome to LQ.

Please post a sample of what your input file looks like, and a example of the expected script output.

And by the way, please use code tags to enclose code samples and similar.
 
1 members found this post helpful.
Old 04-27-2012, 02:29 PM   #3
jfsiegel
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
such as I have available

Well I can only paraphrase

Code:
#####: hh hh hh hh hh hh hh hh .#.#.#.#.
so yeah, its a log file. 5 digit number followed by 8 hex pairs followed by a series of characters each spearated by periods instead of spaces. Sorry but I cannot transport the log itself, but I was hoping to get some advice of where to go in a manner that I can apply to the problem at hand and future challenges us much as a way to just solve it. I can do that already using two separate commands but I am hoping for something quicker and more elegant.
 
Old 04-27-2012, 03:36 PM   #4
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 682

Rep: Reputation: 63
So are spaces the delimiter or periods? and do you want the output on different lines?

sample of what I'm using for input:
Code:
12345: ab cd ef gh ij kl mn op .1.2.3.4. column11-1
67890: qr tu vw xy za aa ab ac .5.6.7.8. column11-2
col1: col2 col3 col4 col5 col6 col7 col8 col9 col10 column11-3
if it's just spaces awk '{print $10 " " $11}' filename
Code:
$ awk '{print $10 " " $11}' testfile 
.1.2.3.4. column11-1
.5.6.7.8. column11-2
col10 column11-3
if you're going to use a period for the delimiter then my input isn't right (not enough columns using . for delimiter)
use awk -F. if you want the . as the delimiter and figure out which column you want.
 
Old 04-30-2012, 07:38 AM   #5
jfsiegel
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yes, the spaces are the delimiters. The real difficulty is that sometimes there is a space in the data portion thus creating the column #11. This is why I need to grab both...to make sure I do not loose data where there is data to loose. So you go from

12345: ab cd ef gh ij kl mn op 1.2.3.4.
67890: qr st uv wx yz ab cd ef 5.6.7.8.
23456: ef gh ij kl mn op qr st 9.0. 1.
etc

to

1.2.3.4.
5.6.7.8.
9.0. 1.

which is half way there

What I am trying to get to is

1.2.3.4.5.6.7.8.9.0.1.
etc

Taking what you put there I was able to get it done in one line now as

awk '{print $10 " " $11}' testlog | awk {printf}

however I cannot get this to work in a script. This part has be a bit confused. So although with your help I can get the job done now in one clean command I would really love to understand why it is not working in the script. Thanks!
 
Old 04-30-2012, 08:15 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Once more, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Assuming I understand it correctly, I think this is more robust. It can handle any number of extra spaces.

Code:
$ cat file.txt
12345: ab cd ef gh ij kl mn op 1.2.3.4.
67890: qr st uv wx yz ab cd ef 5.6.7.8.
23456: ef gh ij kl mn op qr st 9.0. 1.

$ nawk '{ for (i=10;i<=NF;i++) { printf("%s" , $i) } } END{ print "" }' file.txt
1.2.3.4.5.6.7.8.9.0.1.
The END statement is only there to add a final newline, if desired.


As for your "script" problem, again, you'll need to explain exactly what you are doing, show us what code you have, and the actual and desired behavior. We aren't mind readers here.

Besides, it's usually better to focus on what you want to do, and leave the how open. Depending on your exact requirements, There may be better solutions.

Last edited by David the H.; 04-30-2012 at 08:33 AM. Reason: small correction
 
1 members found this post helpful.
Old 04-30-2012, 08:26 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Looks like what you want is something like that:
Code:
nawk '{printf("%s %s",$10,$11)}' testlog | tr -d ' ' ; echo
Edit: Sorry, David the H. already provided a cleaner solution.

Last edited by jlliagre; 04-30-2012 at 08:28 AM.
 
Old 04-30-2012, 08:37 AM   #8
jfsiegel
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
David,
That certainly provides a cleaner output...my thanks. The reason I was looking for the answers to scripted solutions is that while I am beginning in this realm I recognize this issue is fairly simple and was hoping to use the occasion to start an understanding of scripts. I have other places I want to go with this but I have no real deep abckground with programming or scripts. So I am sorry if it seemed that I was trying to go at this one certain way...I just had several alterior learning motives behind it. This is also my first forray into using this (or any other UNIX/LINUX) forum so pardon the rough edges.
 
Old 04-30-2012, 08:56 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Na, I wasn't really thinking that. It's just that many new posters don't seem to understand how to properly explain their needs. The more relevant details you can post up-front, the greater the chance of getting useful solutions quickly.

As for scripting, it all comes down to study and practice, just like every other skill. I was a rank beginner myself 10 years ago.

BTW, if you want to turn this exact command into a stand-alone script, it would really be better to make it an awk script directly, rather than an awk command wrapped inside a shell script.

Code:
$ cat oneline.awk
#!/usr/bin/nawk -f

{
        for ( i=10 ; i<=NF ; i++ )
	  {
                printf( "%s" , $i )
          }
}

END{ print "" }


$ ./oneline.awk file.txt
1.2.3.4.5.6.7.8.9.0.1.
Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/


(Be aware that gnu awk has many extensions not available on solaris!)
 
1 members found this post helpful.
  


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
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] Problems to exclude lines and bad filter within AWK script cgcamal Programming 7 04-30-2010 12:38 AM
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 03:38 PM
AWK script problems yaazz Programming 3 07-27-2009 07:14 AM
problems with variables using sed or awk in script FeatherKing Programming 4 09-14-2008 11:13 PM


All times are GMT -5. The time now is 06:52 AM.

Main Menu
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