sed and variable as target file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed and variable as target file
# 1  
Old 09-04-2013
sed and variable as target file

Code:
for file in `ls /tmp/*_sw_list`; do
  /usr/bin/sed -i '' '1,/^Software\ Update/d' $file
done

In my script, this doesn't work. I can copy-and-paste it, and it works. Enabling debugging shows it is resolving the file name correctly... it isn't an issue with special characters in the filename. It returns an error code of 0, but doesn't actually modify the file. Permissions on the file are 644.
# 2  
Old 09-04-2013
This is a useless use of ls *.

Piping the * through ls accomplishes nothing you couldn't have accomplished without it, except possibly scrambling any filenames with whitespace in them -- backticks split across all whitespace, not newlines.

Code:
for FILE in /tmp/*_sw_list
do
        /usr/bin/sed -i '' '1,/^Software\ Update/d' "$FILE"
done

...or even:

Code:
/usr/bin/sed -i '' '1,/^Software\ Update/d' /tmp/*_sw_list

What does the '' do? I don't understand the meaning of the blank expression, but if you say it works...

Why are you escaping a space?
# 3  
Old 09-04-2013
Quote:
Originally Posted by Corona688
Above examples are for '*', to catch EVERYTHING. I'm going to wind up with an unknown number of files in /tmp/ called file_sw_list, file2_sw_list, etc. etc.

Quote:
Piping the * through ls accomplishes nothing you couldn't have accomplished without it, except possibly scrambling any filenames with whitespace in them -- backticks split across all whitespace, not newlines.
It would be a pretty freaky coincidence to wind up with filenames matching my pattern with whitespace... they're generated from unquotes variables. It could happen, and I always like to sanity check as much as possible, but that's pretty far down my list of worries.

Quote:
Code:
for FILE in /tmp/*_sw_list
do
        /usr/bin/sed -i '' '1,/^Software\ Update/d' "$FILE"
done

...or even:

Code:
/usr/bin/sed -i '' '1,/^Software\ Update/d' /tmp/*_sw_list

Neither work. I get the same result.

Quote:
What does the '' do? I don't understand the meaning of the blank expression, but if you say it works...
Two single quotes, not a double quote. I'm performing the operation on a file, and don't want a "backup". BSD sed is picky about this stuff (OS is OSX).

Quote:
Why are you escaping a space?
Because there's a literal space in the pattern I'm matching?

In any case, it turned out to be a second pattern that matched '^Software\ Update' that was so close to the top I didn't notice just a couple of lines missing :-)
# 4  
Old 09-04-2013
Quote:
Originally Posted by jnojr
Above examples are for '*', to catch EVERYTHING. I'm going to wind up with an unknown number of files in /tmp/ called file_sw_list, file2_sw_list, etc. etc.
Look closely, my examples use * too. They are not pseudocode, * does actually work in those places. That's why putting it through ls is redundant.

If you're concerned about * matching too many files and using ls instead because of this, plugging too many files into ls does not make them not be too many files either.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

After adding new iscsi target port, still the session state of that target port is showing offline

Hi, I wanted to configure new iscsi port on HPUX system, i added the target port address and configured it, once done, went to array side and searched for that host iqn number , but was nt able to find the same, came to host, then when i ran "iscsiutil -pVS" command it gave me below result ... (0 Replies)
Discussion started by: Vinay Kumar D
0 Replies

2. Shell Programming and Scripting

sed not outputing variable into new file

Hi all, I have a script that creates folders depending on what variables i enter when calling the script. This all works as expected but what i want to do is add some additional data into another file so that other scripts are aware of this file. I've found the following sed command which... (12 Replies)
Discussion started by: springs2
12 Replies

3. Shell Programming and Scripting

Replace value of a variable in a file through script using sed

Hi, I am trying to replace the value of a variable in a file through another script. Example: Filename : abc.txt contents: a=10 b=20 c=30 Need to change the value of, say, b - Tried using the following: sed "s/${b}/15/g" abc.txt Have tried various forms of sed (with single quotes,... (4 Replies)
Discussion started by: rituparna_gupta
4 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Solaris

Setting/Modifying variable specific to target in Makefile

Hi, I have a small piece of Makefile script which throw's error on Sun Sparc machine, but works fine with Sun Optron, Linux, AIX machines. FOO=Naveen test1:FOO=Dhilip test1: @echo FOO is ${FOO} test2: @echo Me is ${FOO} Output on Sun Sparc - ukhml-v890new-~/test: make test1... (5 Replies)
Discussion started by: nsriram
5 Replies

6. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

7. Shell Programming and Scripting

Having sed to read lines of a file with the use of a variable..Possible?

This i will print the second line of a file sed -n '2p' test2 The use of a variable is impossible here. a=1 while ; do line=`sed -n '$a p' test2` # do some things here with the line variable a=`expr $a + 1` done But the uotput of sed command is 'p'..... What can i do to use a variable... (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

How to get content of a variable into text file (sed)?

Hello, Im working on this problem for 3 days now and i just cant get it to work.. I tried with alot of different sed methods but didnt find any solution. Its proberly verry simple but i just started bash scripting for a month or so.. i have a file called: file.nfo and file.txt the content... (4 Replies)
Discussion started by: atmosroll
4 Replies

9. Programming

Makefile: multiple target variable substitution

Greetings! Basically, I would like to properly handle this with gnu make: alltools: my_tool mysecond_tool mythird_tool etc_tool %_tool: dir1/%_tool.vf dir2/%_tool/subdir2/%_tool.ver <tab>@echo done %.vf: <tab>RUN_VF $* %.ver: <tab>RUN_VER $* So, if I were to do something like:... (0 Replies)
Discussion started by: Harlinator
0 Replies

10. Shell Programming and Scripting

sed -i '7 c\$variable' file ....(?!@#$%^&*!)

I have tried everything I can think of to get sed to change line N of a file to the contents of a variable. I have Googled the Internet, and I find lots of people telling how to use variables with the "Substitute" command, but no one telling how to use variables with the "Change" command. I... (4 Replies)
Discussion started by: Mr.Lauren
4 Replies
Login or Register to Ask a Question