Format output using awk in script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format output using awk in script.
# 1  
Old 01-13-2008
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_pSmiliewuewuu

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_pSmiliewuewuu

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  
Old 01-13-2008
Maybe...
awk '/as_p/ { gsub(" ", "\n")}{print} '
# 3  
Old 01-13-2008
Quote:
Originally Posted by Perderabo
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  
Old 01-13-2008
try using slashes eg gsub(/your pattern/, "" )
# 5  
Old 01-13-2008
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  
Old 01-13-2008
Quote:
Originally Posted by Perderabo
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  
Old 01-13-2008
Quote:
Originally Posted by shamrock
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

2. Shell Programming and Scripting

awk Script to format output

Hi all, I'm fairly new to this and learning along the way, so bare with me... I'm trying to format the output from a script to be more read-friendly. The output contains more servers and more processes but for as example it will do: Server: Test1 ... (4 Replies)
Discussion started by: Bobsonm
4 Replies

3. Shell Programming and Scripting

Adjusting my awk output format

I've been diving into awk but still learning how to use it for text formatting. Below you can see my results are separated by a comma. Can somebody show me how to separate by TAB as well? An explanation would be appreciated as I did not comprehend the answer in the man pages and would like to gain... (1 Reply)
Discussion started by: sudo
1 Replies

4. Shell Programming and Scripting

Format output using awk

Hello all , need help with this ... Input File DEV % POOL 0CB4 FBA 2211300 81792 4 IE RAID-5(3+1) R5_EFD100_1 - - 1805376 82 IF RAID-1 M2_FC300_1 - ... (4 Replies)
Discussion started by: greycells
4 Replies

5. UNIX for Dummies Questions & Answers

after awk-> format output

hi i have a awk command with several querys.... awk 'FS="|""; print $4, $5, $6...etc.... $4 gives me the date 20120304 $5 is timestamp 101023 I want to format these in 2012.03.04 or 2012/03/04 10:10:23 but have no idea, if this is possible with format-parameters in the awk... (2 Replies)
Discussion started by: Jazzmatazz
2 Replies

6. Shell Programming and Scripting

awk to format an output

awk experts, I have in put file with time stamp followed by "," separated data. same patern continues. The output need time stamp in first columns and data total in 2nd columns. Input file T 9:15 d0,1,3,3 d1,2,1,1 d2,3,1,5 e1,1,1,1 T 9:30 d0,1,1,1 d1,2,3,2 d3,1,2,1... (10 Replies)
Discussion started by: arv_cds
10 Replies

7. Shell Programming and Scripting

awk - format output

Input file1 zone: BAU_SERVER1 C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: BAU_SERVER2 C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: ... (4 Replies)
Discussion started by: greycells
4 Replies

8. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

9. Shell Programming and Scripting

[need help] output format from awk

hi all, i have a problem with my nawk command output below is the description : nawk $12 == "00008001" { cnt++;cs_cd } END {for(cd in cs_cd) print cd, cs_cd } 2007020814.TDR output : 133 123 desire output: 133,123,.... please advices thank you so much (6 Replies)
Discussion started by: bucci
6 Replies

10. Shell Programming and Scripting

Output in a particular format using AWK

Hi All, I am trying to check if if column 5 is greater than 90. If greater it will print the term in column 6, else if all are within limit, then it will output "Size is within limit". I can't seem to do that with the below code. The output should only be 1 statement of "Size is within the... (4 Replies)
Discussion started by: Raynon
4 Replies
Login or Register to Ask a Question