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
# 1  
Old 11-26-2013
How to replace first line of every file using awk

How to replace first line of every file with mm200 ? Thanx in advance.

file.mail
Code:
>mm89589585989“”*
GGG
>HH
DG

file1.mail
Code:
>mm454695879357
dg

output

filemail
Code:
>mm200
GGG
>HH
DG

file1.mail
Code:
>mm200
dg


Last edited by quincyjones; 11-26-2013 at 06:04 AM..
# 2  
Old 11-26-2013
Try : for every first line

Code:
$ cat <<eof | awk '$0 = NR==1 ? replace : $0' replace=">mm200"
>mm89589585989*
GGG
>HH
DG
eof

>mm200
GGG
>HH
DG

for file use like this
Code:
$ awk '$0 = NR==1 ? replace : $0' replace=">mm200" file

for many files..
Code:
for file in *.type; do
       awk '$0 = NR==1 ? replace : $0' replace=">mm200" $file >tmpfile && mv tmpfile $file
done

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 11-26-2013
Assuming that all the files are in the same directory :
Code:
#!/bin/sh

for file in /path/to/files/*
do
  if [ -f "$file" ]
  then
    sed '1 s/.*/mm200/' $file >tmpFile
    cp tmpFile $file
    rm tmpfFile
  fi
done

# 4  
Old 11-26-2013
@Akshay

for many file this is working fine like this

Code:
awk '$0 = NR==1 ? replace : $0' replace=">mm200" file*

# 5  
Old 11-26-2013
Quote:
Originally Posted by quincyjones
@Akshay

for many file this is working fine like this

Code:
awk '$0 = NR==1 ? replace : $0' replace=">mm200" file*

for many files this won't work because awk will not understand new file with file* as input, since we are not even checking whetherFNR is resetting, so for many files copy last code in post #1 that will work.

--edit--

change mv tmpfile $file to mv -f tmpfile $file once your test gets over with sample data, and Sorry I forgot to put this note in post #1

Last edited by Akshay Hegde; 11-26-2013 at 06:42 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 11-26-2013
@Akshay Hegde: This is working fine, but it replaces every single line in the file by itself or by replace's contents, and - admittedly remote possibility - should there be an empty or zero-valued line, it would be suppressed. Why not simply use
Code:
awk 'NR==1 {$0=replace} 1' replace=">mm200" file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-26-2013
Thanks RudiC. Yes what you said is true.. I just tested like this...thanks for bringing this to my notice.

Code:
$ awk 'BEGIN{for(i=1;i<=5;i++)print i;print}' | awk '$0 = NR==1 ? replace : $0' replace=">mm200"
>mm200
2
3
4
5

Code:
$ awk 'BEGIN{for(i=1;i<=5;i++)print i;print}' | awk 'NR==1 {$0=replace} 1' replace=">mm200"
>mm200
2
3
4
5
            --- > empty line

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