How to replace first line of every file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace first line of every file using awk
# 8  
Old 11-26-2013
then the final version is like this ?

Code:
for file in *.type; do
       awk 'NR==1 {$0=replace} 1' replace=">mm200" $file >tmpfile && mv -f tmpfile $file
done

# 9  
Old 11-26-2013
@quincyjones Yes final version is like that. as long as there is no empty line what I posted will work, if there is any empty line my solution suppresses it (removes empty line).
# 10  
Old 11-26-2013
Not bad but I would modify it a little to make it safer:

Code:
# VITAL:  Back up first!  Otherwise one typo could mangle all your input files
# and make you very sad.
tar -cf type-backup.tar *.type

for file in *.type; do
       awk 'NR==1 {$0=replace} 1' replace=">mm200" "$file" > tmpfile
       # Using cat instead of mv means $file will not change ownership
       # or permissions by accident.  This is because it overwrites the file,
       # rather than deleting it and putting a brand-new file in its place.
       cat tmpfile > "$file"
done

rm -f tmpfile


Last edited by Corona688; 11-26-2013 at 12:01 PM..
# 11  
Old 11-26-2013
Alternatively you can overwrite the file in awk like this (test first, and correct me if I'm wrong):
Code:
awk '{if(NR==1)$0=replace; print > FILENAME}' replace=">mm200" *.type

# 12  
Old 11-26-2013
Quote:
Originally Posted by Subbeh
Alternatively you can overwrite the file in awk like this (test first, and correct me if I'm wrong):
Sorry, that is incorrect. Doing that will truncate the file before awk is finished reading it, throwing away most of its contents.

Even things like sed -i actually use temporary files to store changes to the file until they're ready to replace it.
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 11-26-2013
Quote:
Originally Posted by Corona688
Sorry, that is incorrect. Doing that will truncate the file before awk is finished reading it, throwing away most of its contents.

Even things like sed -i actually use temporary files to store changes to the file until they're ready to replace it.
You're right, it only works on small files, thanks Smilie
# 14  
Old 11-26-2013
Quote:
Originally Posted by Subbeh
You're right, it only works on small files, thanks Smilie
That it even works on small files means awk must be reading several lines in advance for you. You trash the original file pretty instantly, but it won't realize until it runs out of data and needs to read again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to multiple and replace in a specific line

Hi Folks, I have the file in which I need to multiply the content of a line and replace the initial content of that line with the obtained answer. For example if this is my input file file1.txt 2.259314750 xxxxxx 1.962774350 xxxxxx 2.916817290 xxxxxx 1.355026900 ... (4 Replies)
Discussion started by: Madiouma Ndiaye
4 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

5. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

7. Shell Programming and Scripting

sed or awk to replace a value in a certain line.

I have an input like following. *DEFINE_CURVE_TITLE Force for tool binder $# lcid sidr sfa sfo offa offo dattyp 3 0 1 .000000 125.00000 0.000 0.000 0 $# a1 ... (5 Replies)
Discussion started by: hamnsan
5 Replies

8. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

9. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

10. Shell Programming and Scripting

awk replace first line of files

Hi, I am very new to scripting and I know this is a very basic question, but I couldnt find a solution online or make it work. I need to search all my directories and subdirectories for files named run_* and replace the first line if some pattern is found. Here is my first attempt, which... (2 Replies)
Discussion started by: andlessa
2 Replies
Login or Register to Ask a Question