How to apply the awk commnad ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to apply the awk commnad ?
# 1  
Old 10-30-2006
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  
Old 10-30-2006
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  
Old 10-30-2006
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  
Old 10-30-2006
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>
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to match and apply condtions to matchijng files in directories

I am trying to merge the below awk, which compares two files looking for a match in $2 and then prints the line if two conditions are meet. awk awk 'FNR==NR{A=$0;next} ($2 in A){if($10>30 && $11>49){print A}}' F113.txt F113_tvc.bed This code was improved and provided by @RavinderSingh13,... (18 Replies)
Discussion started by: cmccabe
18 Replies

2. Shell Programming and Scripting

OFS does not apply to few records in awk

Hi , I am having a problem with my awk oneliner , which for some reason leaves the first two records Input File $ cat file1 A1:B1:C1:NoLimit M1:M2:M3:Limit A2:B2:C2,C3,C4,C5 A3:B3:C3,C4,C5,C6,C7Desired output A1,B1,C1,NoLimit M1,M2,M3,Limit A2,B2,C2 ,,,C3 ,,,C4 ,,,C5 A3,B3,C3... (5 Replies)
Discussion started by: chidori
5 Replies

3. UNIX for Dummies Questions & Answers

apply a function twice successively with the same input in awk program

Hi ! It is a general question. When an awk script looks like: #! bin/awk function example(i){ <body> } { example(1) #the function uses input_1 and return output_a } { example(2) #the function uses previous output_a as an input and returns... (15 Replies)
Discussion started by: beca123456
15 Replies

4. UNIX for Advanced & Expert Users

zsh and watch commnad

Hi Guys i have set the watch command on my shell watch=all but this will report when i open and close a shell, is there a way so that it will ignore me user but only when i am on my machine i can set it to watch=notme but that will not detect if someone su -<my... (0 Replies)
Discussion started by: ab52
0 Replies

5. Shell Programming and Scripting

apply record separator to multiple files within a directory using awk

Hi, I have a bunch of records within a directory where each one has this form: (example file1) 1 2 50 90 80 90 43512 98 0909 79869 -9 7878 33222 8787 9090 89898 7878 8989 7878 6767 89 89 78676 9898 000 7878 5656 5454 5454 and i want for all of these files to be... (3 Replies)
Discussion started by: amarn
3 Replies

6. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

7. Shell Programming and Scripting

How to apply sub/gsub of awk on a particular field?

I got a file with contents are of the following format ... 2007-09-28 ./.passwwd1.sh.swp 2007-11-26 ./827-55.jpg 2007-09-28 ./argcheck.pl ... I have to delete all the '-' in the "first field", ie. the date, not on the second field, which is the name of the file. i.e. required output ... (1 Reply)
Discussion started by: jaduks
1 Replies

8. Shell Programming and Scripting

grep commnad

echo "Enter a,: \c" read answer echo case "$answer" in a|A) echo "Enter a file :\c" read $answer echo " Enter a string to be searched :\c" read $answer2 if then echo " file doesn't exist" ... (5 Replies)
Discussion started by: props
5 Replies

9. UNIX for Dummies Questions & Answers

Commnad for getting bandwidth usage

Hi all Wanted to know if there is a command that gives me the bandwidth used at any point of time (in mbps). I pay for the bandwidth I have used and need to be alerted everytime I corss over a certain limit. Regards KS (5 Replies)
Discussion started by: skotapal
5 Replies

10. Programming

How does commnad tail implement

How does commnad tail implement ? Thank you . (1 Reply)
Discussion started by: chenhao_no1
1 Replies
Login or Register to Ask a Question