Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo printing a line in 2 lines; expected to print in one line
# 1  
Old 12-02-2014
Echo printing a line in 2 lines; expected to print in one line

Dear All,

fileName: therm.txt
Code:
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
Code:
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_
# 2  
Old 12-02-2014
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
Code:
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.
Code:
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.
This User Gave Thanks to junior-helper For This Post:
# 3  
Old 12-02-2014
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?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 12-02-2014
now I am able to sed, Thanks a lot.
I have another issue.
content of my file:
Code:
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_
# 5  
Old 12-02-2014
Quote:
> but problem is sed will replace all "al"s
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.

Quote:
> How can I get-ride of this situation?
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:
Code:
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)
Code:
sed 's/^al /LONG0003/' file

and this for al2h6
Code:
sed 's/^al2h6 /LONG0003/' file

This User Gave Thanks to junior-helper For This Post:
# 6  
Old 12-02-2014
Quote:
Originally Posted by linuxUser_
... ... ...
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_
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 12-02-2014
Try this construct with sed...
sed 's/^\<al\>/LONG/' test.txt
This User Gave Thanks to ongoto For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

3. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

4. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

5. Shell Programming and Scripting

Printing multiple lines on the same line between specific text

This is an extract from a large file. The lines that start with fc are ports on a fabric switch. In between each fc port there is information about the port. fc2/12 is up Port description is SEIEDISCOVER-3 Speed is 4 Gbps fc2/13 is down (Administratively down) fc2/14 is up Port... (1 Reply)
Discussion started by: kieranfoley
1 Replies

6. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

7. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

8. Shell Programming and Scripting

print - for printing whole line, but delimeters are changed to spaces

Hi consider the source file R|field1|field2 using the print statement in awk prints it as R field1 field2 Is there an easier way to print the whole line in its original format (i.e. with | delimiters) than doing print $1"|"$2"|"$3 ?? Thanks Storms (1 Reply)
Discussion started by: Storms
1 Replies

9. Shell Programming and Scripting

Print (echo) variable in a single line

Hi, I have written this code ------------------------------------------------ # !/bin/ksh i=0 while do j=$i while do echo -e $j #printf "%d",$j j=`expr $j - 1` done echo i=`expr $i + 1` done ---------------------------------------------------- The ouput which... (2 Replies)
Discussion started by: rac
2 Replies

10. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

Featured Tech Videos