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
# 15  
Old 11-26-2013
How about this ?

Generate some files for test
Code:
$ awk 'BEGIN{for(i=1;i<=5;i++)for(j=1;j<=5;j++)print j >"file"i".tmp"}'
$ ls file*.tmp -1
file1.tmp
file2.tmp
file3.tmp
file4.tmp
file5.tmp

BEFORE
Code:
$ awk '{ print FNR==1 && NR!=1 || NR==1 ? $0 FS FILENAME : $0 }' file*.tmp
1 file1.tmp
2
3
4
5
1 file2.tmp
2
3
4
5
1 file3.tmp
2
3
4
5
1 file4.tmp
2
3
4
5
1 file5.tmp
2
3
4
5

Code:
awk '

function write_2_og_file(file){
                                 system(sprintf("%s %s %s","cat","tmpfile>",file))
                              }

     NR==1 || NR!=1 && FNR==1{
                                  w  = "tmpfile"
                                  close(w)
                                  $0 = replace
                                  f = FILENAME
                             }
              NR!=1 && FNR==1{
                                  write_2_og_file(p)
                             }
                             {
                                  print $0 >w
                                  p=f
                             }
                          END{
                                  close(w)
                                  write_2_og_file(p)
                                  system("rm tmpfile")
                             }
    '   replace="<m200" file*.tmp

AFTER
Code:
$ awk '{ print FNR==1 && NR!=1 || NR==1 ? $0 FS FILENAME : $0 }' file*.tmp
<m200 file1.tmp
2
3
4
5
<m200 file2.tmp
2
3
4
5
<m200 file3.tmp
2
3
4
5
<m200 file4.tmp
2
3
4
5
<m200 file5.tmp
2
3
4
5

Let me know if I missed anything...
# 16  
Old 11-26-2013
I realize that the title of this thread requests a solution in awk, but most of the discussion in this thread seems to revolve around the fact that awk is not the correct tool for this job. Using ed or ex as in:
Code:
#!/bin/ksh
for i in "$@"
do      ed -s "$i" <<EOF
                1s/.*/>mm200/
                w
                q
EOF
done

avoids issues with input files being truncated, permissions changing, ownership changing, etc.
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