Passing UNIX commands inside sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing UNIX commands inside sed
# 1  
Old 02-28-2009
Question Passing UNIX commands inside sed

Hi,

In sed, is it possible to match patterns by directly executing UNIX commands inside sed?

For e.g. -
sed "s/`cat file.txt | cut -d "|" -f2`/replace_string"

Will the above command work?

My expectation is to search for the second field in file.txt (file delimited by | ) and replace those with replace_string.

I know I could store the output of cat file.txt | cut -d "|" -f2 in a variable and pass it to sed. But I want to know if direct command substitution is possible

Any help is greatly appreciated.

-Thanks
# 2  
Old 02-28-2009
Yes, you can directly substitute commands into strings this way, though there may be quoting issues for strings in strings in strings.
# 3  
Old 02-28-2009
Quote:
Originally Posted by devanathann
Hi,

In sed, is it possible to match patterns by directly executing UNIX commands inside sed?

For e.g. -
sed "s/`cat file.txt | cut -d "|" -f2`/replace_string"

Will the above command work?

Yes, so long as there is only one line in file.txt.
Quote:
My expectation is to search for the second field in file.txt (file delimited by | ) and replace those with replace_string.

I know I could store the output of cat file.txt | cut -d "|" -f2 in a variable and pass it to sed. But I want to know if direct command substitution is possible

By using a variable, you can eliminate two external commands, cat and cut:

Code:
IFS='|' read a b c < file.txt
sed "s/$b/replace_string/"

# 4  
Old 02-28-2009
Quote:
Originally Posted by devanathann
Hi,

In sed, is it possible to match patterns by directly executing UNIX commands inside sed?

For e.g. -
sed "s/`cat file.txt | cut -d "|" -f2`/replace_string"

Will the above command work?
No, it should not work that way, to replace the second column in a pipe delimited file with a shell variable in sed you can do something like this:

Code:
sed "s/\([^|]*\)|[^|*]|\(.*\)/\1|$var|\2/" file > newfile
mv newfile file

With awk:

Code:
awk -F"|" '{$2=s}1' s=$var OFS="|" file > newfile
mv newfile file

Regards
# 5  
Old 02-28-2009
Quote:
Originally Posted by Franklin52
No, it should not work that way

Why not? It works for me (after terminating the s command):

Code:
$ cat ~/txt
a|b|c|d
$ printf "%s\n" abc vbn |
  sed "s/`cut -d "|" -f2 ~/txt`/replace_string/"
areplace_stringc
vreplace_stringn


Last edited by cfajohnson; 02-28-2009 at 08:38 PM.. Reason: Removed UUOC
# 6  
Old 02-28-2009
Quote:
Originally Posted by cfajohnson

Why not? It works for me (after terminating the s command):

Code:
$ cat ~/txt
a|b|c|d
$ printf "%s\n" abc vbn |
  sed "s/`cat ~/txt | cut -d "|" -f2`/replace_string/"
areplace_stringc
vreplace_stringn

Quote:
My expectation is to search for the second field in file.txt (file delimited by | ) and replace those with replace_string.
The file has more then one line and the output should be:

Code:
a|replace string|c|d

Regards
# 7  
Old 02-28-2009
Quote:
Originally Posted by Franklin52
The file has more then one line

As I pointed out, it would only work if the file containing the search term contained only one line. The script doesn't make any sense if it has more than one line.
Quote:
and the output should be:

Code:
a|replace string|c|d

Regards

No, it shouldn't. The file containing "a|b|c|d" is where the search string is coming from, not the file that sed is operating on.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing parameter inside the expression

Hello, i need to pass the variable in place of pwd. how to display variable in the bleow syntax. suppose, passwd="test", then 'id/$passwd..... FEDFlagResult=`sqlplus -S 'id/pwd@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1643))(CONNECT_DATA= (SID=peta1)))'<< EOF select... (4 Replies)
Discussion started by: balareddy
4 Replies

2. Shell Programming and Scripting

Trouble passing commands with expect

Hello All, I hope someone could help me with this. I'm creating a shell script to run a process. The trouble is, part of the process has to be ran as a different user. I can 'su' to the user ok, but I'm having trouble passing a 'cd' command as well as some variables I set earlier in the... (1 Reply)
Discussion started by: bbbngowc
1 Replies

3. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

4. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

5. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

6. Shell Programming and Scripting

Using commands inside sed substitution

Dear Friends, Please let me know how to use the date command inside the substitution flag replacement string. echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/' I want to supply \1 string to Here mention below as part of replacement string, date -d <Here>... (5 Replies)
Discussion started by: tamil.pamaran
5 Replies

7. Shell Programming and Scripting

Passing unix command to "sed"

I am trying a simple sed to replace a pattern but it fails ,,c an someone help in where iam wrong, here is what i run and i get no o/p a=`cat abc.sh | grep -i pattern1 | egrep -iv "pattern2" ` sed 's/`echo $a`/XYZ=newvalue/g' < abc.sh > abc.sh_new This is how my ksh -x of teh script looks... (2 Replies)
Discussion started by: yesmani
2 Replies

8. Shell Programming and Scripting

Unix commands failing inside the shell script

When my script deals with large input files like 22Gb or 18 GB the basic commands like sort or join fails when run from inside the shell scripts. Can there be any specific reason for this? For e.g. sort -u -t "," -k1,1 a.csv > a.csv.uniq" sort -u -t "," -k1,1 b.csv > b.csv.uniq" The... (3 Replies)
Discussion started by: esha
3 Replies

9. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies

10. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies
Login or Register to Ask a Question