Help with substitution in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with substitution in shell script
# 1  
Old 01-13-2012
Help with substitution in shell script

Hello,

I Have a file like this, with five columns:
Code:
B D F T L
---------
0 0 0 0 0
0 0 0 1 0
0 2 0 0 1

I need, for each row, to append another five columns similar to the first ones, but with the field labeled as "T" (4th field of the column) substituted in this way:

(16 * value of Fifth field) + value of Fourth field = expr \( 16 \* ${L} \) + ${T} and leave the value of Fifth field as "0".

An example of the output I want:

Code:
B D F T L -----        B D F T  L
--------------------------------
0 0 0 0 0         ----- 0 0 0 0  0
0 0 0 1 0          ----- 0 0 0 1  0
0 2 0 0 1          ----- 0 2 0 16 0

I don't know how to do the substitution. Any help will be appreciated.
(The "-" are for separating columns; in the post, with spaces instead, it is seen all joined)


Best Regards,

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data
# 2  
Old 01-13-2012
Welcome to the forum

With awk

Code:
awk '{printf $0 FS; $4=16*$5+$4;$5=0;printf $0 RS}' file

# 3  
Old 01-13-2012
Hi,

Thank you for your response, but it does not work:

Code:
host1:/:awk '{printf $0 FS; $4=16*$5+$4;$5=0;printf $0 RS}' file
B D F T L B D F 0 0
--------- ---------   0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 1 0
0 2 0 0 1 0 2 0 16 0
host1:/:

Best Regards,
# 4  
Old 01-13-2012
The first two lines are the header part? need to ignore that.

Code:
awk 'NR<=2 {printf $0 FS $0 RS} NR>2{printf $0 FS; $4=16*$5+$4;$5=0;printf $0 RS}' file

O/P
Code:
B D F T L B D F T L
--------- ---------
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 1 0
0 2 0 0 1 0 2 0 16 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bad substitution error in shell script

I have script data.sh which has following error. Script Name : data.sh #!/bin/sh infile=$1 len=${#infile} echo $len texfile=${infile:0:$len-4} echo $texfile run command ./data.sh acb.xml I get following error message: (5 Replies)
Discussion started by: man4ish
5 Replies

2. Shell Programming and Scripting

[SCRIPT] read and substitution

Hi all, Actually I have a huge bash file like: port1=$PORT && if ; then ..... ....stuff port2=$PORT && if ; then ..... ...stuff port3=$PORT && if ; then ..... ...stuff port4=$PORT && if ; then ..... ...stuff port5=$PORT && if ; then ..... I would like to change the $port inside... (2 Replies)
Discussion started by: Dedalus
2 Replies

3. Shell Programming and Scripting

Shell $,/r getting added in echo on variable substitution.

Hi All, I'm trying to run a similar script to copy a files from one location to another. #!/bin/bash source="/home/pradeepk/a.txt" destination="/home/pradeepk/dir1" cp $source $destinationi'm getting following error. cp: cannot stat `/home/pradeepk/a.txt\r': No such file or directorywhen... (1 Reply)
Discussion started by: pradeep2002gs
1 Replies

4. UNIX for Dummies Questions & Answers

sed substitution and shell variables

Hello!! Am trying to substitute the value of a shell variable with the value of another shell variable. The values are obtained into the shell variables through some other processing. for ex. i've used the follow sed command.. sed "s/$var1/$var2/g" i've also tried the other... (5 Replies)
Discussion started by: a_ba
5 Replies

5. Solaris

Get file name in shell scrip loop: bad substitution

Hi guys. Good day, morning, afternoon or night, depending on where you live. I have a script shell in which I am looping on files (absolute path) see code section above. I always have an error: bad substitution. :wall: Is it because my variable file is the index of the loop and not a normal... (4 Replies)
Discussion started by: soueric
4 Replies

6. Shell Programming and Scripting

Using C-Shell Scripting for Substitution

mv myFile.txt myFile.txt.bak sed s/foo/bar/g myFile.txt.bak > myFile.txt Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three parameters: the string to be replaced the string with which to replace it the name of the file in which to make the... (1 Reply)
Discussion started by: RogerW
1 Replies

7. Shell Programming and Scripting

Shell scripting substitution techniques

Hi guys, I'm looking for some thoughts on this. I'm trying to do a clean 1 liner to substitute some values. What I have: sed 's/Personid=.*/Personid=xxxxxx/' $tmpFileOut sed 's/Person:.*/Person:xxxxxx/' $tmpFileOut sed 's/PersonID:.*/PersonID: xxxxxx/' $tmpFileOut Obviously that's a bit... (1 Reply)
Discussion started by: rich@ardz
1 Replies

8. Shell Programming and Scripting

Bad substitution errors in shell script

Hello, I was hoping for a second pair of eyes or a little bit of help figuring out what my error is in a script. I did some searching in the forums and didn't find anything so please forgive me if it a similar problem has been discussed before. My script accepts normal user arguments; however,... (2 Replies)
Discussion started by: Jackinthemox
2 Replies

9. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

10. UNIX for Advanced & Expert Users

shell variables and sed substitution

My specific goal: automatically edit a Makefile variable's (EXTRAVERSION) value in a kernel Makefile. So, I have a shell script that takes one parameter, a version string: buildkernel.sh 2.6.18.21.7-custom In the script, I assign the parameter to K_VER: K_VER="2.6.18.21.7-custom"... (2 Replies)
Discussion started by: duderonomy
2 Replies
Login or Register to Ask a Question