Echo printing a line in 2 lines; expected to print in one line

Dear All,

fileName: therm.txt

nc3h7o2h   7/27/98 thermc   3h   8o   2    0g   300.000  5000.000 1390.000    41 
 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14    2 
-2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05    3 
 7.98979513e-09-9.39626069e-13-2.54265832e+04 1.79683851e+01                   4 
nc3h7o2    7/27/98 thermc   3h   7o   2    0g   300.000  5000.000 1387.000    31 
 1.23635662e+01 1.69377420e-02-5.80705297e-06 9.02805946e-10-5.24229836e-14    2 
-1.19482794e+04-3.57854035e+01 3.15306348e+00 3.52691052e-02-1.90649122e-05    3 
 5.01988372e-09-5.10606304e-13-8.35295454e+03 1.49406763e+01                   4 
c3h6ooh1-3 7/27/98 thermc   3h   7o   2    0g   300.000  5000.000 1388.000    41 
 1.45796713e+01 1.48373142e-02-5.13088443e-06 8.02513899e-10-4.68026484e-14    2 
-5.08733997e+03-4.60293044e+01 3.02841453e+00 3.90930362e-02-2.35990175e-05    3 
 6.75244193e-09-7.05929372e-13-7.99302348e+02 1.69822279e+01                   4 
c3h6ooh1-2 7/27/98 thermc   3h   7o   2    0g   300.000  5000.000 1381.000    41 
 1.43342402e+01 1.50496617e-02-5.20515993e-06 8.14227552e-10-4.74899437e-14    2 
-6.33723421e+03-4.47976591e+01 4.08359443e+00 3.40422329e-02-1.62692689e-05    3 
 2.38765400e-09 2.21650818e-13-2.27965073e+03 1.20159683e+01                   4

sedFile.sh

sed -i 's/"c3h6ooh1-2"/"LONG0001 "/g' therm.txt

What I am trying to do is simple sed operartion to replace text "c3h6ooh1-2" with "LONG0001". Strangely it is not happening.

Can somebody have a look where I am doing wrong?

Thanks & Regards,
linuxUser_

The sed command is attempting to substitute the string "c3h6ooh1-2" with "LONG0001 " , but it doesn't find that string, because you've put the string c3h6ooh1-2 inside quotes.

Try

sed -i 's/c3h6ooh1-2/LONG0001 /g' therm.txt

---------- Post updated at 06:49 AM ---------- Previous update was at 06:38 AM ----------

Edit: Consider adding a second whitespace at the end of the replacement string LONG.., e.g.

sed -i 's/c3h6ooh1-2/LONG0001  /g' therm.txt
                              ^

so a 10 character string gets replaced by a 10 character string, otherwise the indentation will change.

1 Like

What junior-helper suggested should make your sed command work. But, this thread is titled: "Echo printing a line in 2 lines; ecpected to print in one line". I don't see any echo commands and I don't see any indication that extraneous <newline>s are being added by anything you've shown us.

Is there something else we're missing here?

1 Like

now I am able to sed, Thanks a lot.
I have another issue.
content of my file:

al                 62987al  1               g  0300.00   5000.00  0600.00      1 
 0.02559589e+02-0.01063224e-02 0.07202828e-06-0.02121105e-09 0.02289429e-13    2 
 0.03890214e+06 0.05234522e+02 0.02736825e+02-0.05912374e-02-0.04033938e-05    3 
 0.02322343e-07-0.01705599e-10 0.03886795e+06 0.04363880e+02                   4 
al2h6              62987al  2h   6          g  0300.00   1500.00  0600.00      1 
 0.02634884e+02 0.02135952e+00 0.03154151e-05-0.07684674e-07 0.02335832e-10    2 
 0.08871346e+05 0.09827515e+02-0.06800681e+02 0.05080744e+00 0.01039747e-03    3 
-0.01119582e-05 0.08459155e-09 0.01060537e+06 0.05554526e+03                   4

similar to previous sed commands I wanted to sed "al" and " al2h6" with some other word like LONG0003 say ...
but problem is sed will replace all "al"s
How can I get-ride of this situation?

Thanks & Regards,
linuxUser_

I assume you used something like sed 's/al/LONGsomething/g' file
The "problem" is /g means global replacement, thus sed will attempt to replace all occurences of the al string, no matter where in the line.

If you remove g from the sed command like this sed 's/al/LONG/' file sed will only replace the first occurence of al .
By using a caret ^ , you explicitely tell sed to match al at the beginning of the line:

sed 's/^al/LONG/' file

The above would *almost* do what you want... Why almost? It will replace all al 's which are at the beginning of the line, so al gets LONG , and al2h6 gets LONG2h6 You get the idea.

THIS is what you want for al (it acts like replace al only found as single string, not as part of some other string too)

sed 's/^al /LONG0003/' file

and this for al2h6

sed 's/^al2h6 /LONG0003/' file
1 Like

ljunior-helper has already pointed out some of the issues with your last post. Let me expand on that a little bit.

No. A sed substitute command will replace the text you tell it to match with the text you tell it to substitute. It will only replace all instances of al if you tell it to replace all instances of al .

If you don't want it to replace all instances of al , describe more precisely what you want to match and what text you want to use to replace the matched text.

Your latest sample input file contains four instances of al and one instance of al2h6 . One of those instances of al is at the start of a line and is followed by six spaces. One instance of al2h6 is at the start of a line and is followed by 3 spaces.

Can you precisely define what you want to match and what text you want to replace the text you match?

From your last request, I would guess that you don't want to replace al or al2h6 , but instead want to replace eight characters at the start of a line starting with al with the eight replacement characters LONG0003 . If that is what you want, that would be something more like:

sed 's/^al....../LONG0003/' file

Please stop making us guess at what you want and give precise details about what you are trying to do.

1 Like

Try this construct with sed...
sed 's/^\<al\>/LONG/' test.txt

1 Like

Dear All,

Problem solved :slight_smile:
Thanks a lot for help.

Regards,
linuxUser_