The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-30-2006
preethgideon preethgideon is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 71
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.?
  #2 (permalink)  
Old 10-30-2006
BOFH BOFH is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 406
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
  #3 (permalink)  
Old 10-30-2006
Heathe_Kyle Heathe_Kyle is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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?
  #4 (permalink)  
Old 10-30-2006
preethgideon preethgideon is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 71
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>
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0