The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[need help] output format from awk bucci Shell Programming and Scripting 6 02-09-2007 04:41 AM
Output in a particular format using AWK Raynon Shell Programming and Scripting 4 01-24-2007 04:07 AM
format output Tornado Shell Programming and Scripting 7 11-19-2006 06:17 AM
ls output format tonyt UNIX for Dummies Questions & Answers 6 11-23-2001 11:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-13-2008
bperl bperl is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
Format output using awk in script.

Guys,
I have a script which hits the database and pulls the information that I need into files. Now I want to format these files to make them easy to read.
The sample format of the file will be like....

<Start_of_File>

Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

A1 as_p:xxxxxxxxx as_p:yyyyyyyyy as_p:zzzzzzzzz
A2 as_p:dskfjsjfsd as_p:sdfafjlaflkd


Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

B1 as_p:xxxxxxxxx as_p:yyyyyyyyy as_p:zzzzzzzzz
B2 as_p:dskfjsjfsd as_p:sdfafjlaflkd
B3 as_p:dsoiugtoi as_p:woiuiosgsd as_pwuewuu

Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

C1 as_p:xxxxxxxxx as_p:yyyyyyyyy as_p:zzzzzzzzz
C2 as_p:dskfjsjfsd as_p:sdfafjlaflkd
C3
C4
C5
.
.
.
<End_of_file>


The number of fields in the above records which have "as_p" in them are variable (actual content apart from Hearders).

I would like the file to be formated as....

<Start_of_File>

Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

A1
as_p:xxxxxxxxx
as_p:yyyyyyyyy
as_p:zzzzzzzzz
A2
as_p:dskfjsjfsd
as_p:sdfafjlaflkd


Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

B1
as_p:xxxxxxxxx
as_p:yyyyyyyyy
as_p:zzzzzzzzz
B2
as_p:dskfjsjfsd
as_p:sdfafjlaflkd
B3
as_p:dsoiugtoi
as_p:woiuiosgsd
as_pwuewuu

Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

C1
as_p:xxxxxxxxx
as_p:yyyyyyyyy
as_p:zzzzzzzzz
C2
as_p:dskfjsjfsd
as_p:sdfafjlaflkd
C3
C4
C5
.
.
.
<End_of_file>


Basically I want to search the records which have 'as_p' in them and transpose all the fields in that record in one column. The rest of the records which don't have 'as_p' in them should be left untouched.

Thanks in advance.
  #2 (permalink)  
Old 01-13-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Maybe...
awk '/as_p/ { gsub(" ", "\n")}{print} '
  #3 (permalink)  
Old 01-13-2008
bperl bperl is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
Quote:
Originally Posted by Perderabo View Post
Maybe...
awk '/as_p/ { gsub(" ", "\n")}{print} '
It says syntax error.
I think with gsub you are trying to search and replace globally all matches of " " with new line. That too for records only with 'as_p'.

But since the records will be tab delimited....does it need to be like
Code:
gsub("\t", "\n")
So, it would be something like..
Code:
awk '/as_p/ {gsub("\t", "\n")}; print' FileName
Its stil throwing error.

And since the last field will be delimited by "\n", the gsub might not work for the last field.

If I use the following code....its stripping out the headers....and prints only the records with 'as_p'.

Code:
awk '/as_p/ {for (field=1; field<=NF; ++field) {printf ("\n" $field);}} ' FileName
Do you think the below logic would work?

awk ' { if 'as_p' is there record print
{for (field=1; field<=NF; ++field) {printf ("\n" $field);}}
else
print line
} ' FileName


Sorry I'm new to awk.
  #4 (permalink)  
Old 01-13-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
try using slashes eg gsub(/your pattern/, "" )
  #5 (permalink)  
Old 01-13-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Works for me...

Code:
$ cat data
Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

A1 as_p:xxxxxxxxx as_p:yyyyyyyyy as_p:zzzzzzzzz
A2 as_p:dskfjsjfsd as_p:sdfafjlaflkd


Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

B1 as_p:xxxxxxxxx as_p:yyyyyyyyy as_p:zzzzzzzzz
B2 as_p:dskfjsjfsd as_p:sdfafjlaflkd
B3 as_p:dsoiugtoi as_p:woiuiosgsd as_pwuewuu

Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

C1 as_p:xxxxxxxxx as_p:yyyyyyyyy as_p:zzzzzzzzz
C2 as_p:dskfjsjfsd as_p:sdfafjlaflkd
C3
$
$
$
$
$ awk '/as_p/ { gsub(" ", "\n")}{print} ' data
Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

A1
as_p:xxxxxxxxx
as_p:yyyyyyyyy
as_p:zzzzzzzzz

A2
as_p:dskfjsjfsd
as_p:sdfafjlaflkd


Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

B1
as_p:xxxxxxxxx
as_p:yyyyyyyyy
as_p:zzzzzzzzz

B2
as_p:dskfjsjfsd
as_p:sdfafjlaflkd
B3
as_p:dsoiugtoi
as_p:woiuiosgsd
as_pwuewuu

Header1 .....xsdfsfa...adfa......
Header2 ....afefas .aefaefsdf...
-------------------------------------------------

C1
as_p:xxxxxxxxx
as_p:yyyyyyyyy
as_p:zzzzzzzzz

C2
as_p:dskfjsjfsd
as_p:sdfafjlaflkd
C3
$
  #6 (permalink)  
Old 01-13-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by Perderabo View Post
Maybe...
awk '/as_p/ { gsub(" ", "\n")}{print} '
The above code works but if the data is tab delimited then it needs to be changed so very slightly.

Code:
nawk '{gsub("as_p","\nas_p")}{print}' file
  #7 (permalink)  
Old 01-13-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Quote:
Originally Posted by shamrock View Post
The above code works but if the data is tab delimited then it needs to be changed so very slightly.
True enough, but still, the code I posted shouldn't result in a syntax error.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0