awk replace first line of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk replace first line of files
# 1  
Old 10-15-2011
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 almost works:
Code:
find -name "run_*" -exec awk '{if($0 == "#/bin/ksh" || $0 == "#/bin/bash/") print "#\!/bin/bash"; else print $0}' '{}' > "{}_nnew" \;  -exec mv  "{}_nnew" "{}" \;


My problem is that I can not make awk recognize that {} has a value in the output (although it does for the input).
Any suggestions?

Last edited by Franklin52; 10-16-2011 at 08:07 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 10-15-2011
Hi andlessa,

It would be useful if you say what you try to achieve. Next instruction saves original files with suffix .orig. Test it:
Code:
$ find . -type f -iname "run_*" -exec sed -i.orig -e '1 s|^#/bin/[a-zA-Z]\{1,2\}sh|#!/bin/bash|' {} \;

Regards,
Birei
# 3  
Old 10-15-2011

Don't try to squeeze an entire script into the -exec command of find.

Pipe the output of find into a script which you can modify more easily:
Code:
pattern=something
newstring=whatever

find ... |
 while IFS= read -r file
 do
   {
     IFS= read -r line
     case $line in
       $pattern) {
                   printf "%s\n" "$newstring"
                   cat
                 } > tempfile && mv tempfile "$file"
                 ;;
     esac
   } < $file
 done

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

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

3. Shell Programming and Scripting

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 >mm89589585989“”* GGG >HH DG file1.mail >mm454695879357 dg output filemail >mm200 GGG (15 Replies)
Discussion started by: quincyjones
15 Replies

4. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

5. 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

6. Shell Programming and Scripting

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

hi experts , I have an input like following. R sfst 1000.0000 $ new time step for mass scaled calculation R dt2ms -4.000E-7 $ friction value for blank R mue ... (10 Replies)
Discussion started by: hamnsan
10 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 and field using SED and/or AWK or some other suggestion

QUESTION 1: How do you replace a specific line (i.e. line 4) with a new user defined line (i.e. the contents of SAMS’s name, history, math and English grades have been set already). I have been attempting to use SED (FYI: I don’t have GNU SED) or AWK, but haven’t had any luck. FYI: I am using... (1 Reply)
Discussion started by: thibodc
1 Replies

9. Shell Programming and Scripting

Replace line in files using script.

First sorry for my bad english. I have search the forum without finding anything I fully understand, so I ask here. The problem: I have a config file that I will chance onece a day. One line only. I know the linenumber, and I also know the start of the line. Line number 19, and the start... (3 Replies)
Discussion started by: Hempe
3 Replies

10. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question