gopsman
December 3, 2007, 12:55am
1
Hi
I have to do the following.I have to format a file test.t
The contents are
# Header 1231
xyz
asdf
asdapsdcsd
//string1 = "pqrs"
//string1 = "abcd"; //info
//string1 = "*pqrs"; //add
string1 = "#123 "; //sub
//string1 = "#1234567890 "
data = check(string1)
//string1 = "#1234567890 "
//string2 = "pqrs"
//string2 = "abcd"; //info
//string2 = "*pqrs"; //add
string2 = "#123 "; //sub
print "abcd ";
return xy
I want to modify this as
# Header 1231
xyz
asdf
asdapsdcsd
string1 = "#987 ";
data = check(string1)
string2 = "#21 43 65 87 59";
print "abcd ";
return xy
The requirement is I have to remove all the lines starting with string1 or //string1 or string2 or //string2 and add two new lines
string1 = "#987 "; //mult and string2 = "#21 43 65 87 59"; //sub
in its place. How do i do that using a shell script?
"#987 is a default string , but 21 43 65 87 59 is input to the file as
1234567895,ie, if the script is test.sh, i would call that as
./test.sh 1234567895. and i should get this output.
Please help..
vino
December 3, 2007, 1:10am
2
Very similar to your other thread - sed related query
Please be aware that you are breaking the rules. Refrain from doing so.
vino
December 3, 2007, 1:14am
3
Remove all string1 or string2 pattern as
sed -e "/^[\/]*string[12]/d" input.txt
hi thanks
I know that it is a similar query. just that could not solve this!
earlier itried to break down the requirements. but any idea how to do that?
how can i call a script by passing some argument and format the same in the script and use it again?
vino
December 3, 2007, 1:46am
5
Perhaps something like this
#! /bin/sh
rev=$(echo $1 | sed -e 's/\(.\)\(.\)/\2\1 /g')
str1='string1 = "#987";'
str2='string2 = "#'$rev';'
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1\n\1\n$str2/g" input.txt
vino:
Perhaps something like this
#! /bin/sh
rev=$(echo $1 | sed -e 's/\(.\)\(.\)/\2\1 /g')
str1='string1 = "#987";'
str2='string2 = "#'$rev';'
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1\n\1\n$str2/g" input.txt
Hi Vino
I am getting error here
Syntax error at line 3: `rev=$' unexpected
vino:
Perhaps something like this
#! /bin/sh
rev=$(echo $1 | sed -e 's/\(.\)\(.\)/\2\1 /g')
str1='string1 = "#987";'
str2='string2 = "#'$rev';'
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1\n\1\n$str2/g" input.txt
Hi Vino
I am getting the output in a single line as
string1 = "#987 ";ndata = check(string1);nstring2 = "#21 43 65 87 59";
instead of getting in three lines. "n" is added to each word.
How to implement newline functionality?
vino
December 3, 2007, 4:22am
9
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1\\n\1\\n$str2/g" input.txt
gopsman
December 3, 2007, 6:00am
10
Hi Vino
Still the result is the same.coming in a single line with n added to each word.!!
vino
December 3, 2007, 6:18am
11
It works well for me.
[~]$ cat try
#! /bin/sh
rev=$(echo $1 | sed -e 's/\(.\)\(.\)/\2\1 /g')
str1='string1 = "#987";'
str2='string2 = "#'$rev';'
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1\\n\1\\n$str2/g" /tmp/try
[~]$ ./try 123456789
#Header 1231
xyz
asdf
asdapsdcsd
string1 = "#987";
data = check(string1)
string2 = "#21 43 65 87 9;
print "abcd ";
return xy
[~]$
Try this
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1
\1
$str2/g" /tmp/try
Each of those lines are actual new lines.
gopsman
December 3, 2007, 7:03am
12
vino:
It works well for me.
[~]$ cat try
#! /bin/sh
rev=$(echo $1 | sed -e 's/\(.\)\(.\)/\2\1 /g')
str1='string1 = "#987";'
str2='string2 = "#'$rev';'
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1\\n\1\\n$str2/g" /tmp/try
[~]$ ./try 123456789
#Header 1231
xyz
asdf
asdapsdcsd
string1 = "#987";
data = check(string1)
string2 = "#21 43 65 87 9;
print "abcd ";
return xy
[~]$
Try this
sed -e "/^[\/]*string[12]/d" -e "s/\(data = .*\)/$str1
\1
$str2/g" /tmp/try
Each of those lines are actual new lines.
Hi
I tried the second approach. Getting this error
UX:sed: ERROR: Command garbled: s/\(data = .*\)/string1 = "#987 #";
vino
December 3, 2007, 7:16am
13
Post the output of uname -a and sed --version