How to use a dynamic filename with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use a dynamic filename with sed?
# 1  
Old 12-09-2009
Tools How to use a dynamic filename with sed?

I have a line that works for static filename
Code:
cat /directorypath/filename | sed '/[text pattern1]/d;/[text pattern2]/d' > filename

This approach when used in a script works well.

Then i need a list of filenames to give this line.

I can get the list into a file by
Code:
filelist1='ls -m'

then use
Code:
filelist2=${filelist1##ls -m}
echo $filelist2 > mylist

This will create a list of filenames.
This line will print each
Code:
awk '{ for (i = 1; i <=NF; i++) print$i }' mylist

I figured it would be easy to sub the print$i with my cat / sed line using $i as the filename.

This is SFU on a windows box.

any help from the experts out there?

Last edited by pludi; 12-09-2009 at 04:24 PM.. Reason: code tags, please...
# 2  
Old 12-09-2009
How about:
Code:
for FILE in /directorypath/*; do
  cat ${FILE} | sed '/[text pattern1]/d;/[text pattern2]/d' > `basename ${FILE}`
done

?
# 3  
Old 12-09-2009
Hammer & Screwdriver What is currently working

Thanks for your suggestion, what I am looking for now is to loop through the list.

I have just modified the code to:
Code:
cat $filename2 | sed '/Email/d;/Web/d' > temp.1

Code:
mv temp.1 $filename2

This works for one file.

I was looking for a way to handle errors (what if it runs and has no files?)
and what if more than one files comes in?

As mentioned, I can create a file that has the list.

---------- Post updated at 04:16 PM ---------- Previous update was at 04:08 PM ----------

Hi,

I tried the FILE code with multiple files

the message I got back was;
/bin/fix2.sh[3]: FILE: not found and it keep the editor open needed to ctrl 'c' to get prompt back.

Is the code you suggested exactly what I should type? or is FILE something I needed to change?

I am very new to this scripting.
# 4  
Old 12-09-2009
The following is an example of reading in a list of files from a file:
Code:
while read FILE; do
  echo FILE = $FILE
done < file_list

# 5  
Old 12-10-2009
MySQL Thanks Tony

Hi Tony,

thank you for your reply.

I have it working now.

What I figured out was I should have used Korn Shell not Bash.

SFU uses the Korn shell and once I figured that out I redid my script and it is working.

Thanks again

This post can be closed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk : dynamic output flatfile filename

Hello, I'm using the awk command to insert empty columns on a tab delimited flatfile - which works fine - => But I'm not able to manage dynamicaly the filename of the awk output based on the source flatfile filename I have 3 source flatfile: flatfile_Jan-2016.csv flatfile_Feb-2016.csv... (3 Replies)
Discussion started by: Tipiak
3 Replies

2. Shell Programming and Scripting

Embed cmd into sed replacement to have dynamic substitution

Hello, Is it possible to embed the command output to the replacement of sed? Say I need to replace all the blank lines with random strings: input: ACCTTCGTCTTCTGG GCTTGAGATGGTCCA GCAGGGCTAGTGACG GACGAGTCTCTTGAC ACCAAATCAAAGATCand output is: >26aa36d934d44f06d15b3aab4645a602 $(date |... (9 Replies)
Discussion started by: yifangt
9 Replies

3. UNIX for Dummies Questions & Answers

Can someone please explain sed -n '/^$/!{s/<[^>]*>//g;p;}' filename

I came across this sed expression, and it does exactly what I want. However I haven't got the faintest clue how it does it and thus do not feel capable of using it. Can someone please explain how this expression works? (I used it to remove html tags in a html file I was converting to text) ... (3 Replies)
Discussion started by: maximus73
3 Replies

4. Shell Programming and Scripting

Using sed '${linenum}d' $filename in shell

Hi Experts, Please let me know how to below situation: Looking for: sed "${linenum}d" $filename > $filename _Without_7C Working Command: sed "${linenum}d" A020820122200.tis2webGMNA001_GDS_DIDOut.txt > A020820122200.tis2webGMNA001_GDS_DIDOut.txt_Without_7C appriciate if you can... (7 Replies)
Discussion started by: rajubollas
7 Replies

5. Shell Programming and Scripting

setting variable value to dynamic sed match - escaping hell

Hello All, I'm trying to write a script that will perform a dynamic match (of a dynamic variable) and set a variable to have the resulting (match) value. The idea is that the environment variable to check ($1) and the regular expression to use ($2) are given as parameters. For example,... (5 Replies)
Discussion started by: aedgar
5 Replies

6. Shell Programming and Scripting

h=`sed -n '57p' filename`

sed -n '57p' filename My file contains 57 lines ,the above command prints the 57 th line,but i want to assign the result of this to a variable, to compare with another line in a different file suggest me some idea if i directly use this to assign with variable like h=sed -n '57p' filename... (2 Replies)
Discussion started by: ragavendar
2 Replies

7. Shell Programming and Scripting

Dynamic filename in awk

Hi The following code seems to work, but why am i getting an error message? cscyabl@comet:(develop)> awk 'BEGIN {FS="|"}{print $2 >> $1}' test.sum awk: A print or getline function must have a file name. The input line number is 8. The file is test.sum. The source line number is 1. ... (2 Replies)
Discussion started by: Indalecio
2 Replies

8. Shell Programming and Scripting

sed - dynamic search and replace

Hi all, I have a data file formatted as in the following line: Achadd 0:35 1:35 2:35 3:40 4:40 5:40 I need the minutes converted to seconds; I wrote a script, min2sec, to do so for one datapoint. I was hoping to use sed as in the following code to call this script and... (4 Replies)
Discussion started by: x-375HK-x
4 Replies

9. Shell Programming and Scripting

sed X amount of times - X is dynamic

I'm trying to make a bash shell script that will allow a user to modify another file based on input they give. Maybe someone can see what I'm doing wrong here. I'm still pretty new at this... Let's say my temp file contains this: 0 1 HELLO 3 4 And here's the code: old=(0 1 3 4) new=(zero... (2 Replies)
Discussion started by: Loriel
2 Replies

10. Shell Programming and Scripting

sed / shell - how to use $filename

I've forgotten how to use a filename parameter using sed inside a shell script. What I want to do is replace a string inside a file with the name of the file being processed. I think this should work .. for filename do sed -e "s/xxx/$filename/" ... ... done Thanks! Ps: ... (5 Replies)
Discussion started by: eadie
5 Replies
Login or Register to Ask a Question