I have a file that I need to add 3 new lines of text for every line in the file - when I attempt the sed scripting below I keep getting 'command garbled' errors. Any suggestions would be greatly appreciated
#!/bin/ksh
list=`cat /export/home/list`
for i in $list
do
sed ' a \
send_notification: F \n
notification_msg: "Process Has Failed "\n
notification_emailaddress: 720394@tmomail.net' >> list.out
done
I don't know what you're running sed for.
while read LINE
do
cat <<EOF
send_notification: F \n
notification_msg: "Process Has Failed "\n
notification_emailaddress: 720394@tmomail.net
EOF
done < list.txt > list.out
This worked to a point - it did not pass in the original lines from the list.txt file - just wrote out the 3 new lines for every line in the file - I need all 4 lines in final output. Sorry but I am new to scripting and thought sed was the way to go.
If you would have shown us an input file and what you wanted the output to look like as well as the code you were using to try to get that result, we might have given you something more like:
#!/bin/ksh
list=/export/home/list
list=list
sed ' a \
send_notification: F \
notification_msg: "Process Has Failed "\
notification_emailaddress: 720394@tmomail.net
' "$list" > list.out
to start with. I'm still making some wild guesses about what the ends of the three lines you're adding are supposed to look like. With an input file that contains:
line1
line2
line3
line4
the above script writes:
line1
send_notification: F
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
line2
send_notification: F
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
line3
send_notification: F
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
line4
send_notification: F
notification_msg: "Process Has Failed "
notification_emailaddress: 720394@tmomail.net
in list.out . Is this what you're trying to do?
Yes that is exactly what I am trying to do. When I attempted to use your script above I am getting the following error:
sed: command garbled: a \
sed is still not required...
while read LINE
do
cat <<EOF
$LINE
send_notification: F \n
notification_msg: "Process Has Failed "\n
notification_emailaddress: 720394@tmomail.net
EOF
done < list.txt > list.out
That worked perfectly!!! Thank you so much - you guys rule
#!/bin/ksh
list=/export/home/list
sed ' a \
send_notification: F \
notification_msg: "Process Has Failed "\
notification_emailaddress: 720394@tmomail.net
' "$list" > list.out
should work as long as there is nothing following the backslash characters shown above in red except the line-terminating <newline> characters.
If you want the literal \n sequences to appear in list.out (as was done in the script provided by Corona688), when using sed you would need to change the above script to:
#!/bin/ksh
list=/export/home/list
sed ' a \
send_notification: F \\n\
notification_msg: "Process Has Failed "\\n\
notification_emailaddress: 720394@tmomail.net
' "$list" > list.out
If you are going to use Corona688's script, to protect against lines in your input that might end with a backslash character, you should change:
while read LINE
to:
while read -r LINE
unless you want lines following lines ending with a backslash to be joined and have the backslashes remove by read instead of being processed as is.