Can sed use a file on the syntax?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Can sed use a file on the syntax?
# 1  
Old 12-18-2014
Can sed use a file on the syntax?

Dear all,

I need help, again.

I would like to use a sed on a for. Is is possible to ask sed to call a file in the syntax?

For exemple:

Code:
   sed "/Y/ s/number/X/" test_imput > test_output

where Y is a file which inside there is one pattern only; Also X is a file with one pattern only.

This files will change during the for loop.

Thank you in advance,

Leandro

Last edited by Don Cragun; 12-18-2014 at 11:18 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-18-2014
What you're asking for should be possible with something like:
Code:
sed "/$(cat Y)/ s/number/$(cat X)/" test_imput > test_output

but since you haven't said what OS or shell you're using, we can only guess at the syntax your shell uses for command substitution.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-19-2014
Dear Don Cragun,

Thank you for your help. Sorry, I am using CentOS release 6.6 server and the SHELL is 4.1.2(1)-release.

Code:
 for line in $(cat list.txt)
do
 grep $line code_to_value.txt | cut -c18 > value_1
 grep $line code_to_value.txt | cut -c22 > value_2

 paste -d "" value_1 value_1 > code_1
 paste -d "" value_1 value_2 > code_2
 paste -d "" value_2 value_2 > code_3

grep -n $line table_values.txt | cut -d : -f1 > line

sed "/$(cat line)/ s/1/$(cat code_1)/g" table_values.txt > test_output
done

I am getting this error.

sed: -e expression #1, char 0: no previous regular expression

Here is a better exemple of the data:

Code:
PT_1 1 1 1 1 1 1 2 2 1 1 2 
PT_2 2 2 3 1 2 3 2 2 1 3 1
PT_3 3 2 3 2 3 3 2 1 1 2 3
PT_4 3 2 1 2 3 1 3 2 1 1 2

Thanks very much for your attention!
Leandro

Last edited by lColli; 12-19-2014 at 12:41 AM..
# 4  
Old 12-19-2014
Quote:
Originally Posted by lColli
Dear Don Cragun,

Thank you for your help. Sorry, I am using CentOS release 6.6 server and the SHELL is 4.1.2(1)-release.

Code:
 for line in $(cat list.txt)
do
 grep $line code_to_value.txt | cut -c18 > value_1
 grep $line code_to_value.txt | cut -c22 > value_2

 paste -d "" value_1 value_1 > code_1
 paste -d "" value_1 value_2 > code_2
 paste -d "" value_2 value_2 > code_3

grep -n $line table_values.txt | cut -d : -f1 > line

sed "/$(cat line)/ s/1/$(cat code_1)/g" table_values.txt > test_output
done

I am getting this error.

sed: -e expression #1, char 0: no previous regular expression

Here is a better exemple of the data:

Code:
PT_1 1 1 1 1 1 1 2 2 1 1 2 
PT_2 2 2 3 1 2 3 2 2 1 3 1
PT_3 3 2 3 2 3 3 2 1 1 2 3
PT_4 3 2 1 2 3 1 3 2 1 1 2

Thanks very much for your attention!
Leandro
What do you mean, this is a better example of the data? Your script has three input files: list.txt, code_to_value.txt, and table_values.txt. Is this example of the data an example of one of these input files? Is it an example of the output you're trying to produce?

From your sample code, we can see that there are several things that could be made more efficient; but without knowing what you're trying to do, we can easily produce efficient code that doesn't do anything at all like what you want to do. Why do you have a loop, if you only want to keep the output produced by the last time through the loop?

Please help us help you. Show us sample data for each of your three input files. And, given those sample input files, what output are you trying to produce in test.output?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-19-2014
Dear Don Cragun,

Ok, so, input files:

list.txt ## this is just a copy of first column of table_values.txt that I use on for loop
Code:
PT_0000000001 
PT_0000000002
PT_0000000003
PT_0000000004

table_values.txt ## Main table file
Code:
PT_0000000001 1 1 1 1 1 1 2 2 1 1 2 
PT_0000000002 2 2 3 1 2 3 2 2 1 3 1
PT_0000000003 3 2 3 2 3 3 2 1 1 2 3
PT_0000000004 3 2 1 2 3 1 3 2 1 1 2

code_to_value.txt ## Table for decoding table_values.txt (not in the same order of table_values.txt)
Code:
PT_0000000001 A G
PT_0000000002 G T
PT_0000000004 A C
PT_0000000003 C G


So, I want to go line per line of table_values.txt and replace 1, 2 or 3 buy the combination of alleles on code_to_value.txt.
The code is: 1 means 2 x first allele (second column), 2 means second column + third column, 3 means 2x third column (for each PT_*)
For exemple, for PT_0000000001 1 means AA, 2 AG and 3 GG. While for PT_0000000002 1 means GG, 2 GT and 3 TT.

So the final output I am trying is:
Code:
PT_0000000001 AA AG AA AA AA AA AG AG AA AA AG 
PT_0000000002 GG GT TT GG GT TT GT GT GG TT GG
PT_0000000003 GG CG GG CG GG GG CG CC CC CG GG
PT_0000000004 AC AC AA AC CC AA CC AC AA AA AC

Thank you and sorry not be clear previously.

Last edited by lColli; 12-19-2014 at 01:54 AM..
# 6  
Old 12-19-2014
Since your code_to_value.txt file only contains 17 characters per line (not counting the terminating <newline> character), the cut calls creating the files value_1 and value_2 always produce empty files. So, even if the calls to paste work, the files code_1, code_2, and code_3 will also always be empty files.

When I run your code on OS X, I get errors from all of your invocations of paste instead of errors from sed. Bur rather than invoking cat nine times, cut, grep, and paste twelve times, and sed four times; the following script using a single invocation of awk seems to do what you said you wanted done (without using the file list.txt).

Code:
awk '
FNR == NR {
	list[$1] = ++listc
	code[listc, 1] = $2 $2
	code[listc, 2] = $2 $3
	code[listc, 3] = $3 $3
	next
}
{	for(i = 2; i <= NF; i++)
		$i = code[list[$1], $i]
	print
}' code_to_value.txt table_values.txt > test_output

With your sample code_to_value.txt and table_values.txt input files, it produces the output:
Code:
PT_0000000001 AA AA AA AA AA AA AG AG AA AA AG
PT_0000000002 GT GT TT GG GT TT GT GT GG TT GG
PT_0000000003 GG CG GG CG GG GG CG CC CC CG GG
PT_0000000004 CC AC AA AC CC AA CC AC AA AA AC

rather than the output you said you wanted:
Code:
PT_0000000001 AA AG AA AA AA AA AG AG AA AA AG 
PT_0000000002 GG GT TT GG GT TT GT GT GG TT GG
PT_0000000003 GG CG GG CG GG GG CG CC CC CG GG
PT_0000000004 AC AC AA AC CC AA CC AC AA AA AC

If I understand your requirements correctly, I believe the output shown in red in the output you said you were trying to get is inconsistent with your requirements.

Note that if you input files have Windows <carriage-return><line-feed> line terminators instead of UNIX and Linux <newline> line terminators, you'll need to change the line:
Code:
awk '

in the above script to something like:
Code:
awk '
{	gsub(/\r/, "")
}

to get rid of the extraneous <carriage-return> characters.

I hope this helps.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 12-19-2014
Dear Don Cragun,

The script worked perfectly! Thank you so much for the help and patience.

I will try to learn awk.

Best, Leandro
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another sed Syntax Puzzle . . .

Greetings! Have a quick question for the community today; this time looking at a nifty little sed puzzle ;) Consider the following file content to be worked through:What needs to happen is theblock should be removed up to and including the following blank line, leavingI have bits and pieces... (8 Replies)
Discussion started by: LinQ
8 Replies

2. Shell Programming and Scripting

sed syntax error

Hi, In the following excerpt of shell script code: I could not understand the sed syntax. Could anyone shed some light on this? configure_ssl() { jboss_conf_file=$1 echo "Configuring SSL for -" ${jboss_conf_file} isSSLSetup=`echo cat ${jboss_conf_file} | grep <Connector... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

sed s/// syntax help

<tr><td width=10% style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>Package</b></td><td width=10% valign=center style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>JTs</b></td> This is got to be simple. I run this on the above .html file: sed... (8 Replies)
Discussion started by: dba_frog
8 Replies

4. UNIX for Dummies Questions & Answers

sed - need help for syntax

Hello, here is what I've got : FILE='/OPERATIONNEL/SATURNE/CHAMPS/MASTER/ANA/SATURNE_1DAV_20080805_20080806_S3D_T_R20080806.NC ';;... (4 Replies)
Discussion started by: Aswex
4 Replies

5. Shell Programming and Scripting

Explain following sed syntax please

Thanks to this forum I have managed to work out a solution to my problem and actually understand most of it, but one thing is confusing me and I am sure someone here can explain. I need to insert a piece of txt into a file. This txt is awk '{ sub(/$/,"\r"); print }' $JCL_WBB50103_EFTOUT >... (2 Replies)
Discussion started by: hukcjv
2 Replies

6. Shell Programming and Scripting

Obscure sed extraction syntax

Hi, Could anyone clearly explain me the below sed construct in detail to get to know what it actually does? sed 's/\(* *\)//4' echo 'test;10;20' | sed 's/*;\(*\)/\1/' (1 Reply)
Discussion started by: royalibrahim
1 Replies

7. Shell Programming and Scripting

What syntax to use with sed c\

I know that I want to entirely replace line 3 in my file filename.txt. I have tried all sorts of variations of sed 3,3,c\replacement stuff\ filename.txt with no success. about the only thing that causes any reaction is sed 3,3c\\ filename.txt but it just prints out the whole file. ... (13 Replies)
Discussion started by: SusanDAC
13 Replies

8. Shell Programming and Scripting

syntax for variables in sed

I always kind of wondered this but I have a variable that I want to use in a search and replace. Basically I want to search a file for the string in my variable and replace it with something fixed but I'm unsure of the variable rule in sed. Here's generally what I have: sed 's/$name/newname/g'... (15 Replies)
Discussion started by: eltinator
15 Replies

9. Shell Programming and Scripting

Help for Sed Syntax problem

I have one File named "txt_file" # cat txt_file <DBType>RT</DBType> <AppType>RT</AppType> -------------------------------------------------- I want replace "<AppType>RT</AppType>" to <AppType>XY</AppType> in txt_file and output redirect to Newfile ... (2 Replies)
Discussion started by: SanjayLinux
2 Replies

10. UNIX for Dummies Questions & Answers

sed syntax

Hi, How can i use sed command to modify a part of a variable containing "/" by another containing "/" like describe below: VAR="/app/share/eai" VAR1="/app/share" VAR2="/data/test" echo $VAR | sed 's/... ??? # using sed to replace $VAR1 in $VAR by $VAR2 ? (4 Replies)
Discussion started by: jo_aze
4 Replies
Login or Register to Ask a Question