0

I having a file with three fields name,Rev_ration and sync which is :(colon delimeted) as shown below:

##Increase 
JOe:4.8:7

##Check as per revision 67
jonny:[5:9]:[7:0]
##Check as per revision 56
Salman:q[0:234]:5

Initially I am replacing the : which is in square brackets to !. SecondlyExecuting my AWK. Third I am replacing the ! which is in square brackets to : I am trying below code:

#!/bin/bash
sed 's/\([[0-9]\+\)!\([0-9]\+]\)/\1:\2/g' t5 >> t6
awk -F: '/^$/ /^#/ {c=$0; sub(/#+/,"", c); next} 
                {print $1,$2,"- message", "\"" c "\""}' t6 >> t7
sed 's/\([[0-9]\+\)!\([0-9]\+]\)/\1:\2/g' t7 >> t8

Getting syntax error

Above code is working fine but not able to ignore the empty lines

I need to get rid of empty lines inside the input file.

Desired output

JOe:4.8:7 - message "Increase "
jonny:[5:9]:[7:0] - message "Check as per revision 67"
Salman:q[0:234]: - message "Check as per revision 56"

How to club all three commands and how to get rid of empty lines.. How to fix that with the code shown above

1
  • @Kusalananda it got to know how to fix that issue but not able to remove empty lines.
    – Kavitha
    Jun 16 '20 at 6:50
0

As it doesn't seem that you're actually doing anything with your :-delimited fields, we may concentrate on the lines starting with ##, pick out the text from them, and insert the text on the following lines.

Using only sed:

$ sed '/^$/d; /^##/{ s///; h; d; }; G; s/\n\(.*\)/ - message "\1"/' file
JOe:4.8:7 - message "Increase "
jonny:[5:9]:[7:0] - message "Check as per revision 67"
Salman:q[0:234]:5 - message "Check as per revision 56"

The sed expression deletes empty lines with /^$/d. It then saves any text after ## into the hold space with h (after deleting the initial ##, replacing whatever may be stored in the hold space already). For any other line that is not empty or a line that starts with ##, it appends the text from the hold space with G and modifies it so that the added text is prefixed with - message " and terminated by ".

4
  • In my above code itself how can I do that by ignoring the empty lines in AWK command because the input files which I am going to be used is some what different
    – Kavitha
    Jun 16 '20 at 6:56
  • @Kavitha Empty lines matches the regular expression ^$. In awk, you could ignore empty lines with /^$/ { next }. My answer produces the correct result for your given text though, so if your actual data looks different, please update the data in the question.
    – Kusalananda
    Jun 16 '20 at 7:00
  • I add it with my above code but getting syntax error
    – Kavitha
    Jun 16 '20 at 7:17
  • @Kavitha Yes, because you forgot the { next } bit. Also, if you update the code in the question, what's the point of the question?
    – Kusalananda
    Jun 16 '20 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.