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
User History and commnad log jaydeep_sadaria UNIX for Dummies Questions & Answers 5 02-11-2008 12:01 AM
sorting file and unique commnad.. amon Shell Programming and Scripting 2 02-16-2006 01:19 AM
grep commnad props Shell Programming and Scripting 5 12-14-2004 03:50 PM
Commnad for getting bandwidth usage skotapal UNIX for Dummies Questions & Answers 5 01-16-2003 04:47 AM
How does commnad tail implement chenhao_no1 High Level Programming 1 08-22-2002 10:13 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-29-2006
Registered User
 

Join Date: Jun 2006
Posts: 57
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
How to apply the awk commnad ?

Hi,
I have a file and the contents of the file is say
World
World
World
Now i need to append some more words in each of the line and the the output of the file should like the one below
Will India win the World Cup?
Will India win the World Cup?
Will India win the World Cup?

How can this be done using awk command.?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-29-2006
Registered User
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 363
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Have you givin it a try yet? Any suggestions you might have to offer? It sounds like a homework problem which isn't a horrible thing if you've given some thought and made some effort before asking for assistance.

Carl
Reply With Quote
  #3 (permalink)  
Old 10-29-2006
Registered User
 

Join Date: Oct 2006
Posts: 15
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
While I'm sure that awk can do that, the first thing I thought of when I read your problem was to use sed. Try this:

# sed 's/^/Will India win the /g' file > file1
# mv file1 file
# sed 's/$/ Cup/g' file > file1
# mv file1 file

sed is a good tool when you need to make several simple edits to a text file. In this case, here is what my code does:

First, the general format of a sed command is:

# sed <some commands> <file to edit>

The 's/^/Will India win the /g' file says:
s - substitute
^ - This is the regular expression that means "The beginning of the line".
Will India win the - These are the words that need to go in front of "World".
g - globally. If you do not specify this, sed will apply the change you are asking for only to the first line that matches the regular expression. Using a g here means make that change everywhere (in this case, everywhere means "every line").

The end of the line has "file > file1". When using text processing commands like sed, awk, grep, shell, and perl, it is V-E-R-Y important that you do not redirect the output right back into the file from which you are reading. For example: never do this -

sed some commands file > file

That will cause all kinds of weird stuff to happen. The solution is to store the output of a command to another file, then use the mv command to rename the new file back to the old one.

So, putting it all together:

# sed 's(ubstitute)/^(at the beginning of the line)/(all the stuff between these two forward slashes)Will India win the /g(lobally)' file >(store the output into) file1(or whatever you want to call the second file).

I'm guessing you can figure out the second sed command. The $ is the regular expresion that means "the end of the line". Between these 4 commands, that should do what you want.

So that's how I would do that in sed. I am curious, can anyone explain how to do that in awk?
Reply With Quote
  #4 (permalink)  
Old 10-30-2006
Registered User
 

Join Date: Jun 2006
Posts: 57
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hey thank you for the sed command.You would have taken some pain to explain so much.I got this awk from my friend

awk '{print "Will India win the " $0 " Cup?"}' <file name>
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:50 AM.


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101