Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie with AWK - need help

Status
Not open for further replies.

SP68

IS-IT--Management
May 22, 2005
18
US
All,

I am new with AWK and want to learn how to manipulate a text file.

Here how the text file looks like before:

Name: XXX
Address: yyy
City: ZZZ

Name: AAA
Address: BBB
City: CCC

And it is how it looks like after:

Name: XXX Address: YYY City: ZZZ
Name: AAA Address: BBB Citry: CCC

Basically, I want to convert name and address in block to line format and as well the other way around.

Please help

Thx

Steve
 
A starting point:
awk '
/^Name:/{n=$0}
/^Address:/{a=$0}
/^City:/{printf "%s %s %s\n",n,a,$0;n=a=""}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
nawk -v RS='' '$1=$1' inputFile

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 

I put the commands in the script file called awk.txt and type in awk -f awk.txt addr.txt and it error out.

awk.txt contains:
'/^Name:/{n=$0}
/^Address:/{a=$0}
/^City:/{printf "%s %s %s\n",n,a,$0;n=a=""}'

Please advice
Thanks,

SP



 

Thanks. It worked after remove the single quote.

SP
 

How do I reverse the format from

Name: XXX Address: YYY City: ZZZ
Name: AAA Address: BBB Citry: CCC

to

Name: XXX
Address: yyy
City: ZZZ

Name: AAA
Address: BBB
City: CCC

Thanks,

SP

 
What is common between "Name:", "Address:" and "City:" ?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
A program like this ?
{sub(/ Address:/,"\nAddress:");sub(/ City:/,"\nCity:");print}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Is it column and row number?

SP
 
You may try this program too:
{gsub(/[^ :]+:/,"\n&");print substr($0,2)"\n"}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Thank you. You are very helpful.

SP
 

I had one issue with script if one or several entries in the input file looks like this:

Name: XXX
Address: yyy
City: ZZZ

Name: AAA
Address: BBB
City: CCC
CCC

Name: AAA
Address: BBB
City: CCC
CCC

How I modify this command

awk '
/^Name:/{n=$0}
/^Address:/{a=$0}
/^City:/{printf "%s %s %s\n",n,a,$0;n=a=""}
' /path/to/input > output

So it prints the wrap around in City: field all on one line.

Result:

Name: XXX Address: YYY City: ZZZ
Name: AAA Address: BBB Citry: CCCCCC
Name: AAA Address: BBB Citry: CCCCCC

Thx,

SP


 

I had one issue with script if one or several entries in the input file looks like this:

Name: XXX
Address: yyy
City: ZZZ

Name: AAA
Address: BBB
City: CCC
CCC

Name: AAA
Address: BBB
City: CCC
CCC

How I modify this command

awk '
/^Name:/{n=$0}
/^Address:/{a=$0}
/^City:/{printf "%s %s %s\n",n,a,$0;n=a=""}
' /path/to/input > output

So it prints the wrap around in City: field all on one line.

Result:

Name: XXX Address: YYY City: ZZZ
Name: AAA Address: BBB City: CCCCCC
Name: AAA Address: BBB City: CCCCCC

Thx,

SP


 
Use the Vlad's suggestion:
awk -v RS='' '$1=$1' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

It worked except I need to get rid of the space on certain lines.
Example:

Name: XXX Address: YYY City: ZZZ
Name: AAA Address: BBB City: CCCC CC
Name: AAA Address: BBB City: CCCCCC

Thx,

SP
 
awk -v RS='' '{gsub(/\n +/,"");$1=$1;print}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

I got an error when I entered at the command prompt:

awk -v RS=" '{gsub(/\n +/,"");$1=$1;print}' test.txt >testt.txt
gawk: cmd. line:1: test.txt
gawk: cmd. line:1: ^ syntax error


 
[tt]awk -v RS=[highlight]''[/highlight] '{gsub(/\n +/,"");$1=$1;print}'[/tt] test.txt >testt.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
BEGIN { RS = "" ; FS = " *\n *" }
{ printf "%s %s %s%s\n", $1,$2,$3,$4 }
Save as test.awk and run with
[tt]awk -f test.awk test.txt >testt.txt[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top