The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print line whatever line i want in a file... there any way kittusri9 Shell Programming and Scripting 1 05-15-2008 09:37 AM
awk print the next line on the current line ajp7701 Shell Programming and Scripting 4 04-17-2008 01:50 PM
How to print 3rd to last line of file? NivekRaz Shell Programming and Scripting 2 09-10-2007 05:04 PM
Print file line by line handak9 Shell Programming and Scripting 2 10-20-2005 05:44 AM
cannot print 3 variables on same line Chris Jones Shell Programming and Scripting 2 04-20-2004 06:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 28
Lightbulb awk print line

Hi All,

I know that i can print the lines from awk just using the print method, but i need to print stuff before it i.e:


BEGIN
{
i=0
}
{
i++
print i ")"
print
}
END
{
}

Here the output is:

1)
Line
2)
Line
3)
etc

But i need it to be on the same line, i.e.
1) Line
2) Line

Thanks, Chris.
Reply With Quote
Forum Sponsor
  #2  
Old 06-14-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Code:
print i ")" $0
or look into 'printf' for finer formatin' options.
Reply With Quote
  #3  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 28
Thanks vgersh99, knew it would be something simple. And ill take a look at printf.

Thanks again, Chris.
Reply With Quote
  #4  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 28
Actually i have another problem, i didnt actually want to print the line to out put i wanted to save it as an array and print at the end i.e.

BEGIN
{
i=0
}
{
i++
Lines[i]= i ")" $0
}
END
{
for(Line in Lines)
{
print Lines[Line]
}
}

Problem here is that the array is returned totally unordered and i get an output like:

4) blah
2) blah
17) blah

Whats happening?
Reply With Quote
  #5  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 28
nevermind just did

for(j=0;J<i;j++)
{
print Lines[j]
}

Ta Chris.
Reply With Quote
  #6  
Old 06-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 28
lol, im back :-). I have no problems printing the results to the screen but i can seem to add them to a file i.e.

for(j=1;J<=i;j++)
{
print Lines[j] > Out
}

Gives me this error:

awk: cmd. line:17: (FILENAME=- FNR=28) fatal: expression for `>' redirection has null string value

ive tryed this but still doesnt work:


for(j=1;J<=i;j++)
{
if(Lines[j]!=null)
{
print Lines[j] > Out
}
}
Reply With Quote
  #7  
Old 06-14-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Code:
for(j=1;J<=i;j++)
   print Lines[j] > "Out"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0