use awk to edit a file..pls help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use awk to edit a file..pls help
# 1  
Old 07-18-2010
use awk to edit a file..pls help

hey i want to over write the fourth field of a ':' delimited file by first finding the required row by using grep.
i have done the following
Code:
cat file | grep no. | awk -F ':' { $4=count; print $1:$2:$3:$4;}


the correct values are being printed but nothin is bein added to the file..please help.What is wrong ?

Last edited by Scott; 07-18-2010 at 07:56 AM.. Reason: Please use code tags
# 2  
Old 07-18-2010
Hi
awk does not change your files in place. You have to take care of changing it by re-direction and etc..

Guru.
# 3  
Old 07-18-2010
Hi.

awk writes to standard output (the screen), not back to the file.

With awk, you can do this:

Code:
awk -F ":" -v OFS=: ' /no./ $4="new value" ' file > new_file && cp new_file file && rm new_file

# 4  
Old 07-18-2010
hey thanx for the replies
@scottn
Will this keep my other rows of the file intact and not affect them. when i use the cp command will it copy to the same row location?
# 5  
Old 07-18-2010
Oops. There was an error in my script!

Code:
awk -F ":" -v OFS=: ' /no./ { $4="new value" } 1' file > new_file && cp new_file file && rm new_file

To answer your questions:
  • Only rows matching what your previous grep did are changed
  • This writes to a temporary file with the new values, then replaces the original file with the new file (with the new values)
# 6  
Old 07-18-2010
hey man!! thanx for the help...correct results now....thanx a lot...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Edit a file using awk ?

Hey guys, I'm trying to learn a bit of awk/sed and I'm using different sites to learn it from, and i think I'm starting to get confused (doesn't take much!). Anyway, say I have a csv file which has something along the lines of the following in it:"test","127.0.0.1","startup... (6 Replies)
Discussion started by: jimbob01
6 Replies

2. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

3. Shell Programming and Scripting

search and edit in the same file using awk

Hi, I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt $cat usr.txt root bin daemon cap $cat passwd.txt root:x:0:0:root:/root:/usr/bin/ksh bin:x:1:1:bin:/bin:/sbin/csh daemon:x:2:2:daemon:/sbin:/usr/bin/ksh adm:x:3:4:adm:/var/adm:/sbin/nologin... (4 Replies)
Discussion started by: Manabhanjan
4 Replies

4. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name? inputfile: ======================= # comment # comment # comment 5 8 10 /tmp 5 8 10 /var/run 5 8 10 /etc/vfstab 5 8 9 /var/tmp 5 8 10 /var/adm/messages... (7 Replies)
Discussion started by: Arsenalman
7 Replies

6. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

7. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

8. UNIX for Dummies Questions & Answers

Pls help me with file editing using awk

Hi all, Pls help me with file editing using awk. I have a text file with the below format. "STANDARD VOLUME ","2009","BUX","V","JCBH49","NF", 001413 "VENDOR MATERIAL-STD ","2009","BUX","V","JCBH49","NF", 009948 "INBOUND TRANS-STD ... (2 Replies)
Discussion started by: srinivas.maddy
2 Replies

9. Shell Programming and Scripting

Help needed in 'awk' : pls.

we have a file File1.I have used the below code to append 'file1' to each row.it is appending 'file1' but there is 5 empty space is coming after the end of each row.i found this opening in 'vi' mode and moving the cursor on each row ...can u help me ... File1: E100|0|05/29/1993|0|E001|E000... (3 Replies)
Discussion started by: satyam_sat
3 Replies

10. Shell Programming and Scripting

Pls Help - Need to edit file located in several directories

I need to update an entry called "mail.RiskNavigatorFromEmailAddress" in all of the "Application.properties" files below, which are located in several different directories... Each of these Application.properties files has the following entry: ... (3 Replies)
Discussion started by: kthatch
3 Replies
Login or Register to Ask a Question