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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Field names with AWK or SED (Newbie)

Status
Not open for further replies.

udaybo

Programmer
Jun 2, 2008
23
US
Hello,


Please HELP

I have a text file in below format, how can I put a header and assign field names to the file with either AWK or SED.


STRT~ VA ~23606 ~TM14~8506~1485 (page 1)

STRT~ VA ~23662 ~TM17~8362~1783 (page 2)
STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783

result:

------------------HEADER-------------------

--A------B------C--------D------E-----F----(Field names)
STRT~ VA ~23606 ~TM14~8506~1485 (page 1)

------------------HEADER-------------------
--A------B------C--------D------E-----F----(Field names)
STRT~ VA ~23662 ~TM17~8362~1783 (page 2)
STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783


Thank you
 
A starting point (typed, not tested):
Code:
awk '
/page /{
  print "------------------HEADER-------------------"
  print "--A------B------C--------D------E-----F----(Field names)"
}
{ print }
' /parh/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you PH,

I ran the script, but get expected output, may be I am doing it wrong (I am very new to Unix scripting)

awk '
/page /{
print "------------------HEADER-------------------"
print "--A------B------C--------D------E-----F----(Field names)"
}
{ print }' F.txt > H.txt

Thanks again
 
but get expected output
Sorry, but what is the problem ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The output doesn't change, it is same as the input file even after running the above script for assigning field names and Header to the file.

STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783
STRT~ VA ~23662 ~TM17~8362~1783 (input file)

Thank you
UD
 
What is the REAL content of F.txt ?
i.e. how do you detect a new page ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hello PH,

Below is the real content of F.txt

STRT ~ 5~NEWPORT NEWS ~VA ~23606 ~TM14~8506~1485
STRT ~ A~NEWPORT NEWS ~VA ~23602 ~TM15~8802~1588
STRT ~ ~NEWPORT NEWS ~VA ~23602 ~TM15~9002~1590
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ 112~HAMPTON ~VA ~23666 ~TM22~8466~2284

Below is the script which I use for next page and I get the output which is below the script. I am trying to add field names and header to the file.

gawk '{
if(NR == 1)
{
prev = $NF
print $0
}
else
{
if($NF == prev)
print $0
else
{
print "\f"
print $0
prev = $NF
}
}
}' F.txt> G.txt

STRT ~ 5~NEWPORT NEWS ~VA ~23606 ~TM14~8506~1485

STRT ~ A~NEWPORT NEWS ~VA ~23602 ~TM15~8802~1588

STRT ~ ~NEWPORT NEWS ~VA ~23602 ~TM15~9002~1590

STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783
STRT ~ ~POQUOSON ~VA ~23662 ~TM17~8362~1783

STRT ~ 112~HAMPTON ~VA ~23666 ~TM22~8466~2284
 
A starting point:
Code:
gawk '
function hdr(){
  print "------------------HEADER-------------------"
  print "--A------B------C--------D------E-----F----(Field names)"
  prev=$NF
}
NR==1{
  hdr()
}
$NF!=prev{
  hdr();print "\f"
}
{
  print
}' F.txt> G.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 


Actually tweeking a bit PHV's code, this should work:
Code:
gawk -F'~' '
function hdr(){
  print "------------------HEADER-------------------"
  print "--A------B------C--------D------E-----F----(Field names)"
  prev=$6
}
NR==1{
  hdr()
}
$6!=prev{
  print "\f"; hdr();
}
{
  print
}' F.txt> G.txt
[noevil]
PS: I used nawk instead.

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
THANK YOU PHV and LKBrwnDBA, you guys are wonderful, thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top