sed Command new Line not working Tried many variations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed Command new Line not working Tried many variations
# 1  
Old 01-19-2014
sed Command new Line not working Tried many variations

Hi

I have assigned an output of a command to $I. I try to print the input and put a new line after occurrence of the hostname which is assigned to $HOST1 ( Example: pwrm16 ) . First of all I need to get rid of the Colon after the host name pwrm16: and make it pwrm16 then I want to print the rest of the line until I hit another hostname. In short I need to get rid of the the colon before the host pwrm16: and then print the line from the string until I get to the next host name . Can someone please help me ? Tried my best and wasted a lot of times


***********************************
I want the output be in this format:


Code:
pwrm16: Info: Thu Dec 12 22:20:23 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition.

pwrm16: Info: Sun Dec 15 21:37:30 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition.

*****************************************

I can get rid of the colon by using tr and reassign it ti the variable. However
I just have not able to print anything on a new line. It just not working. I went on internet and tried everything I could find. Just don't seem able to print anything on a new line. Basically all I get is the entire string.

Current command I'm using ( used so many difference variations )
Code:
printf "$I "| sed 's/$HOST1/\n/g'
$HOST1 = pwrm16


*****************************************

Current output I get is as follow

Code:
pwrm16: Info: Thu Dec 12 22:20:23 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm17: Info: Sun Dec 15 21:37:30 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm18: Info: Wed Dec 18 11:31:43 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm16: Info: Wed Dec 18 14:00:05 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm16: Info: Thu Jan 02 13:51:24 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm19: Info: Fri Jan 17 01:16:26 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm16: Info: Fri Jan 17 03:39:19 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition.

********************************************

Last edited by Scrutinizer; 01-19-2014 at 03:39 PM..
# 2  
Old 01-19-2014
Quote:
Originally Posted by mnassiri
Current command I'm using ( used so many difference variations )
printf "$I "| sed 's/$HOST1/\n/g'
$HOST1 = pwrm16

First, the variable assignment should be:

Code:
HOST1=pwrm16  ## No $ and no spaces around =


Second, you don't need sed; use parameter expansion. For example:

Code:
var1=${i#*: ] ## the contents of the variable after the first ': '
var2=${i%%:*} ## The contents of the variable before the first colon


Last edited by cfajohnson; 01-19-2014 at 03:36 PM..
# 3  
Old 01-19-2014
Power

Try :
Code:
$ cat file
pwrm16: Info: Thu Dec 12 22:20:23 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm17: Info: Sun Dec 15 21:37:30 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm18: Info: Wed Dec 18 11:31:43 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm16: Info: Wed Dec 18 14:00:05 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm16: Info: Thu Jan 02 13:51:24 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm19: Info: Fri Jan 17 01:16:26 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. pwrm16: Info: Fri Jan 17 03:39:19 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition.

Code:
$ awk 'gsub(host_prefix,"\n\n&")' host_prefix=pwrm  file


pwrm16: Info: Thu Dec 12 22:20:23 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. 

pwrm17: Info: Sun Dec 15 21:37:30 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. 

pwrm18: Info: Wed Dec 18 11:31:43 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. 

pwrm16: Info: Wed Dec 18 14:00:05 2013 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. 

pwrm16: Info: Thu Jan 02 13:51:24 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. 

pwrm19: Info: Fri Jan 17 01:16:26 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition. 

pwrm16: Info: Fri Jan 17 03:39:19 2014 User PWPYNYFWD01 at position 170.198.8.160/net on host 170.198.8.160 using application 257 has been disconnected due to an overflow condition.

Code:
$ awk 'gsub(host_prefix,"\n\n&")' host_prefix=pwrm <<<$Variable


Last edited by Akshay Hegde; 01-19-2014 at 03:47 PM..
# 4  
Old 01-20-2014
Hi thanks for ur responses .. I will try these and get back to u when I get to work tomorrow .. I try to set a virtual host and try it now
good day

---------- Post updated at 02:52 PM ---------- Previous update was at 02:46 PM ----------

Ashkey Hedge .. I do not have a file to cat . I ssh to certain hosts and get that information from log file and assign it to A variable so I'm not sure what u mean bu catting the file? I can read the variable.

also the host . I'm not sure what you mean by this?
host prefix is pwrm and a number. It can be anything from pwrm01 through pwrm19 ..

awk 'gsub(host_prefix,"\n\n&")' host_prefix=pwrm <<<$Variable
so I'm not sure how this will work. Maybe I'm missing soething here but not sure what toi substitute the host_prefix with as it will change .?

# 5  
Old 01-20-2014
Hello,

Following may help you.

Code:
a=`echo "120|000000002|2PM||0|0098442940|0|0134515050|MOBILE 013|0||03|01|NL02||0|11|1|1|C|20130401175314|20130401175317|+0800|3|0|10|0||02|0|||M"`
echo $a | perl -ple 's/MOBILE/\n/'

Output will be as follows.

Code:
120|000000002|2PM||0|0098442940|0|0134515050|
 013|0||03|01|NL02||0|11|1|1|C|20130401175314|20130401175317|+0800|3|0|10|0||02|0|||M

It is just a simple example for changing a word with a new line for a variable.



Thanks,
R. Singh
# 6  
Old 01-20-2014
cfajohnson ,, Thanks for ur reply .. I just tested the reassignment of the variable and it works great. But please remember , I have to print everything after the hostname until the occurence of the next host name on a new line. You reply does not address my question in full though is very useful ...So the output should be.

my input is assigned to variable i is as follow :
hostname1: text goes here hostname2: text goes here hostname3: text goes here

The output should be:
hostname1 text here
hostname2 text here
hostname3 text here

---------- Post updated at 03:04 PM ---------- Previous update was at 03:01 PM ----------

Ravindersingh13 - Thanks for ur reply .. I appreciate the perl command. But I won;t be able to use it in this script. I need to use the shell command to do this. Can you help with a shell command:

my input is assigned to variable i is as follow :
hostname1: text goes here hostname2: text goes here hostname3: text goes here

The output should be:
hostname1 text here
hostname2 text here
hostname3 text here Image
# 7  
Old 01-20-2014
If your variable is "A" then use like this

Code:
$ awk '{for(i=1;i<=NF;i++)printf  i!=1 && $(i+1)~/Info:/ ? RS RS $i : i < NF ? $i FS : $i RS RS}' <<<$A


OR

Code:
$ echo $A |  awk '{for(i=1;i<=NF;i++)printf  i!=1 && $(i+1)~/Info:/ ? RS RS $i : i < NF ?  $i  FS : $i RS RS}'

Hope this would be fine for you.

Last edited by Akshay Hegde; 01-20-2014 at 04:40 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove matching pattern on each line with number variations

Hello folks! I have a file containing lines like this Something text 18:37Remove This: 1,111"Keep this text" Some more text 19:37Remove This: 222"Keep this text" More text 20:50Remove This: 3,333Keep this text And more text 25:50Remove This: 44,444Keep this text I would like to... (4 Replies)
Discussion started by: martinsmith
4 Replies

2. Shell Programming and Scripting

sed command not working

cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend this is my file when i use sed 's/Unix/Linux/' bipin.txt all the occurences are getting... (0 Replies)
Discussion started by: Bipin_1991
0 Replies

3. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

sed adding a new line not working

sed '/patternstring/ a\ new line string' file1 The above code is not working even with the i option.... it shows sed grambled if '\' after new line string is not being used....after using no changes it is displaying..Pls help (5 Replies)
Discussion started by: bhavanabahety
5 Replies

6. Shell Programming and Scripting

sed adding a new line not working

The file which is used is /abc/apps/cobbbbbb/apps/abadv/binder/axyz.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/na6115.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/kc22.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/tr4823.bnd /abc/apps/cobbbbbb/apps/abcmp/binder/cpc0105.bnd The commads which I ran... (3 Replies)
Discussion started by: bhavanabahety
3 Replies

7. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

8. Shell Programming and Scripting

Sed: Working on a line Previous to a pattern.

Hello everyone, I am working with some train time tables, and i have hit a bit of a road block. Using grep/sed i have done a reasonable job of parsing the html into comma delimited format, but NJ transit prints The Track number and status on a new line, and I would much prefer it all on a... (6 Replies)
Discussion started by: mussen
6 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

using sed to replace a line is not working

This is what I have this far rsh server1 "cat /home/test.txt |sed s/01-jun-2009/01-aug-2009/ |sed s/ABCD/1234/" but it is not working is there something I am doing wrong in my syntax? The file test.txt is the same on all of my 15 servers it has the same length and contents only certain... (3 Replies)
Discussion started by: deaconf19
3 Replies
Login or Register to Ask a Question