SED command using multiple input files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED command using multiple input files
# 1  
Old 09-20-2010
SED command using multiple input files

What is the syntax to use multiple input files in a SED command. i.e. substitute a word with a phrase in every file in a directory.

for every file in /usr/include that has the word "date" in the file
grep -l '\<date\>' /usr/include/*.h
find each occurrence of the word "time" in the file & replace it with "Smarts" and write the results to newfile.txt
sed -n 's/\<time\>/Smarts/pg' [results of the grep -l query above] >>newfile.txt
I tried starting with the grep & pipe to sed. it just reads the filenames in, not the file.

I can sed -n 's/\<time\>/Smarts/pg' /usr/include/*.h >>newfile.txt but that uses every file in /usr/include, not just the ones containing the word "date"

Looks like I need your help!
# 2  
Old 09-20-2010
You can try..

Code:
grep -l '\<date\>' /usr/include/*.h | xargs -iVAR sed -n 's/\<time\>/Smarts/pg' VAR > newfile.txt

# 3  
Old 09-20-2010
Assuming that the text you are matching against in the files is "<date>" and "<time>" (< and > are not special in sed/grep basic regular expressions and do not require escaping), the following approach is safe regardless of what characters the filenames may contain:
Code:
find /usr/include -type f -name \*.h -exec sh -c '
    for f; do
        grep -q "<date>" "$f" && sed -n "s/<time>/Smarts/gp" "$f" >> newfile.txt
    done
' sh {} +

Regards,
Alister

Last edited by alister; 09-20-2010 at 05:16 AM..
# 4  
Old 09-20-2010
Code:
grep -l '\<date\>' /usr/include/*.h |while read LINE
do
    sed 's/<time>/Smarts/g' $LINE >> newfile.txt
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Command on Multiple Files and retaining the same file name

Hi All I am trying to run sed command to remove first 2 charcaters from a file on Multiple Files in my directory and what to retain the same file name . I want to know how to retain the same file name but with changes . Can some one please let me know how to proceed with this . ... (7 Replies)
Discussion started by: honey26
7 Replies

2. Shell Programming and Scripting

[Solved] Multiple input files and output files

Hi, I have many test*.ft1 files to which I want to read as input for a script called pipe2txt.tcl and print the output in each separate file. For example, pipe2txt.tcl < test001.ft1 > test001.txt How can I read many files in this maner? thank you very much, Best, Pahuja (5 Replies)
Discussion started by: Pahuja
5 Replies

3. Shell Programming and Scripting

Command line multiple input

I'm using the below to get multiple input from USER and it is working, is there any better way in awk array single liner? echo "Enter Multiple input (Ctrl+d to exit)" >output while read A do echo "$A" >>output done (3 Replies)
Discussion started by: Roozo
3 Replies

4. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

5. UNIX for Advanced & Expert Users

Input for multiple files.

Hi, I am trying to come up with a script, and would like the script to pick all the files place within a folder and interactive take my yes/no before processing within the command. Could you someone help me in modifying the script : #!/bin/bash # LDIF_FILES="File Name" for MY_FILE... (5 Replies)
Discussion started by: john_prince
5 Replies

6. Shell Programming and Scripting

how to redirect multiple input files?

I have a program that runs like "cat f1 - f2 -", I need to write shell script to run the program whose standard input will be redirected from 2 files. I spend a whole day on it, but didn't figure out. Can someone help me out? Thanks! (8 Replies)
Discussion started by: microstarwwx
8 Replies

7. Shell Programming and Scripting

how to use multiple files in sed with w command

i have a command like : sed -n 's/^* /&/w even' <file if i want to write to multiple files like sed -n 's/^* /&/w zero two three' < file its not working it is taking "zero two three" as a single file i want to write to 3 seperate files . pls can anyone help me (2 Replies)
Discussion started by: santosh1234
2 Replies

8. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies

9. Shell Programming and Scripting

flexible sed command needed to handle multiple input types

Hello, I need a smart sed command that can take any of the following two as an input and give below mentioned output. As you can see, I am trying to convert some C code INPUT: struct abc_sample1 { char myString; UINT16 myValue1; ... (2 Replies)
Discussion started by: SiftinDotCom
2 Replies

10. UNIX for Dummies Questions & Answers

can you redirect multiple files for input?

I have a program that is reading strings into a vector from a file. Currently I am using this command: a.out < file1 The program runs and prints the contents of the vector to the screen, like its supposed to. The problem is that it needs to read in 3 files to fill the vector. Is there anyway... (4 Replies)
Discussion started by: Matrix_Prime
4 Replies
Login or Register to Ask a Question