![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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_p wuewuuHeader1 .....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_p wuewuuHeader1 .....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. |
|
||||
|
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")
Code:
awk '/as_p/ {gsub("\t", "\n")}; print' FileName
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
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. |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|